我有点问题。我试图在java中建立简单的RSA聊天。但我不知道我的代码中有什么正确可以避免问题。当我运行chat_Server.java,然后chat_Client.java并发送文本冻结,没有响应。当我只运行chat_Server.java并发送某个时,然后:
(chat_Serwer.java:92) ois = new ObjectInputStream(s.getInputStream());
(chat_Serwer.java:13)公共类chat_Serwer扩展javax.swing.JFrame {
GUI中的(chat_Serwer.java:53) msg_sendActionPerformed(evt); :initComponents()
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at chat.chat_Serwer.msg_sendActionPerformed(chat_Serwer.java:92)
at chat.chat_Serwer.access$100(chat_Serwer.java:13)
at chat.chat_Serwer$2.actionPerformed(chat_Serwer.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我乞求帮助。
声明:
public static int BIT_LENGTH = 1024;
static ServerSocket ss;
static Socket s;
static ObjectInputStream ois;
static ObjectOutputStream oos;
对Server.java中按钮的操作:
private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {
try{
ois = new ObjectInputStream(s.getInputStream());
oos = new ObjectOutputStream(s.getOutputStream());
//reading
Message mess = (Message) ois.readObject();
BigInteger d = mess.d;
BigInteger n = mess.n;
BigInteger e = mess.e;
System.out.println("d: "+d);
System.out.println("n: "+n);
System.out.println("e: "+e);
String msgout = "";
msgout = msg_text.getText().trim();
// conversion String to BigInteger
String txt = msgout;
byte[] msBytes = txt.getBytes();
BigInteger msg = new BigInteger(msBytes);
System.out.println(msg);
// Encryption
BigInteger enc = msg.modPow(e, n);
// sending
Message mes = new Message();
mes.enc = enc;
oos.writeObject(mes);
oos.flush();
}catch(IOException ex){
System.out.println("w przycisku: "+ex.getMessage());
} catch (ClassNotFoundException ex) {
Logger.getLogger(chat_Serwer.class.getName()).log(Level.SEVERE, null, ex);
}
}
主要在Server.java
// Prime numbers
Random rand = new SecureRandom();
BigInteger p = new BigInteger(BIT_LENGTH / 2, 100, rand);
BigInteger q = new BigInteger(BIT_LENGTH / 2, 100, rand);
BigInteger n = p.multiply(q);
BigInteger phi = p.subtract(BigInteger.ONE)
.multiply(q.subtract(BigInteger.ONE));
BigInteger e;
do e = new BigInteger(phi.bitLength(), rand);
while (e.compareTo(BigInteger.ONE) <= 0
|| e.compareTo(phi) >= 0
|| !e.gcd(phi).equals(BigInteger.ONE));
BigInteger d = e.modInverse(phi);
try{
ss = new ServerSocket(8080); //choose port
s = ss.accept();
//sending
Message mes = new Message();
mes.d = d;
mes.n = n;
mes.e = e;
oos.writeObject(mes);
oos.flush();
//reading
Message mess = (Message) ois.readObject();
BigInteger enc = mess.enc;
//decryption
BigInteger dec = enc.modPow(d, n);
String wejscie = new String(dec.toByteArray());
msg_area.setText(msg_area.getText().trim()+"\n Serwer: "+wejscie); //shows message
}catch( IOException z){
System.out.println("w main: "+z);
} catch (ClassNotFoundException ex) {
Logger.getLogger(chat_Serwer.class.getName()).log(Level.SEVERE, null, ex);
}
Message.java
package chat;
import java.io.Serializable;
import java.math.BigInteger;
public class Message implements Serializable {
public BigInteger e;
public BigInteger d;
public BigInteger n;
public BigInteger enc;
public BigInteger dec;
}
Client.java几乎是一样的。我只是没有向Message.java发送e,d,n,而是阅读它们。