IvParameterSpec 干什么用的

               

转载自:http://www.chinajavaworld.com/thread.jspa?messageID=673776

请教:
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);

Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
两种有什么区别?


我用不带"ivParameterSpec"参数的cipher能够顺利加解密,而当我使用了ivParameterSpec参数后代码可以编译通过,但是运行时抛出异常:

java.security.InvalidAlgorithmParameterException: ECB mode cannot use IV
java.security.InvalidAlgorithmParameterException: ECB mode cannot use IV
at com.sun.crypto.provider.SunJCE_h.a(DashoA12275)
at com.sun.crypto.provider.DESedeCipher.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)
at jiangyifen.util.cryptography.SNgenerator.encrypt(SNgenerator.java:140)
at jiangyifen.util.cryptography.SNgenerator.main(SNgenerator.java:62)

这是何故?请有经验的兄弟赐教。原代码如下:

略。。。。。。。。。。

重点看原文链接~


带iv的应该像下面这么写~

bytes[] iv = {2,5,2,6,3,6,7,2};

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

           

猜你喜欢

转载自blog.csdn.net/qq_44894420/article/details/89203683