AES 加密:InvalidKeyException:密钥长度不是 128/192/256 位
我正在尝试使用 AES 加密 Android 上的字符串。对称密钥之前使用 Diffie-Hellman 算法确定,似乎没问题(密钥长度为 128 位,见下文)。
尽管如此,我得到一个 InvalidKeyException: "Key length not 128/192/256 bits.
"
代码:
KeyAgreement keyAgree = KeyAgreement.getInstance("DH", "BC");
keyAgree.init(this.smartphonePrivKey);
keyAgree.doPhase(serverPubKey, true);
SecretKey key = keyAgree.generateSecret("AES");
System.out.println("Key Length: " + key.getEncoded().length);
System.out.println("Key Algorithm: "+ key.getAlgorithm());
System.out.println("Key Format: "+ key.getFormat());
byte[] encrypted = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
System.out.println("Allowed Key Length: "
+ cipher.getMaxAllowedKeyLength("AES"));
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypted = cipher.doFinal("YEAH".getBytes("UTF8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
上面的代码导致以下输出:
_12-10 20:24:53.119: INFO/System.out(757): Key Length: 128_
_12-10 20:24:53.119: INFO/System.out(757): Key Algorithm: AES_
_12-10 20:24:53.119: INFO/System.out(757): Key Format: RAW_
_12-10 20:24:53.470: INFO/System.out(757): Allowed Key Length: 2147483647_
之后,我得到 InvalidKeyException: Key length不是128/192/256位。
但是正如你所看到的,SecretKey的长度是128位!
有什么想法吗?
I'm trying to encrypt a string on Android with AES. The symmetric key is determined previously with the Diffie-Hellman algorithm and seems to be ok (Key Length is 128 Bit, see below).
Nevertheless, I get a InvalidKeyException: "Key length not 128/192/256 bits.
"
Code:
KeyAgreement keyAgree = KeyAgreement.getInstance("DH", "BC");
keyAgree.init(this.smartphonePrivKey);
keyAgree.doPhase(serverPubKey, true);
SecretKey key = keyAgree.generateSecret("AES");
System.out.println("Key Length: " + key.getEncoded().length);
System.out.println("Key Algorithm: "+ key.getAlgorithm());
System.out.println("Key Format: "+ key.getFormat());
byte[] encrypted = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
System.out.println("Allowed Key Length: "
+ cipher.getMaxAllowedKeyLength("AES"));
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypted = cipher.doFinal("YEAH".getBytes("UTF8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
The above Code leads to the following output:
_12-10 20:24:53.119: INFO/System.out(757): Key Length: 128_
_12-10 20:24:53.119: INFO/System.out(757): Key Algorithm: AES_
_12-10 20:24:53.119: INFO/System.out(757): Key Format: RAW_
_12-10 20:24:53.470: INFO/System.out(757): Allowed Key Length: 2147483647_
After that, I get the InvalidKeyException: Key length not 128/192/256 bits.
But as you can see, the SecretKey has a length of 128 Bits!
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您生成的密钥是 128字节,而不是 128位。 “密钥长度”应为 16。
The key you generated is 128 bytes, not 128 bits. "Key Length" should be 16.
这种异常的发生主要是由于您为加密传递的密钥长度所致。如果您使用 AES 加密,则字符数必须为 128/192/256 位长度。
例如,您可以使用 16 个字符、24 个字符或 32 个字符的密钥。
希望这有帮助...
This exception basically occur due to length of key that you hava passed for encryption.If you are using AES encryption then the number of characters must be in length of 128/192/256 bits.
For example you can use the key of 16 character,24 character or 32 character.
Hope this help...