请教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)