编译时出错:class 无法实例化
Error at compilation: class cannot be instantiated
以下代码在编译时出现以下错误。
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
有人能告诉我哪里出了问题吗?
此外,在小程序 window 中出现消息:"Start: applet not initialized"。
提前谢谢你。
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);
}
}
}
<init>
中的
Caused by: java.lang.NoSuchMethodException: RSA_plus.<init>()
不是在谈论一个名为 init
的实际方法(即 RSA_plus.init
),而是在谈论一个不带参数的 构造函数 。
小程序必须实现无参数构造函数。首先构造小程序,然后它的Applet#init
方法被调用。
以下代码在编译时出现以下错误。
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
有人能告诉我哪里出了问题吗? 此外,在小程序 window 中出现消息:"Start: applet not initialized"。 提前谢谢你。
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);
}
}
}
<init>
中的
Caused by: java.lang.NoSuchMethodException: RSA_plus.<init>()
不是在谈论一个名为 init
的实际方法(即 RSA_plus.init
),而是在谈论一个不带参数的 构造函数 。
小程序必须实现无参数构造函数。首先构造小程序,然后它的Applet#init
方法被调用。