在做AES加密之后再进行base64输出时遇到的坑,因为编码不一致的问题导致最后的输出结果不一致
pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
AES加密
/**
* 算法
*/
private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";
/**
* base 64 encode
*
* @param bytes 待编码的byte[]
* @return 编码后的base 64 code
*/
private static byte[] base64Encode(byte[] bytes) {
return Base64.encodeBase64(bytes);
}
/**
* base 64 decode
*
* @param base64Code 待解码的base 64 code
* @return 解码后的byte[]
* @throws Exception 抛出异常
*/
private static byte[] base64Decode(String base64Code){
//return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code);
return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code.getBytes(StandardCharsets.UTF_8));
}
/**
* AES加密
*
* @param content 待加密的内容
* @return 加密后的byte[]
*/
private static byte[] aesEncryptToBytes(String content) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(StandardCharsets.UTF_8));
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
}
/**
* AES加密为base 64 code
*
* @param content 待加密的内容
* @return 加密后的base 64 code
*/
public static String aesEncrypt(String content) throws Exception {
byte[] bytes = base64Encode(aesEncryptToBytes(content));
String s = new String(bytes, StandardCharsets.UTF_8);
System.out.println(s);
return s;
}
/**
* AES解密
*
* @param encryptBytes 待解密的byte[]
* @return 解密后的String
*/
private static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES"), iv);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes, StandardCharsets.UTF_8);
}
/**
* 将base 64 code AES解密
*
* @param encryptStr 待解密的base 64 code
* @param decryptKey 解密密钥
* @return 解密后的string
*/
public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(decryptKey));
}
问题
private static byte[] aesEncryptToBytes(String content) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(StandardCharsets.UTF_8));
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
}
之前在这里的**cipher.doFinal(content.getBytes(StandardCharsets.UTF_8))**语句里没有指定utf8字符集,
导致在打包jar包后和在idea中运行得到的加密结果不同
分析
没有具体查证,仅自我分析。
因为在idea中运行,虽然没有指定utf8字符集,但可能idea本身的指定utf8被使用了,但打包为jar包在其它平台上运行后,就会使用其它平台的默认字符集,使得加密的结果不同