我有一个Python应用程序,可以创建一些使用AES / PKCS7加密的文件。我必须使用Java服务读取这些文件。但是我的代码抛出异常:
“javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整”
这是我的解密代码:
public String Decrypt(String strText)
{
try
{
// Text to decrypt
byte[] test = strText.getBytes();
//bytKey is the same key as Python app
SecretKeySpec objKey = new SecretKeySpec(bytKey, "AES");
Cipher objCipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
objCipher.init(Cipher.DECRYPT_MODE, objKey);
// Here I got an exception >>
byte[] bytValue = objCipher.doFinal(test);
return new String(bytValue);
}
catch (Exception exc)
{
exc.printStackTrace();
}
return "";
}
如果我在步入doFinal
之前解码加密文本,我会得到另一个例外:
“javax.crypto.BadPaddingException:pad block corrupted”
public String Decrypt(String strText)
{
try
{
BASE64Decoder decoder = new BASE64Decoder();
byte[] test = decoder.decodeBuffer(strText);
SecretKeySpec objKey = new SecretKeySpec(bytKey, "AES");
Cipher objCipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
objCipher.init(Cipher.DECRYPT_MODE, objKey);
byte[] bytValue = objCipher.doFinal(test);
return new String(bytValue);
}
catch (Exception exc)
{
exc.printStackTrace();
}
return "";
}
我不是加密/解密的专家,我想这很难解决。 有什么想法可以解决这个问题?提前谢谢!