## 实验一
#### 任务
两人一组结对编程:
0. 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA
1. 结对实现中缀表达式转后缀表达式的功能 MyBC.java
2. 结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能,调用MyDC.java
3. 上传测试代码运行结果截图和码云链接
#### 任务
两人一组结对编程:
0. 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA
1. 结对实现中缀表达式转后缀表达式的功能 MyBC.java
2. 结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能,调用MyDC.java
3. 上传测试代码运行结果截图和码云链接
#### 实验过程
这个任务的目的是将中缀表达式转换为后缀表达式,并通过老师的代码来进行计算,在添加代码的地方就是将将中缀表达式转化为后缀表达式。要求写成myBC进行操作。
关于中缀表达式转后缀表达式需要用到关键步骤如下:
* 重要的数据结构----栈
* 如果读入操作数,则直接放入输出字符串;
* 如果读入操作符则放入堆栈,但是放入堆栈之前必须要检查栈顶。
* 如果读入(,便放入栈中,但是注意,当左括号放入栈中后,则优先级最低
* 如果读入),则将栈中运算符取出放入输出字符串。
* 如果还有其他操作符,便栈中弹出元素直到遇到
* 更低优先级的元素或栈空。之后将遇到的操作符压入栈中。
* 如果读到了输入的末尾,则将栈中所有元素依次弹出。
* 如果读入操作数,则直接放入输出字符串;
* 如果读入操作符则放入堆栈,但是放入堆栈之前必须要检查栈顶。
* 如果读入(,便放入栈中,但是注意,当左括号放入栈中后,则优先级最低
* 如果读入),则将栈中运算符取出放入输出字符串。
* 如果还有其他操作符,便栈中弹出元素直到遇到
* 更低优先级的元素或栈空。之后将遇到的操作符压入栈中。
* 如果读到了输入的末尾,则将栈中所有元素依次弹出。
关于后缀表达式的计算 关键步骤如下:
* 从左到右遍历表达式
* 如果遇到数字就进栈
* 如果遇到操作符就将处于栈顶两个数字出栈
* 然后将运算结果进栈。
* 直到遍历完后缀表达式为结束
补充31到40行
```
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
if (isOperator(token)) {
op2 = (stack.pop().intValue());
op1 = (stack.pop().intValue());
result = evalSingleOp(token.charAt(0), op1, op2);
stack.push(result);
} else {
stack.push((Integer.parseInt(token)));
}
}
return result;
}
```
#### 实验截图:
* 如果遇到数字就进栈
* 如果遇到操作符就将处于栈顶两个数字出栈
* 然后将运算结果进栈。
* 直到遍历完后缀表达式为结束
补充31到40行
```
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
if (isOperator(token)) {
op2 = (stack.pop().intValue());
op1 = (stack.pop().intValue());
result = evalSingleOp(token.charAt(0), op1, op2);
stack.push(result);
} else {
stack.push((Integer.parseInt(token)));
}
}
return result;
}
```
#### 实验截图:
## 实验二
任务
结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器;
(4)服务器接收到后缀表达式,调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(5)客户端显示服务器发送过来的结果;
(6)上传测试结果截图和码云链接;
#### 实验过程
目的是实现客户端输入中缀表达式后将后缀表达式发送给服务器,服务器计算后,将结果发送给客户端
任务
结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器;
(4)服务器接收到后缀表达式,调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(5)客户端显示服务器发送过来的结果;
(6)上传测试结果截图和码云链接;
#### 实验过程
目的是实现客户端输入中缀表达式后将后缀表达式发送给服务器,服务器计算后,将结果发送给客户端
1. 首先打开设置可以只知道自己本机的ip地址
1. 使用InetAddress类的静态方法getLocalHost()得到一个InetAddress对象即可得到本机的ip套接字。
2. 套接字
3. 实现程序之间的通信时,需要Socket类建立对象并连接在一起
4. 客户端套接字
2. 套接字
3. 实现程序之间的通信时,需要Socket类建立对象并连接在一起
4. 客户端套接字
`. Socket client = new Socket("172.1.0.0",一个端口号)`
* 使用方法getInputStream()获得一个输入流
* 使用方法getOutputStream()获得一个输出流
5.SeverSocket对象与服务器端套接字
* SeverSocket severForClient = new SeverSocket(端口号)
* 使用accept()方法将客户端和服务器的套接字连接起来
```
Socket sc = severForClient.accept()`
```
* sc调用方法getInputStream()获得一个输入流
* sc调用方法getOutputStream()获得一个输出流
* 使用方法getOutputStream()获得一个输出流
5.SeverSocket对象与服务器端套接字
* SeverSocket severForClient = new SeverSocket(端口号)
* 使用accept()方法将客户端和服务器的套接字连接起来
```
Socket sc = severForClient.accept()`
```
* sc调用方法getInputStream()获得一个输入流
* sc调用方法getOutputStream()获得一个输出流
#### 实验代码
客户端
```
package kd;
```
package kd;
import kd.MyBC;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.Scanner;
import java.net.*;
import java.lang.*;
import java.util.Scanner;
public class Client {
public static void main(String args[]) {
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{ mysocket=new Socket("127.1.0.0",2010);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
System.out.println("请输入算式:");
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
MyBC b=new MyBC();
str=b.result(str);
out.writeUTF(str);
String s=in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回答:"+s);
Thread.sleep(500);
}
catch(Exception e) {
System.out.println("服务器已断开"+e);
}
}
}
```
用户端
```
package kd;
public static void main(String args[]) {
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try{ mysocket=new Socket("127.1.0.0",2010);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
System.out.println("请输入算式:");
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
MyBC b=new MyBC();
str=b.result(str);
out.writeUTF(str);
String s=in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回答:"+s);
Thread.sleep(500);
}
catch(Exception e) {
System.out.println("服务器已断开"+e);
}
}
}
```
用户端
```
package kd;
import kd.MyDC;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String args[]) {
int answer;
ServerSocket serverForClient=null;
Socket socketOnServer=null;
DataOutputStream out=null;
DataInputStream in=null;
try { serverForClient = new ServerSocket(2010);
}
catch(IOException e1) {
System.out.println(e1);
}
try{ System.out.println("等待客户呼叫");
socketOnServer = serverForClient.accept(); //堵塞状态,除非有客户呼叫
out=new DataOutputStream(socketOnServer.getOutputStream());
in=new DataInputStream(socketOnServer.getInputStream());
String s=in.readUTF(); // in读取信息,堵塞状态
System.out.println("服务器收到客户的提问:"+s);
MyDC d=new MyDC();
answer=d.evaluate(s);
out.writeUTF(answer+"");
Thread.sleep(500);
}
catch(Exception e) {
System.out.println("客户已断开"+e);
}
}
}
```
#### 实验截图
import java.net.*;
public class Server {
public static void main(String args[]) {
int answer;
ServerSocket serverForClient=null;
Socket socketOnServer=null;
DataOutputStream out=null;
DataInputStream in=null;
try { serverForClient = new ServerSocket(2010);
}
catch(IOException e1) {
System.out.println(e1);
}
try{ System.out.println("等待客户呼叫");
socketOnServer = serverForClient.accept(); //堵塞状态,除非有客户呼叫
out=new DataOutputStream(socketOnServer.getOutputStream());
in=new DataInputStream(socketOnServer.getInputStream());
String s=in.readUTF(); // in读取信息,堵塞状态
System.out.println("服务器收到客户的提问:"+s);
MyDC d=new MyDC();
answer=d.evaluate(s);
out.writeUTF(answer+"");
Thread.sleep(500);
}
catch(Exception e) {
System.out.println("客户已断开"+e);
}
}
}
```
#### 实验截图
## 任务三
#### 1.实验要求:
加密结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器;
(4)服务器接收到后缀表达式表达式后,进行解密(和客户端协商密钥,可以用数组保存),然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(5)客户端显示服务器发送过来的结果;
(6)上传测试结果截图和码云链接;
#### 实验过程
堵塞状态,除非有客户呼叫
` socketOnServer = serverForClient.accept();`
通过in读取信息,否则为堵塞状态
`String s=in.readUTF();`
`String s = in.readUTF(); `
输入算式
` String str = scanner.nextLine();`
客户端进行加密
` String secret= Encorder.AESEncode(key,str);`
#### 实验代码
客户端
```
package kd;
#### 1.实验要求:
加密结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器;
(4)服务器接收到后缀表达式表达式后,进行解密(和客户端协商密钥,可以用数组保存),然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(5)客户端显示服务器发送过来的结果;
(6)上传测试结果截图和码云链接;
#### 实验过程
堵塞状态,除非有客户呼叫
` socketOnServer = serverForClient.accept();`
通过in读取信息,否则为堵塞状态
`String s=in.readUTF();`
`String s = in.readUTF(); `
输入算式
` String str = scanner.nextLine();`
客户端进行加密
` String secret= Encorder.AESEncode(key,str);`
#### 实验代码
客户端
```
package kd;
import kd.Encorder;
import kd.MyBC;
import kd.MyBC;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.Scanner;
import java.net.*;
import java.lang.*;
import java.util.Scanner;
public class Client1 {
public static void main(String args[]) throws Exception {
String key = "";
int n = -1;
byte[] a = new byte[128];
try {
File f = new File("key1.dat");
InputStream in = new FileInputStream(f);
while ((n = in.read(a, 0, 100)) != -1) {
key = key + new String(a, 0, n);
}
in.close();
} catch (IOException e) {
System.out.println("File read Error" + e);
}
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
System.out.println("请输入算式:");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();//输入算式
MyBC b = new MyBC();
str = b.result(str);
String secret= Encorder.AESEncode(key,str);//客户端进行加密
public static void main(String args[]) throws Exception {
String key = "";
int n = -1;
byte[] a = new byte[128];
try {
File f = new File("key1.dat");
InputStream in = new FileInputStream(f);
while ((n = in.read(a, 0, 100)) != -1) {
key = key + new String(a, 0, n);
}
in.close();
} catch (IOException e) {
System.out.println("File read Error" + e);
}
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
System.out.println("请输入算式:");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();//输入算式
MyBC b = new MyBC();
str = b.result(str);
String secret= Encorder.AESEncode(key,str);//客户端进行加密
try {
mysocket = new Socket("127.1.0.0", 2010);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
out.writeUTF(key);
out.writeUTF(secret);
String s = in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回答:" + s);
Thread.sleep(500);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
}
}
```
服务器
mysocket = new Socket("127.1.0.0", 2010);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
out.writeUTF(key);
out.writeUTF(secret);
String s = in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回答:" + s);
Thread.sleep(500);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
}
}
```
服务器
```
package kd;
package kd;
import kd.Encorder;
import kd.MyDC;
import kd.MyDC;
import java.io.*;
import java.net.*;
public class Server1 {
public static void main (String args[]) throws Exception {
/*String key="";
int n=-1;
byte [] a=new byte[128];
try{ File f=new File("key1.dat");
InputStream in = new FileInputStream(f);
while((n=in.read(a,0,100))!=-1) {
key=key+new String (a,0,n);
}
in.close();
}
catch(IOException e) {
System.out.println("File read Error"+e);
}*/
ServerSocket serverForClient=null;
Socket socketOnServer=null;
DataOutputStream out=null;
DataInputStream in=null;
try { serverForClient = new ServerSocket(2010);
}
catch(IOException e1) {
System.out.println(e1);
}
try{ System.out.println("等待客户呼叫");
socketOnServer = serverForClient.accept(); //堵塞状态,除非有客户呼叫
out=new DataOutputStream(socketOnServer.getOutputStream());
in=new DataInputStream(socketOnServer.getInputStream());
String key = in.readUTF();
String s=in.readUTF(); // in读取信息,堵塞状态
System.out.println("服务器收到的信息:"+s);
String clear= Encorder.AESDncode(key,s);
MyDC d=new MyDC();
System.out.println("服务器收到客户的提问:"+clear);
int answer=d.evaluate(clear);
out.writeUTF(answer+"");
Thread.sleep(500);
}
catch(Exception e) {
System.out.println("客户已断开"+e);
}
}
}
```
#### 实验截图
import java.net.*;
public class Server1 {
public static void main (String args[]) throws Exception {
/*String key="";
int n=-1;
byte [] a=new byte[128];
try{ File f=new File("key1.dat");
InputStream in = new FileInputStream(f);
while((n=in.read(a,0,100))!=-1) {
key=key+new String (a,0,n);
}
in.close();
}
catch(IOException e) {
System.out.println("File read Error"+e);
}*/
ServerSocket serverForClient=null;
Socket socketOnServer=null;
DataOutputStream out=null;
DataInputStream in=null;
try { serverForClient = new ServerSocket(2010);
}
catch(IOException e1) {
System.out.println(e1);
}
try{ System.out.println("等待客户呼叫");
socketOnServer = serverForClient.accept(); //堵塞状态,除非有客户呼叫
out=new DataOutputStream(socketOnServer.getOutputStream());
in=new DataInputStream(socketOnServer.getInputStream());
String key = in.readUTF();
String s=in.readUTF(); // in读取信息,堵塞状态
System.out.println("服务器收到的信息:"+s);
String clear= Encorder.AESDncode(key,s);
MyDC d=new MyDC();
System.out.println("服务器收到客户的提问:"+clear);
int answer=d.evaluate(clear);
out.writeUTF(answer+"");
Thread.sleep(500);
}
catch(Exception e) {
System.out.println("客户已断开"+e);
}
}
}
```
#### 实验截图
## 任务四
1.实验要求:
密钥分发结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文发送给服务器;
(4)客户端和服务器用DH算法进行3DES或AES算法的密钥交换;
(5)服务器接收到后缀表达式表达式后,进行解密,然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(6)客户端显示服务器发送过来的结果;
(7)上传测试结果截图和码云链接;
1.实验要求:
密钥分发结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文发送给服务器;
(4)客户端和服务器用DH算法进行3DES或AES算法的密钥交换;
(5)服务器接收到后缀表达式表达式后,进行解密,然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(6)客户端显示服务器发送过来的结果;
(7)上传测试结果截图和码云链接;
#### 实验过程:
创建密钥对生成器:
KeyPairGenerator kpg=KeyPairGenerator.getInstance("DH");
初始化密钥生成器
kpg.initialize(1024);
生成密钥对
KeyPair kp=kpg.genKeyPair( );
获取公钥和私钥
PublicKey pbkey=kp.getPublic( );
PrivateKey prkey=kp.getPrivate( );
创建密钥协定对象:KeyAgreement ka=KeyAgreement.getInstance("DH");
初始化密钥协定对象:ka.init(prk);
执行密钥协定:ka.doPhase(pbk,true);
生成共享信息:byte[ ] sb=ka.generateSecret();
创建密钥:SecretKeySpec k=new SecretKeySpec(sb,"AES");
KeyPairGenerator kpg=KeyPairGenerator.getInstance("DH");
初始化密钥生成器
kpg.initialize(1024);
生成密钥对
KeyPair kp=kpg.genKeyPair( );
获取公钥和私钥
PublicKey pbkey=kp.getPublic( );
PrivateKey prkey=kp.getPrivate( );
创建密钥协定对象:KeyAgreement ka=KeyAgreement.getInstance("DH");
初始化密钥协定对象:ka.init(prk);
执行密钥协定:ka.doPhase(pbk,true);
生成共享信息:byte[ ] sb=ka.generateSecret();
创建密钥:SecretKeySpec k=new SecretKeySpec(sb,"AES");
实验代码
Key_DH
```
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
Key_DH
```
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
public class Key_DH{
//三个静态变量的定义从
// C:j2sdk-1_4_0-docdocsguidesecurityjceJCERefGuide.html
// 拷贝而来
// The 1024 bit Diffie-Hellman modulus values used by SKIP
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);
//三个静态变量的定义从
// C:j2sdk-1_4_0-docdocsguidesecurityjceJCERefGuide.html
// 拷贝而来
// The 1024 bit Diffie-Hellman modulus values used by SKIP
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();
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);
}
}
```
KeyAgree
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);
}
}
```
KeyAgree
```
import java.security.PublicKey;
import java.security.PrivateKey;
import java.io.*;
import javax.crypto.KeyAgreement;
import javax.crypto.spec.*;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.io.*;
import javax.crypto.KeyAgreement;
import javax.crypto.spec.*;
public class KeyAgree{
public static void main(String args[ ]) throws Exception{
File file=new File("Sharekey.dat");
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream("Apub.dat");
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream("Bpri.dat");
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]+",");
}
OutputStream out=new FileOutputStream(file);
out.write(sb);
out.close();
SecretKeySpec k=new SecretKeySpec(sb,"AES");
}
}
```
Server2
public static void main(String args[ ]) throws Exception{
File file=new File("Sharekey.dat");
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream("Apub.dat");
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream("Bpri.dat");
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]+",");
}
OutputStream out=new FileOutputStream(file);
out.write(sb);
out.close();
SecretKeySpec k=new SecretKeySpec(sb,"AES");
}
}
```
Server2
```
package kd;
package kd;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.*;
public class Server2 {
public static void main(String[] args) {
ServerSocket serverForClient = null;
Socket socketOnServer = null;
DataOutputStream out = null;
DataInputStream in = null;
kd.MyDC myDC = new kd.MyDC();
try{
serverForClient = new ServerSocket(2010);
}catch (IOException e1){
System.out.println("等待客户呼叫"+e1);
}
try{
System.out.println("服务器收到的信息");
socketOnServer = serverForClient.accept();
out = new DataOutputStream(socketOnServer.getOutputStream());
in = new DataInputStream(socketOnServer.getInputStream());
//获取密文
String clength = in.readUTF();
byte []ctext = new byte[Integer.parseInt(clength)];
for(int i=0; i<Integer.parseInt(clength); i++){
String temp = in.readUTF();
ctext[i] = Byte.parseByte(temp);
}
//获取密钥
String keylength = in.readUTF();
byte []ckey = new byte[Integer.parseInt(keylength)];
for(int i=0; i<Integer.parseInt(keylength); i++){
String temp = in.readUTF();
ckey[i] = Byte.parseByte(temp);
}
//密钥解密
SecretKeySpec k1 = kd.KeyAgree.KeyAgree("Clientpub.dat","Serverpri.dat");
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k1);
byte []pkey=cp.doFinal(ckey);
//密文解密
SecretKeySpec k=new SecretKeySpec(pkey,"DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte []ptext=cp.doFinal(ctext);
String suffix = new String(ptext,"UTF8");
System.out.println("服务器提供的解密"+suffix);
out.writeUTF(myDC.evaluate(suffix)+"");
Thread.sleep(500);
}catch (Exception e){
System.out.println("客户已断开");
}
}
}
```
Client2
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.*;
public class Server2 {
public static void main(String[] args) {
ServerSocket serverForClient = null;
Socket socketOnServer = null;
DataOutputStream out = null;
DataInputStream in = null;
kd.MyDC myDC = new kd.MyDC();
try{
serverForClient = new ServerSocket(2010);
}catch (IOException e1){
System.out.println("等待客户呼叫"+e1);
}
try{
System.out.println("服务器收到的信息");
socketOnServer = serverForClient.accept();
out = new DataOutputStream(socketOnServer.getOutputStream());
in = new DataInputStream(socketOnServer.getInputStream());
//获取密文
String clength = in.readUTF();
byte []ctext = new byte[Integer.parseInt(clength)];
for(int i=0; i<Integer.parseInt(clength); i++){
String temp = in.readUTF();
ctext[i] = Byte.parseByte(temp);
}
//获取密钥
String keylength = in.readUTF();
byte []ckey = new byte[Integer.parseInt(keylength)];
for(int i=0; i<Integer.parseInt(keylength); i++){
String temp = in.readUTF();
ckey[i] = Byte.parseByte(temp);
}
//密钥解密
SecretKeySpec k1 = kd.KeyAgree.KeyAgree("Clientpub.dat","Serverpri.dat");
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k1);
byte []pkey=cp.doFinal(ckey);
//密文解密
SecretKeySpec k=new SecretKeySpec(pkey,"DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte []ptext=cp.doFinal(ctext);
String suffix = new String(ptext,"UTF8");
System.out.println("服务器提供的解密"+suffix);
out.writeUTF(myDC.evaluate(suffix)+"");
Thread.sleep(500);
}catch (Exception e){
System.out.println("客户已断开");
}
}
}
```
Client2
```
package kd;
package kd;
import javax.crypto.spec.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client2 {
public static void main(String[] args) {
Scanner inn = new Scanner(System.in);
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
try {
mysocket = new Socket("127.1.0.0", 2010);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(168);
SecretKey k = kg.generateKey();
byte[] kb = k.getEncoded();
System.out.println("请输入中缀表达式:");
String infix = inn.nextLine();
kd.MyBC1 myBC1 = new kd.MyBC1();
String suffix = kd.MyBC1.infixToSuffix(infix);
System.out.println(suffix);
//中缀表达式加密
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[] = suffix.getBytes("UTF8");
byte ctext[] = cp.doFinal(ptext);
out.writeUTF(ctext.length + "");
for (int i = 0; i < ctext.length; i++) {
out.writeUTF(ctext[i] + "");
}
//对密钥进行加密
kd.KeyAgree keyAgree = new kd.KeyAgree();
SecretKeySpec k1 = keyAgree.KeyAgree("Serverpub.dat","Clientpri.dat");
cp.init(Cipher.ENCRYPT_MODE, k1);
byte ckey[] = cp.doFinal(kb);
out.writeUTF(ckey.length + "");
for (int i = 0; i < ckey.length; i++) {
out.writeUTF(ckey[i] + "");
}
String result = in.readUTF();
System.out.println("收到客户回答" + result);
Thread.sleep(500);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
}
}
public static void main(String[] args) {
Scanner inn = new Scanner(System.in);
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
try {
mysocket = new Socket("127.1.0.0", 2010);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(168);
SecretKey k = kg.generateKey();
byte[] kb = k.getEncoded();
System.out.println("请输入中缀表达式:");
String infix = inn.nextLine();
kd.MyBC1 myBC1 = new kd.MyBC1();
String suffix = kd.MyBC1.infixToSuffix(infix);
System.out.println(suffix);
//中缀表达式加密
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[] = suffix.getBytes("UTF8");
byte ctext[] = cp.doFinal(ptext);
out.writeUTF(ctext.length + "");
for (int i = 0; i < ctext.length; i++) {
out.writeUTF(ctext[i] + "");
}
//对密钥进行加密
kd.KeyAgree keyAgree = new kd.KeyAgree();
SecretKeySpec k1 = keyAgree.KeyAgree("Serverpub.dat","Clientpri.dat");
cp.init(Cipher.ENCRYPT_MODE, k1);
byte ckey[] = cp.doFinal(kb);
out.writeUTF(ckey.length + "");
for (int i = 0; i < ckey.length; i++) {
out.writeUTF(ckey[i] + "");
}
String result = in.readUTF();
System.out.println("收到客户回答" + result);
Thread.sleep(500);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
}
}
```
#### 实验截图
#### 实验截图
## 任务五
1.实验要求:
完整性校验结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器;
(4)客户端和服务器用DH算法进行3DES或AES算法的密钥交换;
(5)服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(6)客户端显示服务器发送过来的结果;
(7)上传测试结果截图和码云链接;
#### 实验代码
Skey_AES
```
package kd;
1.实验要求:
完整性校验结对编程:1人负责客户端,一人负责服务器;
(1)注意责任归宿,要会通过测试证明自己没有问题;
(2)基于Java Socket实现客户端/服务器功能,传输方式用TCP;
(3)客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器;
(4)客户端和服务器用DH算法进行3DES或AES算法的密钥交换;
(5)服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端;
(6)客户端显示服务器发送过来的结果;
(7)上传测试结果截图和码云链接;
#### 实验代码
Skey_AES
```
package kd;
import java.io.*;
import javax.crypto.*;
public class Skey_AES{
public static void main(String args[]) throws Exception{
KeyGenerator kg=KeyGenerator.getInstance("AES");
kg.init(128);
SecretKey k=kg.generateKey( );
FileOutputStream f=new FileOutputStream("key1.dat");
ObjectOutputStream b=new ObjectOutputStream(f);
b.writeObject(k);
}
}
```
#### 实验截图
import javax.crypto.*;
public class Skey_AES{
public static void main(String args[]) throws Exception{
KeyGenerator kg=KeyGenerator.getInstance("AES");
kg.init(128);
SecretKey k=kg.generateKey( );
FileOutputStream f=new FileOutputStream("key1.dat");
ObjectOutputStream b=new ObjectOutputStream(f);
b.writeObject(k);
}
}
```
#### 实验截图
## 实验总结
本次实验基本上是教材十三章和之前学的Java密码学算法的实际应用,在之前实验三的基础上加上了一些内容,通过这次实验复习了相关知识,理解了知识点,真正实践起来却还是出现了许多问题。
码云链接:
本次实验基本上是教材十三章和之前学的Java密码学算法的实际应用,在之前实验三的基础上加上了一些内容,通过这次实验复习了相关知识,理解了知识点,真正实践起来却还是出现了许多问题。
码云链接: