AES-CBC模式加密

yuanheci 2022年12月19日 237次浏览

  项目原先使用的是DES加密,出于安全性考虑需要改成AES加密。
  AES的加密解决算法网上一大堆,但是大部分都没有解决一个问题,就是windows上测试正常,但在部分linux操作系统下却出现加密解密异常!比如会发现加密的结果值一直会变,这是因为实例化SecureRandom的方式不对造成的。
  默认初始化方式为:

generator.init(new SecureRandom(KEY_STR.getBytes()));

  需要改成:

SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY_STR.getBytes());
keygen.init(128, random);

  还有一点需要注意的是,建议AES加密结果用Base64再做加密处理,即使用下面工具类中的encodeBase64方法。因为只有AES加密的话结果值可能带有+号,这个作为参数在地址栏中使用的话会被当做拼接串处理,后端接收到这个值的时候会被解析成+号变成空格的情况。

  以下就是整个AES加密解密的工具类的实现:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.util.Base64;
/**
 * AES加密解密
 */
public class AESUtil {
    private static final String default_key = "12345678";
    static final Base64.Decoder decoder = Base64.getDecoder();
    static final Base64.Encoder encoder = Base64.getEncoder();
    static final String charset = "utf-8";
    static final String AES = "AES";
    /**
     * 先AES加密,再Base64加密
     * @param content
     * @return
     */
    public static String encodeBase64(String content){
        String encode = encode(content);//AES加密
        if(encode == null) return null;//加密失败返回空
        try {
            String s = encoder.encodeToString(encode.getBytes(charset));//Base64加密
            return s;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 先Base64解密,再AES解密
     * @param content
     * @return
     */
    public static String decodeBase64(String content){
        try {
            String s = new String(decoder.decode(content), charset);//Base64解密
            String decode = decode(s);//AES解密
            return decode;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;//解密失败返回空
    }
    /**
     * AES加密
     * @param content
     * @return
     */
    public static String encode(String content){
        return encode(default_key, content);
    }
    /**
     * AES解密
     * @param content
     * @return
     */
    public static String decode(String content){
        return decode(default_key, content);
    }
    /*
     * AES加密
     * 1.构造密钥生成器
     * 2.根据ecnodeRules规则初始化密钥生成器
     * 3.产生密钥
     * 4.创建和初始化密码器
     * 5.内容加密
     * 6.返回字符串
     */
    public static String encode(String encodeRules, String content){
        try {
            //1.构造密钥生成器,指定为AES算法,不区分大小写
            KeyGenerator keygen = KeyGenerator.getInstance(AES);
            //2.根据ecnodeRules规则初始化密钥生成器
            //生成一个128位的随机源,根据传入的字节数组
//            keygen.init(128, new SecureRandom(encodeRules.getBytes()));
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(encodeRules.getBytes());
            keygen.init(128, random);
            //3.产生原始对称密钥
            SecretKey original_key = keygen.generateKey();
            //4.获得原始对称密钥的字节数组
            byte [] raw = original_key.getEncoded();
            //5.根据字节数组生成AES密钥
            SecretKey key = new SecretKeySpec(raw, AES);
            //6.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance(AES);
            //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte [] byte_encode = content.getBytes(charset);
            //9.根据密码器的初始化方式--加密:将数据加密
            byte [] byte_AES = cipher.doFinal(byte_encode);
            //10.将加密后的数据转换为字符串
            //这里用Base64Encoder中会找不到包
            //解决办法:
            //在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
            String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
            //11.将字符串返回
            return AES_encode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;//加密失败返回空
    }
    /*
     * AES解密
     * 解密过程:
     * 1.同加密1-4步
     * 2.将加密后的字符串反纺成byte[]数组
     * 3.将加密内容解密
     */
    public static String decode(String encodeRules, String content){
        try {
            //1.构造密钥生成器,指定为AES算法,不区分大小写
            KeyGenerator keygen = KeyGenerator.getInstance(AES);
            //2.根据ecnodeRules规则初始化密钥生成器
            //生成一个128位的随机源,根据传入的字节数组
//            keygen.init(128, new SecureRandom(encodeRules.getBytes()));
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(encodeRules.getBytes());
            keygen.init(128, random);
            //3.产生原始对称密钥
            SecretKey original_key = keygen.generateKey();
            //4.获得原始对称密钥的字节数组
            byte [] raw = original_key.getEncoded();
            //5.根据字节数组生成AES密钥
            SecretKey key = new SecretKeySpec(raw, AES);
            //6.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance(AES);
            //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);
            //8.将加密并编码后的内容解码成字节数组
            byte [] byte_content = new BASE64Decoder().decodeBuffer(content);
            /*
             * 解密
             */
            byte [] byte_decode = cipher.doFinal(byte_content);
            String AES_decode = new String(byte_decode,charset);
            return AES_decode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;//解密失败返回空
    }
    public static void main(String[] args) {
        String a = "1236";
        String s = encodeBase64(a);
        System.out.println(s);
        String s1 = decodeBase64(s);
        System.out.println(s1);
    }
}