我正在做RSA聊天Messenger(单个服务器,多个客户端)。我包括了send,decrypt,exit按钮。当客户端将消息从一个客户端发送到另一客户端时,接收消息的客户端以加密形式获取它,然后单击解密按钮以获得原始消息。但是,当我单击解密按钮时,我得到AWT-EventQueue-0“ java.lang.NumberFormatException并在解密部分得到了奇怪的字符。

这是我的代码:

private String bytesToString(byte[] encrypted) {
    // TODO Auto-generated method stub
    //return null;
    String test = "";
    for (byte b : encrypted) {
        test += Byte.toString(b);
    }
    return test;
}

public static void main(String ... args) {

    // take username from user
    String name = JOptionPane.showInputDialog(null,"Enter your name :", "Username",
         JOptionPane.PLAIN_MESSAGE);
    String servername = "localhost";  
    try {
        new ChatClient( name ,servername);
    } catch(Exception ex) {
        out.println( "Error --> " + ex.getMessage());
    }

} // end of main

// inner class for Messages Thread
class  MessagesThread extends Thread {
    public void run() {
        String line;
        try {
            while(true) {
                line = br.readLine();
                taMessages.append(line + "\n");
            } // end of while
        } catch(Exception ex) {}
    }
}
public class RSA {

    private BigInteger p;
    private BigInteger q;
    private BigInteger N;
    private BigInteger phi;
    private BigInteger e;
    private BigInteger d;
    private int bitlength = 1024;
    private int blocksize = 256; //blocksize in byte

    private Random r;
     public RSA() {
        r = new Random();
        p = BigInteger.probablePrime(bitlength, r);
        q = BigInteger.probablePrime(bitlength, r);
        N = p.multiply(q);

        phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
        e = BigInteger.probablePrime(bitlength/2, r);

        while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0 ) {
            e.add(BigInteger.ONE);
        }
 d = e.modInverse(phi); 
    }

    public RSA(BigInteger e, BigInteger d, BigInteger N) {
        this.e = e;
        this.d = d;
        this.N = N;
    } 
    private String bytesToString(byte[] encrypted) {
        String test = "";
        for (byte b : encrypted) {
            test += Byte.toString(b);
        }
        return test;
    }

 //Encrypt message
     public byte[] encrypt(byte[] message) {     
        return (new BigInteger(message)).modPow(e, N).toByteArray();
    }

// Decrypt message
    public byte[] decrypt(byte[] message) {
        return (new BigInteger(message)).modPow(d, N).toByteArray();
    } 
}

class Eavesdropper implements ActionListener {
    JTextArea myTextArea;
            public Eavesdropper(JTextArea ta) {
        myTextArea = ta;
    }
       public void actionPerformed(ActionEvent e) {
           RSA rsa = new RSA(); 
        //      String teststring = tfInput.getText();
      //    byte[] encrypted = rsa.encrypt(teststring.getBytes());  
        String text1 = taMessages.getText();
        String[] parts =text1.split("-");
           String part1=parts[1];
        part1 =part1.replaceAll("\n", "");
        byte[] b = part1.getBytes();
        byte[] decrypted = rsa.decrypt(b);       
        //RSAEncryption rsa=new RSAEncryption(1024);
    //  BigInteger plaintext = new BigInteger(part1.getBytes());
      //    BigInteger ciphertext = rsa.encrypt(plaintext);
        //String plaintext3 = rsa.decrypt(part1);
        //String text2 = new String(plaintext3.toByteArray());

              //    myTextArea.append(plaintext3);
        myTextArea.append("Decrypted String:" + new String(decrypted));
    }
}



} //  end of client

===============>>#1 票数:0

根据文档

Throws: NumberFormatException - val is zero bytes long.

http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#BigInteger(字节[])

  ask by user3552965 translate from so

未解决问题?本站智能推荐: