PedroFaria
PedroFaria

Reputation: 51

KeyPairGenerator cannot be resolved

I'm trying to create a little message system using RSA to encrypt the messages, but I'm stuck right in the beginning because for some reason, the code doesn't recognize KeyPairGenerator class. My code so far, as following many examples on the Internet, is:

public class RSA {
    private KeyPairGenerator key_par_gen = null;
    private KeyPair kp = null;
    private PublicKey publicKey = null;
    private PrivateKey privateKey = null;
    private KeyFactory factory_rsa = null;
    private RSAPublicKeySpec pub = null;
    private RSAPrivateKeySpec priv = null;

    public RSA(){       
        setKey_par_gen(); 
        setKp(); //No error here
    }

    public void setKey_par_gen() {
        this.key_par_gen = new KeyPairGenerator.getInstance("RSA");
        //Error: Description    Resource    Path    Location    Type
        //       KeyPairGenerator.getInstance cannot be resolved to a type  RSA.java    /RSA - Cloud/src    line 41 Java Problem

    }

    public void setKp() {
        this.kp = getKey_par_gen().genKeyPair();
    }
//....
}

I already updated to the latest java version, opened the KeyPairGenerator declaration and it's there, with the declared function and all. Also can't be from the IDE, since I tried on Intellij, Eclipse and Netbeans. Don't know what I'm missing here. Any help is appreciated.

Upvotes: 2

Views: 1645

Answers (1)

Andreas Fester
Andreas Fester

Reputation: 36630

You are trying to instanciate a nested class KeyPairGenerator.getInstance which does not exist:

this.key_par_gen = new KeyPairGenerator.getInstance("RSA");
// Error: KeyPairGenerator.getInstance cannot be resolved to a type

Instead, you need to call the static getInstance() method of KeyPairGenerator - simply remove the new:

this.key_par_gen = KeyPairGenerator.getInstance("RSA");

Upvotes: 2

Related Questions