想要使用32字节的AES 256 CBC,但它显示java.security.invalidalgorithParameterException
我使用的是AES 256 CBC。我有32字节的IV。但当我运行它时,它会显示一个异常,如下所示:想要使用32字节的AES 256 CBC,但它显示java.security.invalidalgorithParameterException,java,security,aes,Java,Security,Aes,我使用的是AES 256 CBC。我有32字节的IV。但当我运行它时,它会显示一个异常,如下所示: Exception in thread "main" java.lang.RuntimeException: java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long at com.abc.aes265cbc.AESUtil.decrypt(AESUtil.java:50
Exception in thread "main" java.lang.RuntimeException: java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
at com.abc.aes265cbc.AESUtil.decrypt(AESUtil.java:50)
at com.abc.aes265cbc.Security.main(Security.java:48)
Caused by: java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:430)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:217)
at javax.crypto.Cipher.implInit(Cipher.java:790)
at javax.crypto.Cipher.chooseProvider(Cipher.java:848)
at javax.crypto.Cipher.init(Cipher.java:1347)
at javax.crypto.Cipher.init(Cipher.java:1281)
at com.abc.aes265cbc.AESUtil.decrypt(AESUtil.java:47)
... 1 more
我不知道怎么解决这个问题。我搜索过了,但我不知道如何解决这个问题。我第一次尝试安全概念。
我的AES 256 CBC代码为:
public static void setENCRYPTION_IV(String ENCRYPTION_IV) {
AESUtil.ENCRYPTION_IV = ENCRYPTION_IV;
}
public static void setENCRYPTION_KEY(String ENCRYPTION_KEY) {
AESUtil.ENCRYPTION_KEY = ENCRYPTION_KEY;
}
public static String encrypt(String src) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, makeKey(), makeIv());
return Base64.encodeBytes(cipher.doFinal(src.getBytes()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String decrypt(String src) {
String decrypted = "";
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, makeKey(), makeIv());
decrypted = new String(cipher.doFinal(Base64.decode(src)));
} catch (Exception e) {
throw new RuntimeException(e);
}
return decrypted;
}
static AlgorithmParameterSpec makeIv() {
try {
return new IvParameterSpec(ENCRYPTION_IV.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
static Key makeKey() {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] key = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
return new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
你能帮助我,通过改变什么在这段代码中,我将能够使用32字节的IV。
提前谢谢
编辑:
调用此函数的My main函数:
AESUtil.setENCRYPTION_KEY("96161d7958c29a943a6537901ff0e913efaad15bd5e7c566f047412179504ffb");
AESUtil.setENCRYPTION_IV("d41361ed2399251f535e65f84a8f1c57");
String decrypted = AESUtil.decrypt(new String(sw0SrUIKe0DmS7sRd9+XMgtYg+BUiAfiOsdMw/Lo2RA=)); // AES Decrypt
AES算法具有128位的块大小,而不管密钥长度是256位、192位还是128位 当对称密码模式需要IV时,IV的长度必须等于密码的块大小。因此,在AES中必须始终使用128位(16字节)的IV
无法将32字节的IV与AES一起使用。您能否提供一个可编译、可运行的开箱即用版本,并显示问题所在?(包括一个main方法,它演示了您以何种顺序调用哪些方法)您希望使用32字节(256字节)的原因是什么?查看我的更新答案-对于
AES/CBC/PKCS5Padding
,blockLength=key Length=IV Length=128位(16字节)。我已经添加了我的keys值…这将向您显示我得到的例外情况。Duncan,我已投票删除我的答案,因为您的答案非常清晰明了:)对于任何未来的读者,我只想保留一个提示,为了使用AES-256,有必要。否则您将获得java.security.InvalidKeyException:非法密钥大小
,即使是密钥本身,也不只是IV。@Andreas谢谢,非常感谢。我很抱歉抢了你的风头,但我想确保未来的读者能得到一个简洁的答案。你关于无限制文件的观点总是值得记住:-)你能帮我解决这些问题吗(