java.security.InvalidKeyException:没有已安装的提供程序支持此密钥:com.sun.crypto.provider.DESKey
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22602496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:46:21 来源:igfitidea点击:
java.security.InvalidKeyException: No installed provider supports this key: com.sun.crypto.provider.DESKey
提问by user3391115
import java.security.*;
import java.security.spec.InvalidParameterSpecException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.*;
import java.util.*;
import javax.crypto.spec.DHParameterSpec;
public class Encrypting{
public static void main(String[] args) {
try {
KeyPair alice_key;
KeyPair bob_key ;
String plaintext="hi how r u";
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator
.getInstance("DH");
paramGen.init(1024);
AlgorithmParameters params = paramGen.generateParameters();
DHParameterSpec dhSpec = (DHParameterSpec) params
.getParameterSpec(DHParameterSpec.class);
keyGen.initialize(dhSpec);
alice_key = keyGen.generateKeyPair();
bob_key = keyGen.generateKeyPair();
Cipher cipher = Cipher.getInstance("RSA");
SecretKey secret_alice = combine(alice_key.getPrivate(), bob_key.getPublic());
cipher.init(Cipher.ENCRYPT_MODE, secret_alice);
byte[] x = cipher.doFinal(plaintext.getBytes());
System.out.println("encrypted message");
for(int i=0;i<x.length;i++)
System.out.print((char)x[i]);
cipher.init(Cipher.DECRYPT_MODE, secret_alice);
byte[] y = cipher.doFinal(x);
System.out.println();
System.out.println("decrypted message");
for(int i=0;i<y.length;i++)
System.out.print((char)y[i]);
SecretKey secret_bob = combine(bob_key.getPrivate(),
alice_key.getPublic());
System.out.println(Arrays.toString(secret_alice.getEncoded()));
System.out.println(Arrays.toString(secret_bob.getEncoded()));
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidParameterSpecException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static SecretKey combine(PrivateKey private1, PublicKey public1) {
SecretKey secretKey=null;
try {
KeyAgreement ka = KeyAgreement.getInstance("DH");
ka.init(private1);
ka.doPhase(public1, true);
secretKey = ka.generateSecret("DES");
return secretKey;
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
return secretKey;
}
}
I got an error as follows
我收到如下错误
java.security.InvalidKeyException: No installed provider supports this key: com.sun.crypto.provider.DESKey
at javax.crypto.Cipher.chooseProvider(Cipher.java:878)
at javax.crypto.Cipher.init(Cipher.java:1213)
at javax.crypto.Cipher.init(Cipher.java:1153)
at NewClass.main(Encrypting.java:33)
回答by Duncan Jones
As Oleg mentions in the comments, you have created a Cipher
object of the wrong type. Try changing:
正如 Oleg 在评论中提到的,您创建了一个Cipher
错误类型的对象。尝试改变:
Cipher cipher = Cipher.getInstance("RSA");
to
到
Cipher cipher = Cipher.getInstance("DES");
I would strongly recommend you specify a mode and padding as well, to avoid relying on provider defaults.
我强烈建议您也指定一个模式和填充,以避免依赖提供程序的默认值。