package win1;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.TextArea;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import myrsa2.RSAUtils;
/*
*
* 1、生成RSA密钥对,公钥发给客户端使用,私钥由服务器使用;
2、客户端用公钥加密所有发出的数据,也用公钥解密所有收到的数据;
3、服务器用私钥加密所有发出的数据,也用私钥解密所有收到的数据;
* */
public class ServerWindow1 extends JFrame implements Runnable
{
/**
*
*/
private static final long serialVersionUID = -9061845795224200533L;
private JPanel contentPane;
JLabel jLabel1 = new JLabel();
TextArea jTextArea1 = new TextArea("你好!", 100, 250,TextArea.SCROLLBARS_VERTICAL_ONLY);
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
Thread c;
private DatagramSocket sendSocket, receiveSocket;
private DatagramPacket sendPacket, receivePacket;
private String public_exponent="65537";
private String modulus="104344065049535235337602750745972062206029307981021347712560211453856339371441553987973583092897777954138410872748183586468653712704255491417965770505559379560464351555212396558781035059397673682011104226754831711072822991514780158821061675743098642785196847109412740308768227675829387795015925250708654667999";
//私钥指数
private String private_exponent = "26190699453207419035103304236862236954532281250099961393893761972869322408108603736853463568338014363574421454395343393768548355493614337455567647661877282282990417681871242149491729973582918161723756665580166306049786985876261601361679056199457576596963451088706448549329113352190937000411497436484850157873";
private RSAPublicKey pubKey;
private RSAPrivateKey priKey;
private String client_modulus="113073745494321136725643384276722554235918070888758423613996572688337880324241477133718433831999005142699224319183845232695295297468715990337671678419444783042160223540025404015195691927944232410884578134672967651594589403061833363946041818438441160271287476138927799777730705047831779398357394582752490728477";
private String client_public_exponent="65537";
private RSAPublicKey client_pubKey;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
ServerWindow1 frame = new ServerWindow1();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/*initial the pubkey and prikey
*/
public void InitKey()
{
// 使用模和指数生成公钥和私钥
pubKey = RSAUtils.getPublicKey(modulus, public_exponent);
priKey = RSAUtils.getPrivateKey(modulus, private_exponent);
jTextArea1.append("\npublicKey:" + pubKey);
jTextArea1.append("\nprivateKey:" + priKey + "\n");
client_pubKey = RSAUtils.getPublicKey(client_modulus, client_public_exponent);
jTextArea1.append("\nclient publicKey:" + client_pubKey);
}
/*
* Create the frame.
*/
public ServerWindow1()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 430, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//this.setSize(new Dimension(400, 400));
this.setTitle("UDPServer");
jLabel1.setText("通信记录:");
jLabel1.setBounds(new Rectangle(10, 3, 70, 30));
contentPane.setLayout(null);
jTextArea1.setBounds(new Rectangle(15, 30, 400, 350));
jTextArea1.setEditable(false);
InitKey();
jLabel2.setText("输入通信内容:");
jLabel2.setBounds(new Rectangle(17, 385, 92, 37)); // 创建输入内容区域
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(130, 385, 244, 31));
jTextField1.setEditable(true);
jTextField1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}
});
contentPane.add(jLabel1, null);
contentPane.add(jTextArea1, null);
contentPane.add(jTextField1, null);
contentPane.add(jLabel2, null);
try
{
sendSocket = new DatagramSocket();
receiveSocket = new DatagramSocket(55513);
} catch (SocketException e)
{
jTextArea1.append("不能打开数据报Socket,或者数据报Socket无法与指定端口连接!");
}
c = new Thread(this); // 创建一个线程
c.start();
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jTextField1_actionPerformed(ActionEvent e)
{
try
{
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);
jTextArea1.append("\n\n服务端"+time);
String string1 = jTextField1.getText().trim();
jTextArea1.append("\n加密前明文:"+string1);
string1 = RSAUtils.encryptByPublicKey(string1, client_pubKey);
jTextArea1.append("\n加密后密文:"+string1);
System.out.print("\nstring1.getBytes():"+string1.getBytes());
System.out.print("\nstring1.getBytes().length:"+string1.getBytes().length);
sendPacket = new DatagramPacket(string1.getBytes(),
string1.getBytes().length,
java.net.InetAddress.getByName("127.0.0.1"), 55512);
sendSocket.send(sendPacket);
} catch (IOException e0)
{
jTextArea1.append("\n网络通信出现错误,问题在于" + e0.toString());
} catch (Exception e1)
{
// TODO Auto-generated catch block
jTextArea1.append("\nRSAUtils.encryptByPublicKey error!!!:" + e1.toString());
}
}
@Override
public void run()
{
// TODO Auto-generated method stub
while (true)
{
try
{
byte buf[] = new byte[256];//接受的最大长度,超出的长度被丢弃
receivePacket = new DatagramPacket(buf, buf.length);
receiveSocket.receive(receivePacket);
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);
jTextArea1.append("\n\n客户端:"+time);
byte[] data = receivePacket.getData();
String receiveString = new String(data);
jTextArea1.append("\n解密前密文:"+receiveString);
System.out.print("\npriKey:"+priKey);
receiveString = RSAUtils.decryptByPrivateKey(receiveString, priKey);
jTextArea1.append("\n解密后明文:"+receiveString);
} catch (IOException e)
{
jTextArea1.append("\n网络通信出现错误,问题在于" + e.toString());
} catch (Exception e)
{
// TODO Auto-generated catch block
jTextArea1.append("\nRSAUtils.decryptByPrivateKey error!!!:" + e.toString());
}
}
}
}
UDP协议实现对等通讯Java+RSA加密解密传送信息实现
2星 需积分: 23 18 浏览量
2016-06-24
21:40:00
上传
评论 2
收藏 46KB RAR 举报
chenshuichuan
- 粉丝: 16
- 资源: 7