java.security.InvalidKeyException:没有安装的提供程序支持此密钥:com.sun.crypto.provider.DESKey

时间:2014-03-24 06:32:15

标签: java encryption

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;

}

}

我收到如下错误

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)

1 个答案:

答案 0 :(得分:1)

正如Oleg在评论中提到的那样,你创建了一个错误类型的Cipher对象。尝试更改:

Cipher cipher = Cipher.getInstance("RSA");

Cipher cipher = Cipher.getInstance("DES");

我强烈建议您指定模式和填充,以避免依赖提供程序默认值。