AES加密解密

1、AES-Zip压缩-Base64 加密

//AES加密
public static String _aesEncrypt(String str) throws Exception {
    String key = "1234567800000000";//密钥
    if (str == null || key == null) return null;
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));// 初始化 
    byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));//1-加密
    byte[] zipStr = ZipUtils.ecompress(bytes);//2-压缩        
    return new BASE64Encoder().encode(zipStr);//3-base64
}
//AES解密
public static String _aesDecrypt(byte[] str) throws Exception {
    if (null == str) return null;
    byte[] buff= Base64.decodeBase64(str);
    byte[] miwen=ZipUtils.decompress(buff);//解压缩    
    String key = "1234567800000000";//秘钥 
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
    byte[] bytes = cipher.doFinal(miwen);      
    return new String(bytes, "utf-8");
}

未完待续...

你可能感兴趣的:(AES加密解密)