需要解决AES中错误的IV长度 [英] Need solution for wrong IV length in AES

查看:307
本文介绍了需要解决AES中错误的IV长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java实现AES,这是我使用的代码:

  byte [] sessionKey = { 00000000000000000000000000000000000000}; 
byte [] iv = {00000000000000000000000000000000};
byte [] plaintext = 6a84867cd77e12ad07ea1be895c53fa3 .getBytes();
Cipher cipher = Cipher.getInstance( AES / CBC / PKCS5Padding);

cipher.init(Cipher.ENCRYPT_MODE,新的SecretKeySpec(sessionKey, AES),新的IvParameterSpec(iv));
byte [] ciphertext = cipher.doFinal(plaintext);

cipher.init(Cipher.DECRYPT_MODE,新的SecretKeySpec(sessionKey, AES),新的IvParameterSpec(iv));
byte [] deciphertext = cipher.doFinal(ciphertext);

出于测试目的,我需要此固定密钥和IV,但出现以下异常:

 线程主中的异常
java.security.InvalidAlgorithmParameterException:
错误的IV长度:必须为16个字节长
com.sun.crypto.provider.SunJCE_h.a(DashoA12275)at
com.sun.crypto.provider.AESCipher.engineInit(DashoA12275)at
javax.crypto.Cipher.a(
的DashoA12275)javax.crypto.Cipher.a(DashoA12275)

javax.crypto.Cipher.init(DashoA12275)
javax.crypto.Cipher.init(DashoA12275)

如何在此AES实现中使用此固定IV?有办法吗?

解决方案

首先,

  byte [] iv = {00000000000000000000000000000000000000}; 

创建一个大小为1的字节数组,而不是大小为32的字节数组(如果您打算这样做)



其次,AES的IV大小应为16字节或128位(这是AES-128的块大小)。如果使用AES-256,则IV大小应为128位大,因为AES标准仅允许128位块大小。原始的Rijndael算法允许其他块大小,包括256位长的块大小。



第三,如果您打算使用AES-256,则不会出现这种情况。的盒子。您需要下载并安装 JCE无限强度管辖权政策文件(滚动到底部页面);我还建议您阅读随附的许可证。



这将导致您的代码发生以下更改:

  byte [] iv = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};最后,初始化向量的意思是唯一且不可预测的。一个16字节的序列(每个字节由0表示)不适合用于IV。如果这是生产代码,请考虑寻求帮助。


I'm trying to implement AES in Java and this is the code I use:

 byte[] sessionKey = {00000000000000000000000000000000};
 byte[] iv = {00000000000000000000000000000000};
 byte[] plaintext = "6a84867cd77e12ad07ea1be895c53fa3".getBytes();
 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

 cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
 byte[] ciphertext = cipher.doFinal(plaintext);

 cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
 byte[] deciphertext = cipher.doFinal(ciphertext);

I need this fixed key and IV for test purpose but I get the following exception:

Exception in thread "main"
java.security.InvalidAlgorithmParameterException: 
  Wrong IV length: must be 16 bytes long    at
com.sun.crypto.provider.SunJCE_h.a(DashoA12275)     at
com.sun.crypto.provider.AESCipher.engineInit(DashoA12275)   at
javax.crypto.Cipher.a(DashoA12275)  at
javax.crypto.Cipher.a(DashoA12275)  at
javax.crypto.Cipher.init(DashoA12275)   at
javax.crypto.Cipher.init(DashoA12275)

How can I use this fixed IV with this implementation of AES? Is there any way?

解决方案

Firstly,

byte[] iv = {00000000000000000000000000000000};

creates a byte array of size 1 and not a byte array of size 32 (if that is your intention).

Secondly, the IV size of AES should be 16 bytes or 128 bits (which is the block size of AES-128). If you use AES-256, the IV size should be 128 bits large, as the AES standard allows for 128 bit block sizes only. The original Rijndael algorithm allowed for other block sizes including the 256 bit long block size.

Thirdly, if you are intending to use a AES-256, this does not come out of the box. You need to download and install the JCE Unlimited Strength Jurisdiction Policy Files (scroll to the bottom of the page); I would also recommend reading the accompanying license.

This would result in the following change to your code:

byte[] iv = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

Finally, the initialization vector is meant to be unique and unpredictable. A sequence of 16 bytes, with each byte represented by a value of 0, is not a suitable candidate for an IV. If this is production code, consider getting help.

这篇关于需要解决AES中错误的IV长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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