一、网络编程与安全-1:
实验要求:
- 参考
- 结对实现中缀表达式转后缀表达式的功能 MyBc.java
- 结对实验从上面功能中获取的表达式中实现后缀表达式求值的功能,调用MyDc.java
实验代码:
- My.Bc:
import java.util.*;
public class MyBC {
private static LinkedList<String> op1 = new LinkedList<>();
private static LinkedList<String> op2 = new LinkedList<>();
private static StringBuilder a = new StringBuilder();
public StringBuilder houzhui(LinkedList<String> list) {
Iterator<String> i = list.iterator();
while (i.hasNext()) {
String s = i.next();
if (isOperator(s)) {
if (op1.isEmpty()) {
op1.push(s);
} else {
if (priority(op1.peek()) <= priority(s) && !s.equals(")")) {
op1.push(s);
} else if (!s.equals(")") && priority(op1.peek()) > priority(s)) {
while (op1.size() != 0 && priority(op1.peek()) >= priority(s)
&& !op1.peek().equals("(")) {
if (!op1.peek().equals("(")) {
String operator = op1.pop();
a.append(operator).append(" ");
op2.push(operator);
}
}
op1.push(s);
} else if (s.equals(")")) {
while (!op1.peek().equals("(")) {
String operator = op1.pop();
a.append(operator).append(" ");
op2.push(operator);
}
op1.pop();
}
}
} else {
a.append(s).append(" ");
op2.push(s);
}
}
if (!op1.isEmpty()) {
Iterator<String> iterator = op1.iterator();
while (iterator.hasNext()) {
String operator = iterator.next();
a.append(operator).append(" ");
op2.push(operator);
iterator.remove();
}
}
return a;
}
private static boolean isOperator(String oper) {
if (oper.equals("+") || oper.equals("-") || oper.equals("/") || oper.equals("*")
|| oper.equals("(") || oper.equals(")")) {
return true;
}
return false;
}
private static int priority(String s) {
switch (s) {
case "+":
return 1;
case "-":
return 1;
case "*":
return 2;
case "/":
return 2;
case "(":
return 3;
case ")":
return 3;
default:
return 0;
}
}
}
My.Dc:
import java.util.LinkedList;
import java.util.*;
import java.util.*;
public class MyDC {
public static int evaluate(StringBuilder b) {
LinkedList<String> mList = new LinkedList<>();
String[] postStr = b.toString().split(" ");
int result;
for (String s : postStr) {
if (fuhao(s)) {
if (!mList.isEmpty()) {
int num1 = Integer.valueOf(mList.pop());
int num2 = Integer.valueOf(mList.pop());
if (s.equals("/") && num1 == 0) {
System.out.println("除数不能为0");
return 0;
}
int newNum = cal(num2, num1, s);
mList.push(String.valueOf(newNum));
}
} else {
mList.push(s);
}
}
result=Integer.parseInt(mList.pop());
return result;
}
private static boolean fuhao(String a) {
if (a.equals("+") || a.equals("-") || a.equals("/") || a.equals("*")
|| a.equals("(") || a.equals(")")) {
return true;
}
return false;
}
private static int cal(int num1, int num2, String operator) {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
return 0;
}
}
}
My.DcTest:
import java.util.LinkedList;
import java.util.*;
public class MyDCTest {
public static void main(String[] args){
LinkedList<String> list=new LinkedList<>();
StringBuilder result1;
int result2;
String expression, again;
System.out.println("请输入一个中缀表达式并以#结束");
Scanner scanner=new Scanner(System.in);
String s;
while (!(s=scanner.next()).equals("#")) {
list.add(s);
}
MyBC hz=new MyBC();
result1 = hz.houzhui(list);
System.out.println("后缀表达式: "+result1);
MyDC evaluator = new MyDC();
result2 = evaluator.evaluate(result1);
System.out.println("That expression equals " + result2);
System.out.println();
}
}
实验截图:
二、网络安全编程-2:
实验要求:
- 结对编程:1人负责客户端,1人负责服务器
- 注意责任归宿,要会通过测试证明自己没有问题
- 基于Java Scokect实现客户端/服务器功能,传输方式用TCP
- 客户端让用户输入中缀表达式,然后把中缀表达式用MyBc.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器
- 服务器接收到后缀表达式,调用MyDc.java的功能计算后缀表达式,把结果发送给客户端
- 客户端显示服务器发送过来的结果
- 上传测试截图和码云链接
实验代码:
- Stack:
public class MyStack<T> implements SStack<T> {
private Object element[];
private int top;
public MyStack(int size){
this.element = new Object[Math.abs(size)];
this.top = -1;
}
public MyStack() {
this(64);
}
public boolean isEmpty() {
return this.top == -1;
}
public void push(T x) {
if(x==null)
return;
if(this.top == element.length-1){
Object[] temp = this.element;
this.element = new Object[temp.length*2];
for(int i = 0; i < temp.length; i++)
this.element[i] = temp[i];
}
this.top++;
this.element[this.top] = x;
}
public T pop() {
return this.top==-1 ? null:(T)this.element[this.top--];
}
public T get() {
return this.top==-1 ? null:(T)this.element[this.top];
}
}
SStack:
public interface SStack<T> {
boolean isEmpty();
void push(T x);
T pop();
T get();
}
Client(客户端)
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main(String srgs[]) {
try {
//创建连接特定服务器的指定端口的Socket对象
Socket socket = new Socket("127.0.0.1", 4421);
//获得从服务器端来的网络输入流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获得从客户端向服务器端输出数据的网络输出流
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
//创建键盘输入流,以便客户端从键盘上输入信息
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入待发送的数据:");
String str=stdin.readLine(); //从键盘读入待发送的数据
String postfix = MyBC.toPostfix(str);
out.println(postfix); //通过网络传送到服务器
str=in.readLine();//从网络输入流读取结果
System.out.println( "从服务器接收到的结果为:"+str); //输出服务器返回的结果
}
catch (Exception e) {
System.out.println(e);
}
finally{
//stdin.close();
//in.close();
//out.close();
//socket.close();
}
}
}
Service(服务器):
import java.net.*;
import java.io.*;
public class TCPServer{
public static void main(String srgs[]) {
ServerSocket sc = null;
Socket socket=null;
try {
NewMyDC evaluator = new NewMyDC();
sc= new ServerSocket(4421);//创建服务器套接字
System.out.println("端口号:" + sc.getLocalPort());
System.out.println("服务器已经启动...");
socket = sc.accept(); //等待客户端连接
System.out.println("已经建立连接");
//获得网络输入流对象的引用
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
////获得网络输出流对象的引用
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
String aline=in.readLine();//读取客户端传送来的数据
System.out.println("从客户端接收到信息为:"+aline); //通过网络输出流返回结果给客户端
int result = evaluator.value(aline);
out.println("Echo:" + result);
out.close();
in.close();
sc.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
实验截图:
三、网络编程与安全-3:
实验要求:
- 加密结对编程:1人负责客户端,1人负责服务器
- 注意责任归宿,要会通过测试证明自己没有问题
- 基于Java Socket实现客户端/服务器功能,传输方式用TCP
- 客户端让用户输入中缀表达式,然后把中缀表达式调用MyBc.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器
- 服务器接收到后缀表达式后,进行解密(和客户端协商密匙,可以用数组保存)然后调用MyDc.java的功能计算后缀表达式的值,把结果发送给客户端
- 客户端显示服务器发送过来的结果
- 上传测试结果截图和码云链接
实验代码:
- 客服端加密并发送给服务器代码如下:
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(168);
SecretKey k = kg.generateKey();
byte[] ptext2 = k.getEncoded();
Socket socket = new Socket("127.0.0.1", 4421);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
//RSA算法,使用服务器端的公钥对DES的密钥进行加密
FileInputStream f3 = new FileInputStream("Skey_RSA_pub.dat");
ObjectInputStream b2 = new ObjectInputStream(f3);
RSAPublicKey pbk = (RSAPublicKey) b2.readObject();
BigInteger e = pbk.getPublicExponent();
BigInteger n = pbk.getModulus();
BigInteger m = new BigInteger(ptext2);
BigInteger c = m.modPow(e, n);
String cs = c.toString();
out.println(cs); // 通过网络将加密后的秘钥传送到服务器
System.out.print("请输入待发送的数据:");
//用DES加密明文得到密文
String s = stdin.readLine(); // 从键盘读入待发送的数据
String postfix = MyBC.toPostfix(s);
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[] = postfix.getBytes("UTF8");
byte ctext[] = cp.doFinal(ptext);
String str = parseByte2HexStr(ctext);
out.println(str); // 通过网络将密文传送到服务器
服务器解密,计算结果并发送给客户端代码如下:
String line = in.readLine();
BigInteger cipher = new BigInteger(line);
FileInputStream f = new FileInputStream("Skey_RSA_priv.dat");
ObjectInputStream b = new ObjectInputStream(f);
RSAPrivateKey prk = (RSAPrivateKey) b.readObject();
BigInteger d = prk.getPrivateExponent();
BigInteger n = prk.getModulus();//mod n
BigInteger m = cipher.modPow(d, n);//m=d (mod n)
System.out.println("d= " + d);
System.out.println("n= " + n);
System.out.println("m= " + m);
byte[] keykb = m.toByteArray();
// 使用DES对密文进行解密
String readline = in.readLine();//读取客户端传送来的数据
FileInputStream f2 = new FileInputStream("keykb1.dat");
int num2 = f2.available();
byte[] ctext = parseHexStr2Byte(readline);
Key k = new SecretKeySpec(keykb,"DESede");
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte[] ptext = cp.doFinal(ctext);
String p = new String(ptext, "UTF8");//编码转换
System.out.println("从客户端接收到信息为:" + p); //打印解密结果
NewMyDC evaluator = new NewMyDC();
int _result = evaluator.value(p);
out.println("Echo:" + _result);
out.close();
in.close();
link.close();
服务器代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SocketService {
public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
SocketService socketService = new SocketService();
try {
socketService.oneServer();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
public void oneServer() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyDC mydc=new MyDC();
try{
ServerSocket server=null;
try{
server=new ServerSocket(5204);
System.out.println("服务器启动成功");
}catch(Exception e) {
System.out.println("没有启动监听:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer=new PrintWriter(socket.getOutputStream());
line=in.readLine();
System.out.printf("密文=%s\n",line);
// 获取密钥
byte[]ctext=line.getBytes("ISO-8859-1");
FileInputStream f2=new FileInputStream("keykb1.dat");
int num2=f2.available();
byte[ ] keykb=new byte[num2];
System.out.printf("\n");
f2.read(keykb);
SecretKeySpec k=new SecretKeySpec(keykb,"DESede");
// 解密
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte []ptext=cp.doFinal(ctext);
// 显示明文
String p=new String(ptext,"UTF8");
System.out.println("明文:"+p);
int a;
String Np="";
for(int i=1;i<p.length();i++)
Np+=p.charAt(i);
a=mydc.evaluate(Np);
writer.println(a);
writer.flush();
writer.close();
in.close();
socket.close();
server.close();
}catch(Exception e) {
System.out.println("Error."+e);
}
// 获取密文
}
}
客户端代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.*;
import java.security.*;
import java.util.Arrays;
import javax.crypto.*;
public class SocketClient {
// 搭建客户端
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyBC mybc=new MyBC();
try {
Socket socket = new Socket("192.168.43.166",5329);
System.out.println("客户端启动成功");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter write = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
System.out.println("Client:" );
readline = br.readLine();
String line=mybc.evaluate(readline);
FileInputStream f=new FileInputStream("key1.dat");
ObjectInputStream b=new ObjectInputStream(f);
Key k=(Key)b.readObject();
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[]=line.getBytes("UTF-8");
byte ctext[]=cp.doFinal(ptext);
String Str=new String(ctext,"ISO-8859-1");
write.println(Str);
write.flush();
System.out.println("Server return:" + in.readLine());
write.close();
in.close();
socket.close();
} catch (Exception e) {
System.out.println("can not listen to:" + e);
}
}
}
实验截图:
四、网络安全与编程-4:
实验要求:
- 密匙分发结对编程:1人负责客户端,1人负责服务器
- 注意责任归宿,要会通过测试证明自己没问题
- 基于Java Socket实现客户端/服务器功能,传输方式用TCP
- 客户端让用户输入中缀表达式,然后把中缀表达式用MyBc.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文发送给服务器
- 客户端和服务器用DH算法进行3DES或AES算法的密匙交换
- 服务器接收到后缀表达式后,进行解密,然后调用MyDc.java的功能计算后缀表达式的值,把结果发送给客户端
- 客户端显示发送过来的结果
- 上传测试结果截图和码云链接:
实验代码:
- DH公匙代码如下:
public class Key_DH{
private static final byte skip1024ModulusBytes[] = {
(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,
(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,
(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,
(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,
(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,
(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,
(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,
(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,
(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,
(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,
(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,
(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,
(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,
(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,
(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,
(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,
(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,
(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,
(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,
(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,
(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,
(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,
(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,
(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,
(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,
(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,
(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,
(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,
(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,
(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,
(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,
(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7
};
// The SKIP 1024 bit modulus
private static final BigInteger skip1024Modulus
= new BigInteger(1, skip1024ModulusBytes);
// The base used with the SKIP 1024 bit modulus
private static final BigInteger skip1024Base = BigInteger.valueOf(2);
public static void main(String args[ ]) throws Exception{
DHParameterSpec DHP=
new DHParameterSpec(skip1024Modulus,skip1024Base);
KeyPairGenerator kpg= KeyPairGenerator.getInstance("DH");
kpg.initialize(DHP);
KeyPair kp=kpg.genKeyPair();
PublicKey pbk=kp.getPublic();
PrivateKey prk=kp.getPrivate();
// 保存公钥
FileOutputStream f1=new FileOutputStream(args[0]);
ObjectOutputStream b1=new ObjectOutputStream(f1);
b1.writeObject(pbk);
// 保存私钥
FileOutputStream f2=new FileOutputStream(args[1]);
ObjectOutputStream b2=new ObjectOutputStream(f2);
b2.writeObject(prk);
}
}
创建共享密匙:
public class KeyAgree{
public static void main(String args[ ]) throws Exception{
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream(args[0]);
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream(args[1]);
ObjectInputStream b2=new ObjectInputStream(f2);
PrivateKey prk=(PrivateKey)b2.readObject( );
// 执行密钥协定
KeyAgreement ka=KeyAgreement.getInstance("DH");
ka.init(prk);
ka.doPhase(pbk,true);
//生成共享信息
byte[ ] sb=ka.generateSecret();
for(int i=0;i<sb.length;i++){
System.out.print(sb[i]+",");
}
SecretKeySpec k=new SecretKeySpec(sb,"DESede");
}
}
实验截图:
五、网络安全编程-5:
实验要求:
- 完整性校验结对编程:1人负责客户端,1人负责服务器
- 注意责任归宿:要会通过测试证明自己没有问题
- 基于Java Socket实现客户端/服务器功能,传输方式用TCP
- 客户端让用户输入中缀表达式,然后把中缀表达式调用MyBc.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法进行加密通过网络把密文发送给服务器
- 客服端和服务器用DH算法进行3DES或AES算法的密匙交换
- 服务器接收到后缀表达式后进行解密,解密后计算明文的MD5值,和客户端传过来的MD5值进行比较,一致则调用MyDc.java算法计算后缀表达式的值,把结果发送给客户端
- 客户端显示发送过来的结果
- 上传测试结果截图和码云链接
实验代码:
- Service:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SocketService {
public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
SocketService socketService = new SocketService();
try {
socketService.oneServer();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
public void oneServer() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyDC mydc=new MyDC();
try{
ServerSocket server=null;
try{
server=new ServerSocket(5329);
System.out.println("服务器启动成功");
}catch(Exception e) {
System.out.println("没有启动监听:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer=new PrintWriter(socket.getOutputStream());
line=in.readLine();
String line1=in.readLine();
System.out.printf("密文=%s\n",line);
// 获取密钥
byte[]ctext=line.getBytes("ISO-8859-1");
FileInputStream f2=new FileInputStream("keykb1.dat");
int num2=f2.available();
byte[ ] keykb=new byte[num2];
System.out.printf("\n");
f2.read(keykb);
SecretKeySpec k=new SecretKeySpec(keykb,"DESede");
// 解密
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte []ptext=cp.doFinal(ctext);
// 显示明文
String p=new String(ptext,"UTF8");
int a;
System.out.println("明文:"+p);
String Np="";
for(int i=1;i<p.length();i++)
Np+=p.charAt(i);
a=mydc.evaluate(Np);
String x=p;
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(x.getBytes("UTF8"));
byte s[ ]=m.digest( );
String result="";
for (int i=0; i<s.length; i++){
result+=Integer.toHexString((0x000000ff & s[i]) |
0xffffff00).substring(6);
}
if(!(line1.equals(result)))System.out.printf("MD5比对正确!\n");
writer.println(a);
writer.flush();
writer.close();
in.close();
socket.close();
server.close();
}catch(Exception e) {
System.out.println("Error."+e);
}
// 获取密文
}
创建共享密匙:
public class KeyAgree{
public static void main(String args[ ]) throws Exception{
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream(args[0]);
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream(args[1]);
ObjectInputStream b2=new ObjectInputStream(f2);
PrivateKey prk=(PrivateKey)b2.readObject( );
// 执行密钥协定
KeyAgreement ka=KeyAgreement.getInstance("DH");
ka.init(prk);
ka.doPhase(pbk,true);
//生成共享信息
byte[ ] sb=ka.generateSecret();
for(int i=0;i<sb.length;i++){
System.out.print(sb[i]+",");
}
SecretKeySpec k=new SecretKeySpec(sb,"DESede");
}
}
实验截图:
六、实验体会:
- 在最初拿到这个实验时一筹莫展,不同与以往的实验,这次实验没有现成的教程,一切都需要自己摸索实践,我和结对伙伴原本准备分开完成客户端和服务器代码的,但在实践编写后我们发现单独编写对我们两的难度太大了,于是我们两最终一起编写了所有的代码,在此过程中我们两互相启迪,协同共进,虽然遇到了许多问题,但最终还是迎刃而解,在此次试验中,我十分感谢自己的伙伴,他给了许多帮助,也让我了解学习了许多