无法解析 KeyPairGenerator

KeyPairGenerator cannot be resolved

我正在尝试创建一个使用 RSA 来加密消息的小型消息系统,但由于某种原因,代码无法识别 KeyPairGenerator class,所以我一开始就卡住了。 到目前为止,我的代码是:

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();
    }
//....
}

我已经更新到最新的 java 版本,打开 KeyPairGenerator 声明,它就在那里,包含声明的函数和所有内容。 也不能来自 IDE,因为我试过 Intellij、Eclipse 和 Netbeans。 不知道我在这里错过了什么。感谢任何帮助。

您正在尝试实例化不存在的嵌套 class KeyPairGenerator.getInstance:

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

相反,您需要调用 KeyPairGenerator 的静态 getInstance() 方法 - 只需删除 new:

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