编译时出错:无法实例化类

时间:2015-06-21 14:23:19

标签: java instantiation

波纹管代码在编译时给出了以下错误。

load: RSA_plus.class can't be instantiated.
java.lang.InstantiationException: RSA_plus
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: RSA_plus.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 5 more

可以请有人告诉我有什么问题吗? 此外,在小程序窗口中出现以下消息:&#34;开始:小程序未初始化&#34;。 提前谢谢。

import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import java.security.SecureRandom;

public class RSA_plus extends Applet implements ActionListener {
    private static final long serialVersionUID = 1;

    private BigInteger n, d, e;

    private int bitlen = 1024;

    RSA_plus rsa;

    Button okButton;
    TextField nameField;
    TextField name2Field;

    public void init()
    {
    setLayout(new FlowLayout());
    okButton = new Button("Criptare");
    nameField = new TextField(" ",20);
    name2Field = new TextField("",20);

    add(nameField);
    add(okButton);
    add(name2Field);

    okButton.addActionListener(this);
    }

public void paint(Graphics g)
{
    g.drawString(nameField.getText(),20,100);
    g.drawString(name2Field.getText(),20,100);
}

/* Create an instance that can encrypt using someone else's public key. */
public RSA_plus(BigInteger newn, BigInteger newe) {
    n = newn;
    e = newe;
}

/* Create an instance that can both encrypt and decrypt. */
public RSA_plus(int bits) {
    rsa = new RSA_plus(1024);
    bitlen = bits;
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);
    BigInteger m=(p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
          e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
 }

 /* Encrypt the given plaintext message. */
 public synchronized String encrypt(String message) {
    return (new BigInteger(message.getBytes())).modPow(e, n).toString();
 }

 /* Encrypt the given plaintext message. */
 public synchronized BigInteger encrypt(BigInteger message) {
 return message.modPow(e, n);
 }

 /* Decrypt the given ciphertext message. */
 public synchronized String decrypt(String message) {
    return new String((new BigInteger(message)).modPow(d, n).toByteArray());
 }

 /* Decrypt the given ciphertext message. */
 public synchronized BigInteger decrypt(BigInteger message) {
     return message.modPow(d, n);
 }

 /* Generate a new public and private key set. */
 public synchronized void generateKeys() {
     SecureRandom r = new SecureRandom();
     BigInteger p = new BigInteger(bitlen / 2, 100, r);
     BigInteger q = new BigInteger(bitlen / 2, 100, r);
     n = p.multiply(q);
     BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
     e = new BigInteger("3");
     while (m.gcd(e).intValue() > 1) {
        e = e.add(new BigInteger("2"));
     }
     d = e.modInverse(m);
 }

 /* Return the modulus. */
 public synchronized BigInteger getN() {
     return n;
 }

 /* Return the public key. */
 public synchronized BigInteger getE() {
     return e;
 }

 public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okButton) {

            String message = encrypt(nameField.getText());
            System.out.print(message+" ");
            BigInteger plaintext = new BigInteger(message.getBytes());

            String text2 = new String(plaintext.toByteArray());
            System.out.println("Plaintext: " + text2);
            name2Field.setText(text2);

            BigInteger ciphertext = rsa.encrypt(plaintext);
            System.out.println("Ciphertext: " + ciphertext);
            String result = (ciphertext.toString());
            name2Field.setText(result);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

中的<init>

Caused by: java.lang.NoSuchMethodException: RSA_plus.<init>()

不是在谈论一个名为init的实际方法(即RSA_plus.init),而是在谈论一个不带参数的构造函数

Applet必须实现无参数构造函数。首先构建applet,然后调用然后它的Applet#init方法。