DES加密时正常,解密时出错了:
java.security.InvalidKeyException: Parameters missing at com.sun.crypto.provider.CipherCore.init(CipherCore.java:469) at com.sun.crypto.provider.DESCipher.engineInit(DESCipher.java:186) at javax.crypto.Cipher.implInit(Cipher.java:805) at javax.crypto.Cipher.chooseProvider(Cipher.java:867) at javax.crypto.Cipher.init(Cipher.java:1252) at javax.crypto.Cipher.init(Cipher.java:1189)
代码如下:
public static void decrypt() { Cipher cipher = decryptCipher = Cipher.getInstance(CIPHER_ALGORITHM); Key key = CipherDES.generateKey(CdkeyGenerator.PASSWORD); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedBytes = cipher.doFinal(validateBytes); }
解决办法:
错误的参数:String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; 正确的参数:String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";