2017-2018-2 20165202 实验五《网络编程与安全》实验报告
一、实验报告封面
二、实验步骤
(一)
实验要求:
- 结对实现中缀表达式转后缀表达式的功能 MyBC.java
- 结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能,调用MyDC.java
- 上传测试代码运行结果截图和码云链接
步骤:
1.MyBC.java
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;
}
}
}
运行截图:
(二)结对编程
实验要求:
一人负责客户端,一人负责服务器
0.注意责任归宿,要会通过测试证明自己没有问题
1.基于Java Socket实现客户端/服务器功能,传输方式用TCP
2.客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器
3.服务器接收到后缀表达式,调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
4.客户端显示服务器发送过来的结果
5.上传测试结果截图和码云链接
步骤:
-
端口号和IP设置代码
Socket socket = new Socket("192.168.199.185",5202);
System.out.println("客户端启动成功"
1.SocketService.java
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(5202);
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("UTF8");
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);
}
// 获取密文
}
}
- SocketClient.java
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("172.20.10.3",5204);
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);
}
}
}
运行截图
(三)加密结对编程
实验要求
一人负责客户端,一人负责服务器
-
注意责任归宿,要会通过测试证明自己没有问题
-
基于Java Socket实现客户端/服务器功能,传输方式用TCP
-
客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器
-
服务器接收到后缀表达式表达式后,进行解密(和客户端协商密钥,可以用数组保存),然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
-
客户端显示服务器发送过来的结果
-
上传测试结果截图和码云链接
步骤:
代码同(二)
运行截图
(四)密钥分发结对编程
实验要求
0. 注意责任归宿,要会通过测试证明自己没有问题
-
基于Java Socket实现客户端/服务器功能,传输方式用TCP
-
客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文发送给服务器
-
客户端和服务器用DH算法进行3DES或AES算法的密钥交换
-
服务器接收到后缀表达式表达式后,进行解密,然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
-
客户端显示服务器发送过来的结果
-
上传测试结果截图和码云链接
步骤
1.SocketClient.java
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.*;
import javax.crypto.spec.SecretKeySpec;
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.252", 5219);
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);
// 读取对方的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();
System.out.println(sb.length);
byte[]ssb=new byte[24];
for(int i=0;i<24;i++)
ssb[i]=sb[i];
Key k=new SecretKeySpec(ssb,"DESede");
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);
}
}
}
2.SocketService.java
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(args);
} 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(String []args) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyDC mydc=new MyDC();
try{
ServerSocket server=null;
try{
server=new ServerSocket(5219);
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");
// 读取对方的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();
byte[]ssb=new byte[24];
for(int i=0;i<24;i++)
ssb[i]=sb[i];
SecretKeySpec k=new SecretKeySpec(ssb,"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);
writer.println(a);
writer.flush();
writer.close();
in.close();
socket.close();
server.close();
}catch(Exception e) {
System.out.println("Error."+e);
}
// 获取密文
}
}
运行截图
(五)完整性校验结对编程
实验要求
-
注意责任归宿,要会通过测试证明自己没有问题
-
基于Java Socket实现客户端/服务器功能,传输方式用TCP
-
客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器
-
客户端和服务器用DH算法进行3DES或AES算法的密钥交换
-
服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
-
客户端显示服务器发送过来的结果
-
上传测试结果截图和码云链接
步骤
1.SocketClient.java
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.199.185", 5209);
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();
String x=readline;
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);
}
write.println(result);
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);
}
}
}
2.SocketService.java
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(5209);
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);
}
// 获取密文
}
}
运行截图
三、遇到的问题及解决
1.编写客户端代码的时候发现如果要连接到服务器必须知道服务器的IP地址,但是在linux的命令行中查看PI的命令不是windows中的ipconfig
解决办法:通过百度得知linux的命令行中查看IP地址的命令是ifconfig
四、PSP时间统计
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 35min | 7% |
设计 | 45min | 21% |
代码实现 | 90min | 42% |
测试 | 30min | 15% |
分析总结 | 21min | 11% |