密码:IllegalBlockSizeException的原因是什么? [英] Cipher: What is the reason for IllegalBlockSizeException?

查看:121
本文介绍了密码:IllegalBlockSizeException的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用密码

加密代码:

Cipher aes = Cipher.getInstance("AES");
aes.init(Cipher.ENCRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());

解密代码:

Cipher aes = Cipher.getInstance("AES");
aes.init(Cipher.DECRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());

运行解密代码时,我得到IllegalBlockSizeException(输入长度​​必须是16的倍数)。

I get IllegalBlockSizeException ( Input length must be multiple of 16 when ...) on running the Decrypt code.

但是如果我将解密代码更改为

But If I change the decrypt code to

Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding"); //I am passing the padding too
aes.init(Cipher.DECRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());

工作正常。
我知道它的格式为 algorithm / mode / padding 。所以我想是因为我没有提到填充。因此,我尝试在加密过程中提供模式和填充,

It works fine. I understand that it is in the pattern algorithm/mode/padding. So I thought it is because I didn't mention the padding. So I tried giving mode and padding during encryption,

加密代码:

Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");//Gave padding during encryption too
aes.init(Cipher.ENCRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());

解密代码:

Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.DECRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());

但由于IllegalBlockSizeException而失败。

But it fails with IllegalBlockSizeException.

原因是什么,为什么会发生异常以及其下到底发生了什么。
如果有人可以提供帮助?预先感谢

What is the reason, why the exception and what is exactly happening underneath. If anyone can help? Thanks in advance

更新

看起来问题出在字符串I正在加密和解密。因为,即使我说的代码也行不通,我基本上是在加密UUID(例如:8e7307a2-ef01-4d7d-b854-e81ce152bbf6)。

Looks like the issue is with the string I am encrypting and decrypting. Because, even the code that I said works, doesn't always work. I am basically encrypting UUIDs (eg : 8e7307a2-ef01-4d7d-b854-e81ce152bbf6). It works with certain strings and doesn't with certain others.

加密的字符串的长度为64,可被16整除。是的,我在同一字符串上运行它

The length of encrypted String is 64 which is divisible by 16. Yes, I am running it on the same machine.

生成密钥的方法:

    private Key generateKey() throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("SHA");
            String passphrase = "blahbl blahbla blah";
    digest.update(passphrase.getBytes());
    return new SecretKeySpec(digest.digest(), 0, 16, "AES");
}


推荐答案

解密期间,一个可以<如果输入数据不是块大小的倍数(AES为16字节),则em> only 会得到 IllegalBlockSizeException

During decryption, one can only get an IllegalBlockSizeException if the input data is not a multiple of the block-size (16 bytes for AES).

如果键或数据无效(但长度正确),则会得到 BadPaddingException ,因为PKCS#5填充错误以明文形式。偶尔,填充似乎会是正确的,您一点也不例外。

If the key or the data was invalid (but correct in length), you would get a BadPaddingException because the PKCS #5 padding would be wrong in the plaintext. Very occasionally the padding would appear correct by chance and you would have no exception at all.

N.B。我建议您始终指定填充和模式。如果您不这样做,则提供商会更改默认设置,您可能会感到惊讶。 AFAIK,Sun提供程序将 AES 转换为 AES / ECB / PKCS5Padding

N.B. I would recommend you always specify the padding and mode. If you don't, you are liable to be surprised if the provider changes the defaults. AFAIK, the Sun provider converts "AES" to "AES/ECB/PKCS5Padding".

这篇关于密码:IllegalBlockSizeException的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆