AES 加密:InvalidKeyException:密钥长度不是 128/192/256 位

发布于 2024-10-06 18:17:50 字数 1686 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-10-13 18:17:50

您生成的密钥是 128字节,而不是 128。 “密钥长度”应为 16。

The key you generated is 128 bytes, not 128 bits. "Key Length" should be 16.

如何视而不见 2024-10-13 18:17:50

这种异常的发生主要是由于您为加密传递的密钥长度所致。如果您使用 AES 加密,则字符数必须为 128/192/256 位长度。
例如,您可以使用 16 个字符、24 个字符或 32 个字符的密钥。

String encrypted_data=AES.encrypt("HELLO","ASDFGHJKLASDFGHJ");

希望这有帮助...

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.

String encrypted_data=AES.encrypt("HELLO","ASDFGHJKLASDFGHJ");

Hope this help...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文