📄 jclient.txt
字号:
package com.JavaSeries.Java;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.security.*;
import java.net.*;
import javax.swing.event.*;
import javax.swing.JTextArea;
import java.io.*;
import java.util.*;
import java.security.interfaces.*;
import java.security.spec.*;
import java.lang.Integer;
public class JClient extends JFrame implements ActionListener
{
private JButton JBSign,JBSendMes,JBSendPubK,JBEdit,JBSetStrength,JBSetIp; //parameter
private JPanel JPSetPara=new JPanel();
private JPanel JPShowPara=new JPanel();
private JPanel JPShowMes=new JPanel();
private JPanel JPButtons=new JPanel();
private JLabel JLShowPara=new JLabel("设置栏:");
private JLabel JLShowMes=new JLabel("消息编辑框:");
private JTextArea JTAShowMes=new JTextArea(5,20);
private JTextField JTFSetStrength,JTFIpA,JTFIpB,JTFIpC,JTFIpD;
private int n,a,b,c,d;
private InetAddress addr;
Socket ClientSoc=null;
////////////////////////********************///////////////////////////////
//建立框架
JClient()
{
JOptionPane.showMessageDialog(null,"欢迎访问具有DSS签名功能的客户端!","客户端",JOptionPane.INFORMATION_MESSAGE);
setTitle("客户端");
setSize(500,350);
setLocation(50,50);
JPShowMes.setLayout(new BorderLayout());
JPShowMes.add(JLShowMes,BorderLayout.NORTH);
JPShowMes.add(JTAShowMes,BorderLayout.CENTER);
JPButtons.setLayout(new FlowLayout());
JPButtons.add(JBSign=new JButton("签名"));
JPButtons.add(JBSendPubK=new JButton("发送公钥"));
JPButtons.add(JBSendMes=new JButton("发送消息"));
JPButtons.add(JBEdit=new JButton("编辑"));
JPSetPara.setLayout(new BorderLayout());
JPSetPara.add(JLShowPara,BorderLayout.NORTH);
JPSetPara.add(JPShowPara,BorderLayout.CENTER);
JPShowPara.setLayout(new FlowLayout());
JPShowPara.add(JBSetStrength=new JButton("强度参数:"));
JPShowPara.add(JTFSetStrength=new JTextField(4));
JPShowPara.add(JBSetIp=new JButton("接收方IP:"));
JPShowPara.add(JTFIpA=new JTextField(3));
JPShowPara.add(JTFIpB=new JTextField(3));
JPShowPara.add(JTFIpC=new JTextField(3));
JPShowPara.add(JTFIpD=new JTextField(3));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(JPSetPara,BorderLayout.NORTH);
getContentPane().add(JPShowMes,BorderLayout.CENTER);
getContentPane().add(JPButtons,BorderLayout.SOUTH);
JBSign.addActionListener(this);
JBSendMes.addActionListener(this);
JBSendPubK.addActionListener(this);
JBEdit.addActionListener(this);
JBSetStrength.addActionListener(this);
JBSetIp.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
/////////////////////////////////////////////////////////////
//二行制转字符串
public String byte2hex(byte[] b)
{
String hs="";
String stmp="";
for (int n=0;n<b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
//////////////////////////////////////////////////////////////
//响应按钮事件
public void actionPerformed(ActionEvent e){
//设置DSS强度参数************************
if(e.getSource()==JBSetStrength){
int Value;
String ValueStr=JTFSetStrength.getText();
Value=Integer.parseInt(ValueStr);
try
{
if(Value%64==0)
{
n=Value;
System.out.println("强度参数设置为:"+n);
JOptionPane.showMessageDialog(null,"强度参数值为:"+Value,"参数设置成功",JOptionPane.INFORMATION_MESSAGE);
} else
{
JOptionPane.showMessageDialog(null,"参数值必须在512~1024之间且为64的整数倍数,请重新设置!","参数设置错误",JOptionPane.INFORMATION_MESSAGE);
JTFSetStrength.setText("");
}
} catch(Exception ex){ System.out.println(ex);}
}
//*****************设置接收方IP*******************************
if(e.getSource()==JBSetIp){
String stra=JTFIpA.getText();
String strb=JTFIpB.getText();
String strc=JTFIpC.getText();
String strd=JTFIpD.getText();
a=Integer.parseInt(stra);
b=Integer.parseInt(strb);
c=Integer.parseInt(strc);
d=Integer.parseInt(strd);
byte[] addrByte=new byte[4];
addrByte[0]=(byte)a;
addrByte[1]=(byte)b;
addrByte[2]=(byte)c;
addrByte[3]=(byte)d;
try
{
addr=InetAddress.getByAddress(addrByte);
System.out.println("Ip1="+a+",Ip2="+c+",Ip3="+c+",Ip4="+d);
//System.out.println("IpA="+IpA+",IpB="+IpB+",IpC="+IpC+",IpD="+IpD);
}
catch(Exception ex){ System.out.println(ex);}
}
//********************对消息进行签名***********************************
if(e.getSource()==JBSign){
try
{
String myinfo=JTAShowMes.getText();
MessageDigest alga= MessageDigest.getInstance("MD5");//SHA-1
alga.update(myinfo.getBytes());
byte[] digesta=alga.digest();
System.out.println("本信息摘要是:"+byte2hex(digesta));
KeyPairGenerator keygen= KeyPairGenerator.getInstance("DSA");
keygen.initialize(n);
//生成密钥公钥pubkey和私钥prikey
KeyPair keys=keygen.generateKeyPair(); //生成密钥组
PublicKey pubkey=keys.getPublic();
PrivateKey prikey=keys.getPrivate();
System.out.println("公鈅是"+pubkey);
System.out.println("私鈅是"+prikey);
//分别保存在myprikey.dat和mypubkey.dat中,以便下次不在生成,生成密钥对的时间比较长
FileOutputStream fout=new FileOutputStream("myprikey.dat");
ObjectOutputStream priout=new ObjectOutputStream(fout);
priout.writeObject(prikey);
priout.close();
System.out.println("---------存储私鈅成功---------");
System.out.println();
FileOutputStream ffout=new FileOutputStream("mypubkey.dat");
ObjectOutputStream pubout=new ObjectOutputStream(ffout);
pubout.writeObject(pubkey);
pubout.close();
System.out.println("---------存储公鈅成功---------");
System.out.println();
//用私钥(prikey)对信息(info)进行数字签名产生一个签名数组
//从文件中读入私人密钥(prikey)
java.io.ObjectInputStream in=new java.io.ObjectInputStream(new java.io.FileInputStream("myprikey.dat"));
PrivateKey myprikey=(PrivateKey)in.readObject();
in.close();
System.out.println("---------读取私鈅成功,下一步用私鈅对摘要进行签名---------");
System.out.println();
//初始一个Signature对象,并用私钥对信息签名
java.security.Signature signet=java.security.Signature.getInstance("DSA");
signet.initSign(myprikey);
signet.update(myinfo.getBytes());
byte[] signed=signet.sign();
System.out.println("签名成功,签名是"+byte2hex(signed));
System.out.println();
//把信息和签名保存在一个文件中(myinfo.dat)
FileOutputStream fffout=new FileOutputStream("myinfo.dat");
ObjectOutputStream oout=new ObjectOutputStream(fffout);
oout.writeObject(myinfo);
oout.close();
System.out.println("---------已成功将信息和签名保存在文件中,以便接收方进行验证---------");
System.out.println();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
//***********************发送消息****************************************
if(e.getSource()==JBSendMes){
System.out.println("发送消息...");
System.out.println();
//int a,b,c,d;
try
{
/*
王荣: 218.196.119.11
谢子平:218.196.119.217
*/
//ClientSoc=new Socket("218.196.119.11",8088);
ClientSoc=new Socket(addr,8088);
System.out.println("接收方IP:"+a+"."+b+"."+c+"."+d);
System.out.println();
System.out.println("------Connecting---------");
System.out.println("Socket已就绪!");
System.out.println("连接至IP: "+ClientSoc.getInetAddress().getHostAddress());
System.out.println("连接至端口: "+8088);
System.out.println("------Connecting---------");
System.out.println();
FileInputStream fs=new FileInputStream("myinfo.dat");
//创建网络输出流并提供数据包装器
OutputStream netOut=ClientSoc.getOutputStream();
OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
//创建文件读取缓冲区
byte[] buf=new byte[65536];
int len=fs.read(buf);
while(len!=(-1)) //是否读完文件
{
doc.write(buf,0,len);//把文件数据写出网络缓冲区
doc.flush();//刷新缓冲区把数据写往客户端
len=fs.read(buf);//继续从文件中读取数据
}
doc.close();
netOut.close();
fs.close();
ClientSoc.close();
System.out.println("发送消息...");
System.out.println();
}
catch(Exception ex){ System.out.println(ex); }
}
//**************************发送公钥*********************************
if(e.getSource()==JBSendPubK){
System.out.println("发送公钥...");
System.out.println();
try
{
FileInputStream ObjInStr=new FileInputStream("mypubkey.dat");
ClientSoc=new Socket("218.196.119.11",8088);
//创建网络输出流并提供数据包装器
OutputStream OutStr=ClientSoc.getOutputStream();
OutputStream StrPubK=new DataOutputStream(new BufferedOutputStream(OutStr));
//创建文件读取缓冲区
byte[] bf=new byte[1024];
int Num=ObjInStr.read(bf);
System.out.println("***********文件长度:"+Num);
while(Num!=(-1)) //是否读完文件
{
StrPubK.write(bf,0,Num);//把文件数据写出网络缓冲区
StrPubK.flush();//刷新缓冲区把数据写往客户端
Num=ObjInStr.read(bf);//继续从文件中读取数据
}
StrPubK.close();
OutStr.close();
ObjInStr.close();
ClientSoc.close();
}catch(Exception ex){ System.out.println(ex); }
}
//********************打开一个新的消息编辑窗口**************************************
if(e.getSource()==JBEdit){
////如果点击"退出"按钮,关闭窗口,退出程序
try{new JClient();
}catch(Exception ex){ System.out.println(ex); }
}
}
/////////////////////////////////////////////////////////////
//主函数
public static void main(String[] args)
{
JClient frame=new JClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -