javax.crypto.IllegalBlockSizeException:解密中的最后一个块不完整 - 解密加密的AES字符串
2022-09-02 22:06:23
我正在尝试解密我从后端服务器收到的字符串,该服务器使用OpenSSL使用AES-256-CBC加密字符串。有代码块:"~9?8?m???=?T?G"
public static String decryptText(String textToDecrypt) {
try {
byte[] base64TextToDecrypt = Base64.encodeBase64(textToDecrypt.getBytes("UTF-8"));
byte[] guid = "fjakdsjkld;asfj".getBytes("UTF-8");
byte[] iv = new byte[16];
System.arraycopy(guid, 0, iv, 0, guid.length);
IvParameterSpec ips = new IvParameterSpec(iv);
byte[] secret = DECRYPTION_SECRET_HASH.getBytes("UTF-8");
SecretKeySpec secretKey = new SecretKeySpec(secret, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// decryption pass
cipher.init(Cipher.DECRYPT_MODE, secretKey, ips);
byte[] converted = cipher.doFinal(base64TextToDecrypt);
System.out.println(new String(converted));
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Decipher error for " + textToDecrypt, e);
}
return "";
}
不幸的是,当我到达
byte[] converted = cipher.doFinal(base64TextToDecrypt);
语句将引发以下异常:
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
有什么想法吗?