怎么在WindowsBulider中的TextArea中写入一个从Socket流过来的byte[]

即食海苔 2014-10-10 01:16:26
package Caesar_Modification;

import java.awt.EventQueue;
import java.awt.TextArea;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JFrame;

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MyServer {

private JFrame frame;
private static ServerSocket server_text1;
private static Socket you_text1;
private static ServerSocket server_text2;
private static Socket you_text2;
private static ServerSocket server_key;
private static Socket you_key;
private static TextArea SDealWith_Before1;
private static TextArea SDealWith_Before2;
private static TextArea SDealWith_After;
private static byte[]keybyte;


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyServer window = new MyServer();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


public MyServer() throws IOException {
initialize();

}

private void initialize() throws IOException {
frame = new JFrame();
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.setTitle("服务器端");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

final TextArea SDealWith_Before1 = new TextArea();
SDealWith_Before1.setBounds(10, 78, 156, 50);
frame.getContentPane().add(SDealWith_Before1);

final TextArea SDealWith_After = new TextArea();
SDealWith_After.setBounds(227, 103, 150, 115);
frame.getContentPane().add(SDealWith_After);

JLabel lblNewLabel = new JLabel("接收到的秘钥");
lblNewLabel.setBounds(227, 10, 93, 15);
frame.getContentPane().add(lblNewLabel);

JLabel label_1 = new JLabel("处理后");
label_1.setBounds(227, 78, 54, 15);
frame.getContentPane().add(label_1);

JButton button_1 = new JButton("清除");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SDealWith_After.setText("");
SDealWith_Before1.setText("");
SDealWith_Before2.setText("");
}
});
button_1.setBounds(269, 228, 93, 23);
frame.getContentPane().add(button_1);

final TextArea Pri_KeyS = new TextArea();
Pri_KeyS.setBounds(227, 27, 150, 43);
frame.getContentPane().add(Pri_KeyS);

final TextArea SDealWith_Before2 = new TextArea();
SDealWith_Before2.setBounds(10, 159, 156, 59);
frame.getContentPane().add(SDealWith_Before2);

JButton btnNewButton_1 = new JButton("开启秘钥通道");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
server_key = new ServerSocket(5679);
you_key = server_key.accept();
InputStream is = you_key.getInputStream();

try {
int total = is.available();
keybyte = new byte[total];
Pri_KeyS.setText(keybyte.toString());
server_key.close();
you_key.close();

} catch (IOException e1) {
e1.printStackTrace();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
btnNewButton_1.setBounds(10, 6, 150, 23);
frame.getContentPane().add(btnNewButton_1);

JButton btnNewButton = new JButton("解密");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
InputStream in_1 = null;
InputStream in_2 = null;
try {
in_1 = you_text1.getInputStream();
in_2 = you_text2.getInputStream();
int total_ptext = in_1.available();
int total_ctext = in_2.available();
byte[]ctext = new byte[total_ctext];
byte[]ptext = new byte[total_ptext];

SecretKeySpec k = new SecretKeySpec(keybyte,"DESede");
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);

byte []text = cp.doFinal(ctext);
String p = new String(text,"UTF8");

SDealWith_Before1.setText(ptext.toString());
SDealWith_Before2.setText(ctext.toString());
SDealWith_After.setText(p);
} catch (IOException e2) {
e2.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchPaddingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidKeyException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalBlockSizeException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (BadPaddingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
you_text1.close();
server_text1.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(111, 228, 93, 23);
frame.getContentPane().add(btnNewButton);

JButton btnNewButton_2 = new JButton("开启密文通道");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
server_text1 = new ServerSocket(5678);
you_text1 = server_text1.accept();
server_text2 = new ServerSocket(5677);
you_text2 = server_text2.accept();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
btnNewButton_2.setBounds(10, 39, 150, 23);
frame.getContentPane().add(btnNewButton_2);

JLabel lblNewLabel_2 = new JLabel("输入UTF8字节");
lblNewLabel_2.setBounds(20, 61, 72, 23);
frame.getContentPane().add(lblNewLabel_2);

JLabel lbldes = new JLabel("通过DES加密后");
lbldes.setBounds(20, 134, 93, 23);
frame.getContentPane().add(lbldes);

}
}



运行时总是显示空指针异常,秘钥没有传到这个客户端里。。

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Empty key
at javax.crypto.spec.SecretKeySpec.<init>(SecretKeySpec.java:96)
at Caesar_Modification.MyServer$4.actionPerformed(MyServer.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

...全文
76 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧