2018-2019-20175205 实验五《网络编程与安全》实验报告

时间:2019-05-26 10:16:54   收藏:0   阅读:137

2018-2019-20175205 实验五《网络编程与安全》实验报告

实验步骤

任务一

实验过程

实验代码

import java.util.Stack;

public class MyBC {
    MyBC(){}
    public static String infixToSuffix(String exp){
        Stack<String> s = new Stack<String>();         // 创建操作符堆栈
        String suffix = "";            // 要输出的后缀表达式字符串
        String suffix1 = "";             //上一次的后缀表达式
        String suffix2 = "";
        String str[] = exp.split(" ");
        int length = str.length; // 输入的中缀表达式的长度
        String temp="";
        for (int i = 0; i < length; i++) {            // 对该中缀表达式的每一个字符并进行判断
            switch (str[i]) {
                case " ":break;           // 忽略空格
                case "(":
                    s.push(str[i]);                  // 如果是左括号直接压入堆栈
                    break;
                case "+":
                case "-":
                    if(s.size() != 0){          // 碰到'+' '-',将栈中的所有运算符全部弹出去,直至碰到左括号为止,输出到队列中去
                        temp = s.pop();
                        if (temp.equals("(")) {     // 将左括号放回堆栈,终止循环
                            s.push(temp);
                            s.push(str[i]);
                            break;
                        }
                        else{
                            s.push(str[i]);
                            suffix2 = suffix2 + temp + " ";
                            break;
                        }
                    }
                    else{
                        s.push(str[i]);      // 说明是当前为第一次进入或者其他前面运算都有括号等情况导致栈已经为空,此时需要将符号进栈
                        break;
                    }
                    // 如果是乘号或者除号,则弹出所有序列,直到碰到加好、减号、左括号为止,最后将该操作符压入堆栈
                case "*":
                case "÷":
                    if(s.size()!=0){
                        temp = s.pop();
                        if(temp.equals("+")||temp.equals("-")||temp.equals("(")){
                            s.push(temp);
                            s.push(str[i]);
                            break;
                        }
                        else{
                            s.push(str[i]);
                            suffix2 = suffix2+temp+" ";
                            break;
                        }
                    }
                    else {
                        s.push(str[i]);     //当前为第一次进入或者其他前面运算都有括号等情况导致栈已经为空,此时需要将符号进栈
                        break;
                    }
                    // 如果碰到的是右括号,则距离栈顶的第一个左括号上面的所有运算符弹出栈并抛弃左括号
                case ")":
                    while (!s.isEmpty()) {
                        temp = s.pop();
                        if (temp.equals("(")) {
                            break;
                        } else {
                            suffix2 = suffix2+temp+" ";
                        }
                    }
                    break;
                // 默认情况,如果读取到的是数字,则直接送至输出序列
                default:
                    suffix2 = suffix2+str[i]+" ";
                    break;
            }

        }
        // 如果堆栈不为空,则把剩余运算符一次弹出,送至输出序列
        while (s.size() != 0) {
            suffix2 = suffix2+s.pop()+" ";
        }
        if(suffix1.equals("")){          //第一个题目
            suffix1 = suffix2;
            suffix = suffix2;
        }
        else{
            if(suffix2.equals(suffix1))
                suffix = "";
            else
                suffix = suffix2;
        }
        suffix1 = suffix2;
        return suffix;
    }
}

实验结果

技术分享图片

任务二

实验过程

实验代码

import java.net.*;
public class getHostAddress {
    public static void main(String[] args) {
        try{
            InetAddress hostaddress = InetAddress.getLocalHost();
            System.out.println(hostaddress.toString());
        }catch (UnknownHostException e){
            System.out.println(e);
        }
    }
}
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner inn = new Scanner(System.in);
        Socket mysocket;
        DataInputStream in = null;
        DataOutputStream out = null;
        try{
            mysocket = new Socket("172.30.1.177",2010);
            in = new DataInputStream(mysocket.getInputStream());
            out = new DataOutputStream(mysocket.getOutputStream());
            System.out.println("请输入中缀表达式:");
            String infix = inn.nextLine();
            String suffix = MyBC.infixToSuffix(infix);
            out.writeUTF(suffix);
            String result = in.readUTF();
            System.out.println("小猴收到胖砾的回答"+result);
            Thread.sleep(500);
        }catch (Exception e){
            System.out.println("胖砾又去吃饭了"+e);
        }
    }
}
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import java.io.*;
import java.net.*;
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(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 suffix = in.readUTF();
            System.out.println("胖砾收到小猴的提问"+suffix);
            out.writeUTF(myDC.evaluate(suffix)+"");
            Thread.sleep(500);
        }catch (Exception e){
            System.out.println("小猴已断开连接");
        }
    }
}

实验结果

任务三

实验过程

for(int i=0; i<kb.length; i++){
       out.writeUTF(kb[i]+"");
}
for(int i=0; i<ctext.length; i++){
     out.writeUTF(ctext[i]+"");
}

实验代码

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client1 {
    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.0.0.1",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( );
            out.writeUTF(kb.length+"");
            for(int i=0; i<kb.length; i++){
                out.writeUTF(kb[i]+"");
            }
            System.out.println("请输入中缀表达式:");
            String infix = inn.nextLine();
            MyBC myBC = new MyBC();
            String suffix = myBC.infixToSuffix(infix);
            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]+"");
            }
            String result = in.readUTF();
            System.out.println("小猴收到胖砾的回答"+result);
            Thread.sleep(500);
        }catch (Exception e){
            System.out.println("胖砾又去吃饭了"+e);
        }
    }
}
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.*;
public class Sever1 {
    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(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 keylength = in.readUTF();
            byte []kb = new byte[Integer.parseInt(keylength)];
            for(int i=0; i<Integer.parseInt(keylength); i++){
                String t = in.readUTF();
                kb[i] = Byte.parseByte(t);
            }
            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);
            }
            SecretKeySpec k=new  SecretKeySpec(kb,"DESede");
            Cipher cp=Cipher.getInstance("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("小猴已断开连接");
        }
    }
}

实验结果

任务四

实验过程

实验代码

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;

public class Client4 {
    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.0.0.1", 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();
            MyBC myBC = new MyBC();
            String suffix = myBC.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] + "");
            }
            //对密钥进行加密
            KeyAgree keyAgree = new 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);
        }
    }
}
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.*;
public class Server4 {
    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(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 = 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("小猴已断开连接");
        }
    }
}

实验结果

任务五

实验过程

实验代码

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密码学算法的实际应用,当时学的时候感觉就有点迷糊不知道如何真实应用,通过这次实验不仅复习了相关知识,理解以前不懂的知识点,还了解了另一种通信手段

参考资料

Java密码学算法

原文:https://www.cnblogs.com/orii/p/10925190.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!