请教DES算法的解密
如下解密方法:
但问题是:加密后的数据作为字符串保存后,重新被读出时其长度不为8的倍数,通过getBytes()方法转为数组后不能被正确解密,问该如何正确转换?
public byte[] decrypt(byte[] decStr,SecretKey key){
byte[] decedStr=null;
Cipher cipher = null;
try{
String algo = key.getAlgorithm();
cipher = Cipher.getInstance(algo);
cipher.init(Cipher.DECRYPT_MODE, key);
decedStr=cipher.doFinal(decStr);
}
catch (Exception e) {
log.error("解密出错!");
log.error(gse.getMessage());
gse.printStackTrace();
}
return decedStr;
}
报错如下:
Input length must be multiple of 8 when decrypting with padded cipher
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
关键是上面的部分
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at com.zl.security.EncDecrypt.decrypt(EncDecrypt.java:172)
at com.zl.security.EncDecryptTest.test(EncDecryptTest.java:133)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
问题点数:20、回复次数:10Top
1 楼zhou7707(糊糊)回复于 2005-09-22 16:40:57 得分 0
自己顶一下Top
2 楼hwman(药师)回复于 2005-09-22 20:08:34 得分 0
DES加密后的数据长度必然是8的倍数。说明了你加密或者保存的过程出错了Top
3 楼xuqian_2004(许谦)回复于 2005-09-24 16:11:17 得分 0
你用的是jdk几呀?1.4还是1.5?
Top
4 楼lanchong512(懒虫)回复于 2005-09-25 20:27:59 得分 0
重新被读出时其长度不为8的倍数
可不可以补全为8的倍数?
如果还不行就解密失败了。Top
5 楼zhou7707(糊糊)回复于 2005-09-26 13:36:07 得分 0
我用的JDK1.5,不过应该是按照1.4.2编译的,我也想是要补全为8的倍数,不过不知道该怎么补,请指教。加密的方法应该是没错的,加密得到的byte数组直接用该方法解密,可以得到原始值,但将该byte[]转为字符串,再用getBytes()方法转回byte[],则出现如上的解密错误Top
6 楼zhou7707(糊糊)回复于 2005-10-10 18:59:30 得分 0
不要沉下去了,自己顶一下Top
7 楼lanchong512(懒虫)回复于 2005-10-11 23:23:31 得分 0
自己去找吧(DES),论坛里好多的现成的完整程序。Top
8 楼zhou7707(糊糊)回复于 2005-10-12 12:12:37 得分 0
没有一个是转换为字符串后再截密的,加密后的字节数组直接解密,没有问题。Top
9 楼zhou7707(糊糊)回复于 2005-10-18 14:13:52 得分 0
有没有人帮忙呀,高手都哪去了?Top
10 楼cathy97(男人练瑜伽好处多)回复于 2005-10-19 11:05:00 得分 20
如果你的DES加密,使用的操作模式为PKCS5Padding, 那么加密后的密文必然为8的整数倍,但是你把密文转换成String后,原来的byte被转换为了char,也就是说中间增加了新的byte,这样就破坏了密文的原始结构,String.getBytes()返回的将不再是原始的密文,大小应该是原始密文的2倍。
比如:
byte[] desdata1 = {@,#,$,%,2,3,4,5}
String des = "@#$%2345"
byte[] des2 = des.getBytes();
des2 = {0,@,0,#,0,%,0,2,0,3,0,4,0,5}
Top




