• 大小: 5KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: Java
  • 标签: AES  zeropadding  

资源简介

AESUtil 实现了基于AES的ECB模式,选用了zeropadding填充,数据位为128 加上密码去加解密数据。

资源截图

代码片段和文件信息

package com.thinta.testreadword;







import android.util.base64;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


/*****************************************************
 * 版权信息: 北京振中电子技术有限公司版权所有
 * 创建作者: 郭志文
 * 创建日期: 2018/11/14
 * ****************************************************
 * 更改记录: 更新人:    更新时间:    更新概要:
 *
 * ****************************************************
 *  类功能说明:AES加解密工具类
 * ****************************************************/
public class AESUtils {
    private static final String KEY_ALGORITHM = “AES“;
    private static final String DEFAULT_CIPHER_ALGORITHM = “AES/ECB/NoPadding“;//默认的加密算法

    /**
     * AES 加密操作
     *
     * @param content 待加密内容
     * @param password 加密密码
     * @return 返回base64转码后的加密数据
     */
    public static String encrypt(String content String password) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
            int blockSize = cipher.getBlockSize();

            byte[] byteContent = content.getBytes();

            int plaintextLength = byteContent.length;
            if(plaintextLength % blockSize != 0)
            {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(byteContent 0 plaintext 0 byteContent.length);

            int len = password.getBytes().length;
            if(len % 16 != 0)
            {
                len = len + (16 - (len % 16));
            }

            byte[] newpass = new byte[len];

            System.arraycopy(password.getBytes() 0 newpass 0 password.getBytes().length);

            SecretKeySpec keySpec = new SecretKeySpec(newpass “AES“);

            cipher.init(Cipher.ENCRYPT_MODE keySpec);// 初始化为加密模式的密码器

            byte[] result = cipher.doFinal(plaintext);// 加密

           // return base64.encodebase64String(result);//通过base64转码返回
            return base64.encodeToString(resultbase64.DEFAULT);//通过base64转码返回
        } catch (Exception ex) {
           // Logger.getLogger(AesDec.class.getName()).log(Level.SEVERE null ex);
        }

        return null;
    }

    /**
     * AES 解密操作
     *
     * @param content
     * @param password
     * @return
     */
    public static String decrypt(String content

评论

共有 条评论