Error at compilation: class cannot be instantiated

519 views Asked by At

The bellow code gives me the following error at compilation.

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

Could please somebody tell me what is wrong? Also, in the applet window appears the message that: "Start: applet not initialized". Thank you in advance.

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

There are 1 answers

3
T.J. Crowder On BEST ANSWER

The <init> in

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

isn't talking about an actual method called init (that would be RSA_plus.init), it's talking about a constructor that takes no arguments.

Applets must implement a no-arguments constructor. First the applet is constructed, and then its Applet#init method is called.