我是AES -cbc模式,解码时遇到这个错误,解决问题的关键是把输出方式 由hex改为base64:
public static String decode(String secret, String iv, String content) throws Exception {
byte[] secretKeyBytes = secret.getBytes();
SecretKey secretKey = new SecretKeySpec(secretKeyBytes, KEY_ALGORITHM);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(CBC_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
//byte[] contentBytes = hexStringToBytes(content);
byte[] contentBytes = new BASE64Decoder().decodeBuffer(content);
byte[] decodeBytes = cipher.doFinal(contentBytes);
return new String(decodeBytes, ENCODING);
}
byte[] contentBytes = hexStringToBytes(content); 这里输出方式为hex,
修改输出方式为base64: byte[] contentBytes = Base64Utils.decode(content);
就解决了。
另外:附上AES解码小工具,可以辅助解决问题,https://tool.lmeee.com/jiami/aes