怎么在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)

...全文
138 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
源码直接下载地址: https://pan.quark.cn/s/854bd57df23a 在金融行业中,房产购置贷款是众多人在购置不动产时必须进行的一项财务规划。Matlab作为一种功能强大的数学及数据分析平台,经常被应用于各类复杂的运算,其中包括住房贷款的计算。本文将详细研究运用Matlab开发住房贷款计算程序的相关知识要点,以协助读者掌握如何借助编程技术处理实际的信贷问题。 1. **信贷种类** - **商业信贷**:此类贷款通常由金融机构提供,其利率会随市场行情变化,借款人可选用固定利率或浮动利率方案。 - **公积金信贷**:该类贷款获得政府机构的支持,利率一般低于商业信贷,但设有最高贷款金额的限制。 2. **偿付模式** - **等额本金偿付**:每月支付相同数量的本金,利息部分逐月减少,因此整体还款总额会逐步降低。 - **等额本息偿付**:每月支付固定金额的本金与利息组合,利息部分随本金的逐月减少而递减。 3. **住房贷款计算公式** - **等额本息偿付**:每月还款额=贷款本金×[月利率×(1+月利率)^(贷款期数)]÷[(1+月利率)^(贷款期数)-1]。 - **等额本金偿付**:每月应还本金=贷款总额/贷款期数,每月利息=(贷款余额-累计已还本金)×月利率。 4. **Matlab编程基础** - **变量设定**:在Matlab环境中,首要任务是设定贷款数额、年化利率、贷款时长等核心参数。 - **循环控制结构**:借助for循环来模拟每个还款周期的过程,进行本金与利息的计算。 - **函数构建**:将住房贷款的计算逻辑封装为函数,便于重复使用和测试不同条件下的结果。 5. **Matlab代码编写** - **等额本息函数实现**...

62,620

社区成员

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

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