2015-06-21 74 views
2

下面的代碼在編譯時給了我下面的錯誤。編譯時出錯:無法實例化類

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 

請問有人告訴我什麼是錯的? 此外,在小程序窗口中出現消息:「啓動:小程序未初始化」。 預先感謝您。

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); 
     } 
    } 
} 

回答

2

<init>

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

是不是在談論所謂的init實際的方法(這將是RSA_plus.init),它是在談論一個構造不帶參數。

Applets必須實現無參數構造函數。首先構建小程序,然後然後調用它的Applet#init方法。

+0

我已刪除從構造函數參數: '公共RSA_plus(){' \t'RSA =新RSA_plus();'' = bitlen 1024;' ,但現在它給了我另一個錯誤。 'java.lang.StackOverflowError的 \t在java.security.AccessControlContext.optimize(未知來源) \t在java.security.AccessController.getContext(未知來源) \t在java.awt.Component中。 (Unknown Source) \t at java.awt.Container。 (Unknown Source) \t at java.awt.Panel。 (Unknown Source) \t at java.awt.Panel。 (Unknown Source) \t at java.applet.Applet。 (未知來源) \t在RSA_plus。 (RSA_plus.java:52)' –

+0

@AdiS .:那將是一個不同的問題。 –

+0

在T.J .:你能否建議我應該問題的標題? –

相關問題