java EncryptionException: javax.crypto.IllegalBlockSizeException: 使用填充密码解密时,输入长度必须是 8 的倍数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7640463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EncryptionException: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
提问by Honus Wagner
I have inherited an old java project from 2006 (the original dev is long gone, and I've never coded Java before) where I am getting this error:
我从 2006 年继承了一个旧的 Java 项目(原始开发人员早已不复存在,而且我以前从未编写过 Java 代码),我收到此错误:
EncryptionException: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
EncryptionException: javax.crypto.IllegalBlockSizeException: 使用填充密码解密时,输入长度必须是 8 的倍数
The code it references looks like this:
它引用的代码如下所示:
public String decrypt( String encryptedString ) throws EncryptionException
{
if ( encryptedString == null || encryptedString.trim().length() <= 0 )
throw new IllegalArgumentException( "encrypted string was null or empty" );
try
{
SecretKey key = keyFactory.generateSecret( keySpec );
cipher.init( Cipher.DECRYPT_MODE, key );
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] cleartext = base64decoder.decodeBuffer( encryptedString );
byte[] ciphertext = cipher.doFinal( cleartext );
return bytes2String( ciphertext );
}
catch (Exception e)
{
throw new EncryptionException( e );
}
}
I'm not entirely sure of the inner workings of the program, but I do know that in this project directory are a few config files, and a key.properties file. As far as "Input Lengths" go (as the error message refers to), my the password for the database is 15 chars long, and the "key" in key.properties is 25 chars long. I have no idea if that matters or not.
我不完全确定程序的内部工作原理,但我知道在这个项目目录中有一些配置文件和一个 key.properties 文件。就“输入长度”而言(如错误消息所指),我的数据库密码长度为 15 个字符,key.properties 中的“密钥”长度为 25 个字符。我不知道这是否重要。
Things to note:
注意事项:
- I have tried changing the DB password to 16 chars (a multiple of 8) but to no avail.
- I have read thisand thisand they have not helped
- I am moving this project from one server to another. It works on its original server.
- The original server runs JRE 1.4.2. The new server runs JRE 1.6u27.
- I REALLYdon't want to have to rebuild the .jar. I am not a java developer and the project is pretty massive.
- 我曾尝试将数据库密码更改为 16 个字符(8 的倍数),但无济于事。
- 我已经阅读了这个和这个,但他们没有帮助
- 我正在将此项目从一台服务器移动到另一台服务器。它在其原始服务器上运行。
- 原始服务器运行 JRE 1.4.2。新服务器运行 JRE 1.6u27。
- 我真的不想重建 .jar。我不是 Java 开发人员,该项目非常庞大。
Thanks for all your help.
感谢你的帮助。
回答by erickson
The input to which the error message refers is the cipher text (oddly-named cleartext
), the result of the Base-64 decoding operation. Make sure that the encryptedString
you are passing to this method decodes to a byte array with length that is a multiple of 8.
错误消息所指的输入是密文(名字很奇怪cleartext
),它是 Base-64 解码操作的结果。确保encryptedString
您传递给此方法的 解码为长度为 8 的倍数的字节数组。
回答by Case
You should probably not change the JRE version unless you want to re-examine the code. I would try downgrading your JRE version on the new server before anything else, especially since the code previously worked.
除非您想重新检查代码,否则您可能不应该更改 JRE 版本。我会先尝试在新服务器上降级您的 JRE 版本,尤其是因为代码以前可以工作。