Java AES 密钥长度无效:128 字节?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23392085/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:46:46  来源:igfitidea点击:

Invalid AES key length: 128 bytes?

javaencryptionaes

提问by vrwim

I am getting java.security.InvalidKeyException: Invalid AES key length: 128 byteson my line

我要java.security.InvalidKeyException: Invalid AES key length: 128 bytes上线了

CIPHER.init(Cipher.ENCRYPT_MODE, keySpec);

with CIPHER being

密码是

Cipher CIPHER = Cipher.getInstance("AES");

and keySpec

和 keySpec

SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

that key is a byte[]of length 128 I got through a Diffie-Hellman key exchange (though it shouldn't matter where I got it, right?), keyis completely filled with nonzero bytes

该密钥是一个byte[]长度为 128 的密钥,我是通过 Diffie-Hellman 密钥交换获得的(尽管我从哪里得到它并不重要,对吧?),key完全充满了非零字节

Why is Cipher.init(...)complaining that the key is a wrong length? This webpageclearly states that a key of length 128 is supported.

为什么Cipher.init(...)抱怨密钥长度错误?该网页明确指出支持长度为 128 的密钥。

What am I overlooking?

我在看什么?

采纳答案by Konstantin V. Salikhov

I think you need 128 bit key here for AES algorithm - not 128 byte. To convert your long key to needed length you could try something like password-based key derivation function. See PBKDF2 for example.

我认为您需要 128 位密钥用于 AES 算法 - 而不是 128 字节。要将您的长密钥转换为所需的长度,您可以尝试类似基于密码的密钥派生功能。例如,请参见 PBKDF2。

回答by Khalid Habib

AES algorithm allows 128, 192 or 256 bit key length. which is 16, 24 or 32 byte. your keys length should be 16 , 24 or 32 bytes.

AES 算法允许 128、192 或 256 位密钥长度。这是 16、24 或 32 字节。您的密钥长度应为 16 、 24 或 32 字节。