我得到了一个加密文件以及一个 base64 对称密钥和 base 64 IV,并被要求使用 Java 对其进行解密。对数据文件使用的加密是 AES。但是,当我在我的代码中运行加密文件、对称密钥和 IV 时,我收到以下错误:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:913)
这是我的代码:
String encryptedData = "C:\\EncryptedDataFile.data";
FileInputStream fis = null;
File file = new File(encryptedData);
//Convert file into array of bytes
byte[] encryptedDataBytes = new byte[(int) file.length()];
try
{
// Read in array of bytes
fis = new FileInputStream(file);
fis.read(encryptedDataBytes);
fis.close();
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
// AES Key
byte[] decodedKey = Base64.getDecoder().decode("50rofsdb0TnQAQCb702wKz8m6XQeLNj6lamEvivKsh8=");
// decode the base64 encoded string
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); // rebuild key using SecretKeySpec
// IV
byte[] initVecBytes = Base64.getDecoder().decode("OUXLZq4SpyhzNGIei0nerA==");
// Decrypt the cipher text
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivParameterSpec = new IvParameterSpec(initVecBytes);
cipher.init(Cipher.DECRYPT_MODE, originalKey, ivParameterSpec);
byte[] original = cipher.doFinal(encryptedDataBytes);
String s = new String(original);
System.out.println(s);
如果有人可以在这件事上为我提供一些帮助,将不胜感激