2 Star 0 Fork 0

BESTI.IS.JAVA2018 / 20165325

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Client_3.java 3.10 KB
一键复制 编辑 原始数据 按行查看 历史
20165325 提交于 2018-05-27 16:50 . 检查点4:基于DH的秘钥传递
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.Socket;
import java.security.Key;
import java.util.Optional;
import java.util.Scanner;
public class Client_3 {
public static void main(String args[]) {
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
Scanner scanner = new Scanner(System.in);
String str;
try {
mysocket = new Socket("192.168.1.125", 3001);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
System.out.println("已连接服务器,请输入中缀表达式:");
str = scanner.nextLine();
Key_DH.fun("Cpub.txt","Cpri.txt");
FileInputStream fp = new FileInputStream("Cpub.txt");
ObjectInputStream bp = new ObjectInputStream(fp);
Key kp = (Key) bp.readObject();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(kp);
byte[] kb = baos.toByteArray();
out.writeUTF(kb.length + "");
for (int i = 0; i < kb.length; i++) {
out.writeUTF(kb[i] + "");
}
Thread.sleep(1000);
int len = Integer.parseInt(in.readUTF());
byte np[] = new byte[len];
for (int i = 0;i<len;i++) {
String temp = in.readUTF();
np[i] = Byte.parseByte(temp);
}
ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (np));
Key k2 = (Key)ois.readObject();;
FileOutputStream f2 = new FileOutputStream("Spub.txt");
ObjectOutputStream b2 = new ObjectOutputStream(f2);
b2.writeObject(k2);
KeyAgree.fun("Spub.txt","Cpri.txt");
FileInputStream f = new FileInputStream("sb.txt");
byte[] keysb = new byte[24];
f.read(keysb);
System.out.println("公共密钥:");
for (int i = 0;i<24;i++) {
System.out.print(keysb[i]+",");
}
System.out.println("");
SecretKeySpec k = new SecretKeySpec(keysb, "DESede");
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[] = MyBC.tran(new StringBuffer(str)).toString().getBytes("UTF-8");
byte ctext[] = cp.doFinal(ptext);
System.out.println("后缀表达式已被加密:");
for (int i = 0; i < ctext.length; i++) {
System.out.print(ctext[i] + ",");
}
System.out.println("");
out.writeUTF(ctext.length + "");
for (int i = 0; i < ctext.length; i++) {
out.writeUTF(ctext[i] + "");
}
String s = in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回应:" + s);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
}
}
Java
1
https://gitee.com/BESTI-IS-JAVA-2018/20165325.git
git@gitee.com:BESTI-IS-JAVA-2018/20165325.git
BESTI-IS-JAVA-2018
20165325
20165325
master

搜索帮助