java.security.InvalidKeyException:算法错误:需要 DESede 或 TripleDES
java.security.InvalidKeyException: Wrong algorithm: DESede or TripleDES required
这段代码是我写的,我的key是:"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)
DESedeCrypt
class 仅支持 DESede
和 TripleDES
算法,根据源代码:
void init(boolean decrypting, String algorithm, byte[] keys)
throws InvalidKeyException {
if (!algorithm.equalsIgnoreCase("DESede")
&& !algorithm.equalsIgnoreCase("TripleDES")) {
throw new InvalidKeyException
("Wrong algorithm: DESede or TripleDES required");
}
您的代码必须使用 SecretKey
中的两者之一。 Cipher
可以保持为 DESede/ECB/PKCS5Padding
:
String workingKey = "ooWqEPcw7KR/h/JIbrFCRHiEVaybvnB2";
SecretKey secretKey = new SecretKeySpec(
Base64.getDecoder().decode(workingKey),
"DESede");
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(1, secretKey);
你应该把 SecretKeySpec 写成
SecretKey secretKey=new SecretKeySpec(base64Decoder.decode(workingKey),"DESede");
也使用 Cipher.ENCRYPT_MODE
和 Cipher.DECRYPT_MODE
代替常量。
这段代码是我写的,我的key是:"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)
DESedeCrypt
class 仅支持 DESede
和 TripleDES
算法,根据源代码:
void init(boolean decrypting, String algorithm, byte[] keys)
throws InvalidKeyException {
if (!algorithm.equalsIgnoreCase("DESede")
&& !algorithm.equalsIgnoreCase("TripleDES")) {
throw new InvalidKeyException
("Wrong algorithm: DESede or TripleDES required");
}
您的代码必须使用 SecretKey
中的两者之一。 Cipher
可以保持为 DESede/ECB/PKCS5Padding
:
String workingKey = "ooWqEPcw7KR/h/JIbrFCRHiEVaybvnB2";
SecretKey secretKey = new SecretKeySpec(
Base64.getDecoder().decode(workingKey),
"DESede");
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(1, secretKey);
你应该把 SecretKeySpec 写成
SecretKey secretKey=new SecretKeySpec(base64Decoder.decode(workingKey),"DESede");
也使用 Cipher.ENCRYPT_MODE
和 Cipher.DECRYPT_MODE
代替常量。