我写了这段代码,我的密钥是:“ ooWqEPcw7KR / h / JIbrFCRHiEVaybvnB2”。
try
{
Base64Decoder base64Decoder=new Base64Decoder();
String encryptType="DESede/ECB/PKCS5Padding";
String workingKey="ooWqEPcw7KR/h/JIbrFCRHiEVaybvnB2";
SecretKey secretKey=new SecretKeySpec(base64Decoder.decode(workingKey), encryptType);
Cipher cipher=Cipher.getInstance(encryptType);
cipher.init(1, secretKey);
}
catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e)
{
e.printStackTrace();
}
但是我得到这个错误!
java.security.InvalidKeyException: Wrong algorithm: DESede or TripleDES required
at com.sun.crypto.provider.DESedeCrypt.init(DESedeCrypt.java:65)
at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:93)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:582)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:458)
at com.sun.crypto.provider.DESedeCipher.engineInit(DESedeCipher.java:166)
at javax.crypto.Cipher.implInit(Cipher.java:802)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1249)
at javax.crypto.Cipher.init(Cipher.java:1186)
at EncryptText.main(EncryptText.java:24)
最佳答案
您应该将SecretKeySpec编写为
SecretKey secretKey=new SecretKeySpec(base64Decoder.decode(workingKey),"DESede");
还要使用
Cipher.ENCRYPT_MODE
和Cipher.DECRYPT_MODE
而不是常量。