Java AES 128解密中预期的IV长度0错误

Java AES 128解密中预期的IV长度0错误,java,android,aes,caesar-cipher,Java,Android,Aes,Caesar Cipher,我知道有人问了很多这样的问题。但就我而言,错误在于: java.security.InvalidAlgorithmParameterException: expected IV length of 0 我正在尝试AES 128 CBC模式 代码: 如果我将init vector更改为如下所示的错误: byte[] iv = new byte[]{}; 我收到错误信息: java.security.InvalidAlgorithmParameterException: expected IV

我知道有人问了很多这样的问题。但就我而言,错误在于:

java.security.InvalidAlgorithmParameterException: expected IV length of 0
我正在尝试AES 128 CBC模式

代码:

如果我将init vector更改为如下所示的错误:

byte[] iv = new byte[]{};
我收到错误信息:

java.security.InvalidAlgorithmParameterException: expected IV length of 16

对于CBC模式,您应该调用

 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
NoPadding
选项意味着不应用填充。这是有用的,如果

  • 您的数据始终是AES块大小的倍数,即128k
  • 你会做你的填充,可能会开发一个新的
如果你说的是ECB模式,那就不需要IV,也不用ECB。这是不安全的。如果你真的需要,那就不用静脉注射了

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

如果你的Android目标匹配,你会选择GCM模式,而不是ECB或CBC模式。这是一种现代的加密方式;经过身份验证的加密(与相关数据)。您将获得机密性、身份验证和完整性。

按照建议进行了更改,但现在抛出了以下错误:javax.crypto.BadPaddingException:EVP_CipherFinal_exAlso我的数据将始终是128位的,128位密钥和IVencyption由基于NFC的读卡器完成(我使用它进行卡验证)。我将收到带密钥的16字节加密数据。根据文件“使用AES 128 CBC加密/解密,IV值为00h”,我已为文件签署NDA。我无法共享链接或其副本。但它没有说nopadding或AES/CBC/nopadding。我之前发布的是doc提到的全文。
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);