标签:write pre i++ valueof 责任 log question 保存 自己的
两人一组结对编程:
上传测试代码运行结果截图和码云链接
中缀转后缀以及后缀式计算的内容从结对到选做题已完成多次,这里不做阐述,详情参见 结对博客
结对编程:1人负责客户端,一人负责服务器
上传测试结果截图和码云链接
从任务二开始真正进入到了网络编程部分的内容。确定IP地址后,我们要运用第十三章学到的知识。建立套接字传输见下图:
IP地址获取:
具体步骤可参见下方代码
package task_2;
import task_1.MyBC;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Socket mySocket;
DataInputStream in = null;
DataOutputStream out = null;
try{
mySocket =new Socket("172.20.10.7",5303);
in = new DataInputStream(mySocket.getInputStream());
out = new DataOutputStream(mySocket.getOutputStream());
System.out.println("请输入中缀表达式");
String question = scanner.nextLine();
MyBC myBC = new MyBC();
String suffixquestion = myBC.toSuffix(question);
out.writeUTF(suffixquestion);
String result = in.readUTF();
System.out.println("客户端收到答案:"+result);
Thread.sleep(500);
}
catch (Exception e){
System.out.println("服务器已断开");
}
}
}
package task_2;
import task_1.MyDC;
import java.io.*;
import java.net.*;
import java.util.*;
public class Sever {
public static void main(String[] args) {
ServerSocket serverSocket =null;
Socket socket = null;
DataOutputStream out = null;
DataInputStream in =null;
try{
serverSocket =new ServerSocket(5303);
}
catch (IOException e){
System.out.println(e);
}
try{
System.out.println("等待客户呼叫");
socket = serverSocket.accept();
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
String suffixquestion = in.readUTF();
MyDC myDC= new MyDC();
String result = myDC.evaluate(suffixquestion);
out.writeUTF(result);
System.out.println("已将答案发回至客户端");
Thread.sleep(500);
}
catch (Exception e){
System.out.println("客户已断开连接");
}
}
}
加密结对编程:1人负责客户端,一人负责服务器
上传测试结果截图和码云链接
从任务三开始,逐渐加入密码学的内容。对传输内容进行加密。步骤如下(省略任务二中计算的):
KeyGenerator kg=KeyGenerator.getInstance("DESede");
kg.init(168);
SecretKey k=kg.generateKey( );
FileOutputStream f=new FileOutputStream("key1.dat");
ObjectOutputStream b=new ObjectOutputStream(f);
b.writeObject(k);
任务三中,密钥由双方协商获得,因此该生成密钥文件由双方共同保存。
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 ctext[]=cp.doFinal(ptext);
(ptext是byte数组形式的明文)out.writeUTF(ctext.length+"")
for(int i=0; i<ctext.length; i++){
out.writeUTF(ctext[i]+"");
}
cp.init(Cipher.DECRYPT_MODE, k);
int length = Integer.valueOf(in.readUTF());
for(int i =0;i<length;i++){
String temp = in.readUTF();
ctext[i] = Byte.parseByte(temp);
}
byte[] ptext = cp.doFinal(ctext);
将计算好的答案传输回客户端:out.writeUTF(myDC.evaluate(p));
客户端
package task_3;
import task_1.MyBC;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
public class Client {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Socket mySocket;
DataInputStream in = null;
DataOutputStream out = null;
try{
mySocket =new Socket("172.20.10.7",5308);
in = new DataInputStream(mySocket.getInputStream());
out = new DataOutputStream(mySocket.getOutputStream());
System.out.println("请输入中缀表达式");
String question = scanner.nextLine();
MyBC myBC = new MyBC();
String suffixquestion = myBC.toSuffix(question);
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[]=suffixquestion.getBytes("UTF8");
byte ctext[]=cp.doFinal(ptext);
out.writeUTF(ctext.length+"");
for(int i = 0;i<ctext.length;i++){
out.writeUTF(ctext[i]+"");
}
String result =in.readUTF();
System.out.println("收到服务器答案:"+result);
Thread.sleep(500);
}
catch (Exception e){
System.out.println("服务器已断开");
}
}
}
package task_3;
import task_1.MyDC;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class Sever {
public static void main(String[] args) {
ServerSocket serverSocket =null;
Socket socket = null;
DataOutputStream out = null;
DataInputStream in =null;
try{
serverSocket =new ServerSocket(5308);
}
catch (IOException e){
System.out.println(e);
}
try{
MyDC myDC = new MyDC();
System.out.println("等待客户呼叫");
socket = serverSocket.accept();
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
int length = Integer.valueOf(in.readUTF());
byte ctext[] = new byte[length];
for(int i =0;i<length;i++){
String temp = in.readUTF();
ctext[i] = Byte.parseByte(temp);
}
FileInputStream f2 = new FileInputStream("keykb1.dat");
int num2 = f2.available();
byte[] keykb = new byte[num2];
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);
out.writeUTF(myDC.evaluate(p));
System.out.print("已将答案发送至客户端");
Thread.sleep(500);
}
catch (Exception e){
System.out.println("客户已断开连接");
}
}
}
密钥分发结对编程:1人负责客户端,一人负责服务器
上传测试结果截图和码云链接
DH算法是建立在DH公钥和私钥的基础上的, A需要和B共享密钥时,A和B各自生成DH公钥和私钥,公钥对外公布而私钥各自秘密保存。
我的理解是,Sever端和Client端各自生成一对公私钥,除此之外还要用有对方的公钥。这样己方私钥和对方公钥生成的共享密钥是完全相同的,然后Client端用该共享密钥对原密钥进行加密后随密文一并传输到服务端,服务端调用共享密钥先解密出原密钥,然后再执行对密文的解密,最大程度的确保了通信的保密性。
DH算法使用如下:
Key_DH.java
生成Sever端和Client端的公私钥。 FileInputStream f1=new FileInputStream("Severpub.dat");
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream("Clientpri.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();
byte[] sb2 = new byte[24];
for(int i =0;i<24;i++){
sb2[i]=sb[i];
}
SecretKeySpec k1=new SecretKeySpec(sb2,"DESede");
sb2
初始化Cipher对象cp执行加密
package task_4;
import task_1.MyBC;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
public class Client {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
try {
mysocket = new Socket("172.20.10.5", 5308);
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 question = scanner.nextLine();
MyBC myBC = new MyBC();
String suffix = myBC.toSuffix(question);
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] + "");
}
//对密钥进行加密
FileInputStream f1=new FileInputStream("Severpub.dat");
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream("Clientpri.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();
//取共享密钥的24位
byte[] sb2 = new byte[24];
for(int i =0;i<24;i++){
sb2[i]=sb[i];
}
System.out.println("共享密钥为:");
for(int i =0;i<sb2.length;i++){
System.out.print(sb2[i]+" ");
}
SecretKeySpec k1=new SecretKeySpec(sb2,"DESede");
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] + "");
}
System.out.println();
String result = in.readUTF();
System.out.println("客户端收到回答" + result);
Thread.sleep(500);
} catch (Exception e) {
System.out.println("服务器连接断开" + e);
}
}
}
package task_4;
import task_1.MyDC;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
public class Sever {
public static void main(String[] args) {
ServerSocket serverForClient = null;
Socket socketOnServer = null;
DataOutputStream out = null;
DataInputStream in = null;
MyDC myDC = new MyDC();
try{
serverForClient = new ServerSocket(5308);
}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);
}
FileInputStream f1=new FileInputStream("Clientpub.dat");
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream("Severpri.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();
byte[] sb2 = new byte[24];
for(int i =0;i<24;i++){
sb2[i]=sb[i];
}
System.out.println("共享密钥为:");
for(int i =0;i<sb2.length;i++){
System.out.print(sb2[i]+" ");
}
SecretKeySpec k1=new SecretKeySpec(sb2,"DESede");
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();
System.out.println("客户端收到提问:"+suffix);
out.writeUTF(myDC.evaluate(suffix)+"");
System.out.println("已将结果发送至客户端");
Thread.sleep(500);
}catch (Exception e){
System.out.println("连接断开"+e);
}
}
}
完整性校验结对编程:1人负责客户端,一人负责服务器
上传测试结果截图和码云链接
MessageDigest m=MessageDigest.getInstance("MD5")
m.update(x.getBytes("UTF8" ))
byte s[ ]=m.digest( )
处理计算结果
MessageDigest m = MessageDigest.getInstance("MD5");
m.update(suffix.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);
}
在任务四的进行过程中,我运行代码后,打印共享密钥后就会退出程序,且不返回结果。
我们首先发现共享密钥明显过长,添加错误描述后发现错误原因为:java.security.InvalidKeyException: Wrong key size
即对密钥加密时的不满足DES算法的标准密钥长度,所以每次程序执行到给原密钥加密的步骤就会失败。
经过翻找网上资料,询问同学,我发现该共享密钥只取前24位。只需要建立另一个byte数组将其取出即可。
本次实验结合了网络编程、密码学概念、结对编程等多种元素,算得上是一次综合性非常强的实验。在实验过程中,多个所学内容的整合让我对所学内容充分利用,让所学的知识除了理论性更增添了实践性。
标签:write pre i++ valueof 责任 log question 保存 自己的
原文地址:https://www.cnblogs.com/darklord0604/p/10957826.html