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


    一、实验准备情况概述

    • 结对对象:20175209王梓鸿
    • 之前的结对项目已经完成过四则运算的内容,因此在完成实验时没有完全使用博客链接中给定的代码,基本采用之前结对项目的代码,并且结对项目中的四则运算还可以实现分数的运算,更加全面。
    • 在完成实验的过程中需要参考实验三中Java密码学算法中的内容。

    二、实验内容及步骤

    • 1.提交1

      • 实验要求:
      结对实现中缀表达式转后缀表达式的功能 MyBC.java
      结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能,调用MyDC.java
      上传测试代码运行结果截图和码云链接
      
      • 实验原理

        • 我们习惯使用的计算表达式称作中缀表达式,但计算机在计算时不使用中缀表达式;
        • 因为中缀表达式需要计算机遇到符号后向后扫描一位 若为括号或优先级更高的操作符还需要向后继续扫描;
        • 而后缀表达式严格按照从左到右进行计算的模式 符合计算机运行方式;
        • 因此表达式求值分为两部分:中缀转后缀、后缀表达式求值。
      • 实验代码

        • 中缀转后缀MyBC.java

          • 伪代码

    1、设立空栈用于存放操作符
    2、从左到右扫描中缀式,若遇到操作数,直接输出,并以空格作为分隔符;
    3、若遇到运算符,则与栈顶元素比较,比栈顶级别高则进栈,否则退出栈顶元素并输出,并以空格作为分隔符,优先级从高到低为')' '/' (÷*) (+-) ;
    4、若遇到左括号,进栈;若遇到右括号,则一直退栈输出,直到退到左括号止;
    5、当栈变成空时,输出的结果即为后缀表达式。

    
             - 产品代码
             ```
    import java.util.Stack;
    public class MyBC {
        String C = new String();
        String End = "";            //存储数字的字符串
        public void ChangeString(String str) {
        C = str;
        }
        public String ChangeOrder() {
            Stack store = new Stack();     //创建一个存储字符的栈
            for (int i = 0; i < C.length(); i++) {
                char op = C.charAt(i);     //将索引值为i处的字符的值返回
                if (op >= '0' && op <= '9') {
                    End = End + op;
                }
                else if (op == '(') {
                    store.push(op);
                }
                else if (op == '+' || op == '-' || op == '*' || op == '÷'|| op == '/') {
                    End = End + " ";
                    if (store.empty()) {
                        store.push(op);
                    }
                    else if (compareValue(op) > compareValue((char) store.peek())) {     //比较运算符优先级
                        store.push(op);
                    }
                    else {
                        End = End + String.valueOf(store.pop());
                        i--;
                    }
                }
                else if (op == ')') {
                    while ((char) store.peek() != '(') {
                        End = End + " " + String.valueOf(store.pop());
                    }
                store.pop();
                }
            }
            while (!store.empty()) {
                End = End + " " + String.valueOf(store.pop());
            }
            return End;
        }
        public int compareValue(char chi) {
            int number = 0;
            switch (chi) {
            case '(':
                number = 1;
                break;
            case '+':
            case '-':
                number = 2;
                break;
            case '*':
            case '÷':
                number = 3;
                break;
            case '/':
                number = 4;
                break;
            case ')':
                number = 5;
                break;
            default:
                number = 0;
                break;
            }
            return number;
        }
    }
    
      - 后缀表达式计算MyDC.java
    
         - 伪代码
    
         ```
    

    1、设置一个空栈用于存储数字元素;
    2、从左到右扫描后缀表达式,遇操作数,进栈;
    3、若遇运算符,则从栈顶弹出两个操作数,执行对应的运算,并将运算后的结果压栈栈;
    4、重复执行直至后缀表达式扫描完毕,弹出栈中的最后一个元素即为表达式的值。

    
             - 产品代码
    
             ```
    import java.util.StringTokenizer;
    import java.util.Stack;
    public class MyDC {
        String q;
        Stack stack;
        public MyDC() {
            stack = new Stack();
        }
        void set(String question) {   //输入后续排列的字符串
            q = question;
        }
        public Rational get() {
            Rational op1 = new Rational();
            Rational op2 = new Rational();
            Rational result = new Rational();
            result.setNumerator(0);
            StringTokenizer token = new StringTokenizer(q, " ");
            String temp;
            while (token.hasMoreTokens()) {
                temp = token.nextToken();
                if (Isop(temp) == 1) {     //遇到操作符,弹出栈顶的两个数进行运算
                    op2 = (Rational) stack.pop();
                    op1 = (Rational) stack.pop();//弹出最上面两个操作数
                    result = cal(temp.charAt(0), op1, op2);//根据运算符进行运算
                    stack.push(result);//将计算结果压栈
                }
                else {
                    Rational num = new Rational();
                    num.setNumerator(Integer.parseInt(temp));
                    stack.push(num);//操作数入栈
                }
            }
            return result;
        }
        Rational cal(char op, Rational a, Rational b) {           //对栈顶弹出的两个数进行运算
            Rational c = new Rational();
            switch (op) {
                case '+':
                    c = a.add(b);
                    break;
                case '-':
                    c = a.sub(b);
                    break;
                case '*':
                    c = a.muti(b);
                    break;
                case '÷':
                case '/':
                    if(b.getNumerator()==0) {
                        System.out.println("生成的算式计算时出现了分母为0的情况!");
                        System.exit(0);
                    }
                    else {
                        c = a.div(b);
                        break;
                    }
                default:
                    System.out.println("Wrong!");
            }
            return c;
        }
        int Isop(String op) {       //判断是不是运算符
            if (op.equals("+") || op.equals("-") || op.equals("*") || op.equals("÷") || op.equals("/")) {
                return 1;
            }
            else {
                return 0;
            }
        }
    }
    
      - 有理数类Rational
         - 用于实现分数运算
    
         ```
    

    public class Rational {
    int numerator = 1 ; //分子
    int denominator = 1; //分母
    void setNumerator(int a) { //设置分子
    int c=f(Math.abs(a),denominator); //计算最大公约数
    numerator = a/c;
    denominator = denominator/c;
    if(numerator<0&&denominator<0) {
    numerator = -numerator;
    denominator = -denominator;
    }
    }
    void setDenominator(int b) { //设置分母
    int c=f(numerator,Math.abs(b)); //计算最大公约数
    numerator = numerator/c;
    denominator = b/c;
    if(numerator<0&&denominator<0) {
    numerator = -numerator;
    denominator = -denominator;
    }
    }
    int getNumerator() {
    return numerator;
    }
    int getDenominator() {
    return denominator;
    }
    int f(int a,int b) { //求a和b的最大公约数
    if(a0) {
    return 1;
    }
    if(a<b) {
    int c=a;
    a=b;
    b=c;
    }
    int r=a%b;
    while(r!=0) {
    a=b;
    b=r;
    r=a%b;
    }
    return b;
    }
    Rational add(Rational r) { //加法运算
    int a=r.getNumerator();
    int b=r.getDenominator();
    int newNumerator=numeratorb+denominatora; //计算出新分子
    int newDenominator=denominatorb; //计算出新分母
    Rational result=new Rational();
    result.setNumerator(newNumerator);
    result.setDenominator(newDenominator);
    return result;
    }
    Rational sub(Rational r) { //减法运算
    int a=r.getNumerator();
    int b=r.getDenominator();
    int newNumerator=numerator
    b-denominatora;
    int newDenominator=denominator
    b;
    Rational result=new Rational();
    result.setNumerator(newNumerator);
    result.setDenominator(newDenominator);
    return result;
    }
    Rational muti(Rational r) { //乘法运算
    int a=r.getNumerator();
    int b=r.getDenominator();
    int newNumerator=numeratora;
    int newDenominator=denominator
    b;
    Rational result=new Rational();
    result.setNumerator(newNumerator);
    result.setDenominator(newDenominator);
    return result;
    }
    Rational div(Rational r) { //除法运算
    int a=r.getNumerator();
    int b=r.getDenominator();
    int newNumerator=numeratorb;
    int newDenominator=denominator
    a;
    Rational result=new Rational();
    if(a
    0) {
    System.out.println("该算式无解");
    result.setNumerator(0);
    }
    else {
    result.setNumerator(newNumerator);
    result.setDenominator(newDenominator);
    }
    return result;
    }
    }

    
          - 测试代码Test
    
             ```
    import java.util.*;
    public class Test {
        public static void main(String[] args) {
            String question = "";
            String question1 = "";
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入题目:");
            question = scanner.nextLine();
            MyBC change = new MyBC();
            change.ChangeString(question);
            question1 = change.ChangeOrder();
            System.out.println(question1);
            MyDC getanswer = new MyDC();
            getanswer.set(question1);
            Rational answer = getanswer.get();
            int a = answer.getNumerator();
            int b = answer.getDenominator();
            float result = (float)a/b;
            System.out.println("结果为(保留两位小数):");
            System.out.println(String.format("%.2f",result));
        }
    }
    
    • 测试结果截图

    • 2.提交2

      • 实验要求

    1、基于Java Socket实现客户端/服务器功能,传输方式用TCP
    2、客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器
    3、服务器接收到后缀表达式,调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
    4、客户端显示服务器发送过来的结果
    5、上传测试结果截图和码云链接

    
       - 实验原理
    
          - 获取`Client`端的IP地址
             - 打开终端,输入`ipconfig`查看;
             - 在命令行输入IP地址;
             - 调用`InetAddress`类中的`geetByName`方法获取输入的IP地址;
          - 创建一个套接字,可以使用`Socket`的构造方法;
             - 如:`public Socket(java.lang.String host, int port)` 其中,`host`是IP地址,`port`是端口号。
          - 调用`getInputStream()`方法获得一个输入流
          - 调用`getOutputStream()`方法获得一个输出流
          - 服务器端调用`ServerSocke`t实现套接字
          - 使用`accept()`方法连接客户端和服务器的套接字
          - 调用`getInputStream()`方法获得一个输入流
          - 调用`getOutputStream()`方法获得一个输出流
    
       - 实验代码
    
          - 客户端Client
    
          ```
    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
    public class Client {
        public static void main(String args[]) {
            Socket mysocket;
            DataInputStream in=null;
            DataOutputStream out=null;
            Scanner scanner = new Scanner(System.in);
            try{
                System.out.print("输入服务器的IP:");
                String IP = scanner.nextLine();
                InetAddress address=InetAddress.getByName(IP);
                mysocket = new Socket(address, 2010);
                in=new DataInputStream(mysocket.getInputStream());
                out=new DataOutputStream(mysocket.getOutputStream());
                System.out.println("请输入中缀表达式:");
                while (scanner.hasNext()) {
                    String question = scanner.next();
                    MyBC change = new MyBC();
                    change.ChangeString(question);
                    String question1 = change.ChangeOrder();
                    System.out.println("后缀表达式为:"+question1);
                    out.writeUTF(question1);
                    String s=in.readUTF(); //in读取信息,堵塞状态
                    System.out.println("客户收到服务器的回答:"+s);
                    Thread.sleep(500);
                    System.out.println("请输入中缀表达式:");
                }
            }
            catch(Exception e) {
                System.out.println("服务器已断开"+e);
            }
        }
    }
    
      - 服务器Server
    
      ```
    

    import java.io.;
    import java.net.
    ;
    public class Server {
    public static void main(String[] args) {
    ServerSocket serverForClient = null;
    Socket socketOnServer = null;
    DataOutputStream out = null;
    DataInputStream in = null;
    String question = "";
    try {
    serverForClient = new ServerSocket(2010);
    } catch (IOException e) {
    System.out.println(e);
    }
    System.out.println("等待客户呼叫");
    try {
    socketOnServer = serverForClient.accept();
    out = new DataOutputStream(socketOnServer.getOutputStream());
    in = new DataInputStream(socketOnServer.getInputStream());
    while(true) {
    question = in.readUTF(); // in读取信息,堵塞状态
    System.out.println("服务器收到客户传递的后缀表达式为:" + question);
    MyDC getanswer = new MyDC();
    getanswer.set(question);
    Rational answer = getanswer.get();
    int a = answer.getNumerator();
    int b = answer.getDenominator();
    float result = (float) a / b;
    System.out.println("计算出的结果为"+String.format("%.2f",result));
    out.writeUTF(String.format("%.2f",result));
    Thread.sleep(500);
    }
    } catch (Exception e) {
    System.out.println("客户已断开" + e);
    }
    }
    }

    
       - 测试结果截图
    
          - 客户端
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526192848545-417751004.png)
    
          - 服务器
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526192922470-172052464.png)
    
    - 3.提交3
    
       - 实验要求
    

    1、基于Java Socket实现客户端/服务器功能,传输方式用TCP
    2、客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器
    3、服务器接收到后缀表达式表达式后,进行解密(和客户端协商密钥,可以用数组保存),然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
    4、客户端显示服务器发送过来的结果
    5、上传测试结果截图和码云链接

    
       - 实验原理
    
          - 本部分主要参考Java密码学算法中“Java对称加密-DES算法”部分;
          - 但是这次和结对伙伴没有通过对象序列化方式写入文件中,而是直接将密钥进行传递。
    
       - 伪代码
          - 客户端
          ```
    1、获取密钥生成器KeyGenerator kg=KeyGenerator.getInstance("DESede")
    2、初始化密钥生成器kg.init(168);
    3、生成密钥SecretKey k=kg.generateKey( );
    4、获取主要编码格式byte[ ] kb=k.getEncoded( );
    5、传送密钥长度及密钥内容
    6、创建密码器Cipher cp=Cipher.getInstance("DESede");
    7、初始化密码器cp.init(Cipher.ENCRYPT_MODE, k);
    8、获取等待加密的明文byte ptext[]=s.getBytes("UTF8");
    9、执行加密byte []ptext=cp.doFinal(ctext)
    
      - 服务器
    
      ```
    

    1、接收密钥长度
    2、接收密钥内容
    3、创建密码器Cipher cp=Cipher.getInstance("DESede");
    4、初始化密码器cp.init(Cipher.DECRYPT_MODE, k);
    5、执行解密byte []ptext=cp.doFinal(ctext)

    
       - 实验代码
    
          - 客户端Client
          ```
    import java.io.*;
    import java.net.*;
    import javax.crypto.*;
    import java.util.Scanner;
    public class Client {
        public static void main(String args[]) throws Exception{
            Socket mysocket;
            DataInputStream in=null;
            DataOutputStream out=null;
            Scanner scanner = new Scanner(System.in);
            try {
                System.out.print("输入服务器的IP:");
                String IP = scanner.nextLine();
                InetAddress address=InetAddress.getByName(IP);
                mysocket = new Socket(address, 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+ "");
                System.out.println("产生的密钥为");
                for(int i=0;i<kb.length;i++) {
                    System.out.print(kb[i]+ " ");
                    out.writeUTF(kb[i] +"");
                }
                System.out.println("
    请输入中缀表达式:");
                while(scanner.hasNext()) {
                    String question = scanner.next();
                    MyBC change = new MyBC();
                    change.ChangeString(question);
                    String question1 = change.ChangeOrder();
                    System.out.println("后缀表达式为:"+question1);
                    Cipher cp = Cipher.getInstance("DESede");
                    cp.init(Cipher.ENCRYPT_MODE,k);
                    byte ptext[] = question1.getBytes("UTF8");
                    byte ctext[] = cp.doFinal(ptext);
                    out.writeUTF(ctext.length + "");
                    for(int i=0;i<ctext.length;i++) {
                        out.writeUTF(ctext[i] +"");
                    }
                    String s=in.readUTF(); //in读取信息,堵塞状态
                    System.out.println("客户收到服务器的回答:"+s);
                    Thread.sleep(500);
                    System.out.println("请输入中缀表达式:");
                }
            }
            catch (IOException e) {
                System.out.println("服务器已断开"+e);
            }
        }
    }
    
      - 服务器Server
      ```
    

    import java.io.;
    import java.net.
    ;
    import javax.crypto.;
    import javax.crypto.spec.
    ;
    public class Server {
    public static void main(String[] args) throws Exception{
    ServerSocket serverForClient = null;
    Socket socketOnServer = null;
    DataOutputStream out = null;
    DataInputStream in = null;
    try {
    serverForClient = new ServerSocket(2010);
    } catch (IOException e) {
    System.out.println(e);
    }
    System.out.println("等待客户呼叫");
    try {
    socketOnServer = serverForClient.accept();
    out = new DataOutputStream(socketOnServer.getOutputStream());
    in = new DataInputStream(socketOnServer.getInputStream());
    String keylength = in.readUTF();
    byte []kb = new byte[Integer.parseInt(keylength)];
    System.out.println("收到的密钥为:");
    for(int i = 0;i<Integer.parseInt(keylength);i++) {
    String str = in.readUTF();
    kb[i] = Byte.parseByte(str);
    System.out.print(kb[i] + " ");
    }
    while(true) {
    SecretKeySpec k = new SecretKeySpec(kb, "DESede");
    Cipher cp = Cipher.getInstance("DESede");
    cp.init(Cipher.DECRYPT_MODE, k);
    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);
    }
    byte[] ptext = cp.doFinal(ctext);
    String question = new String(ptext,"UTF8");
    System.out.print(" 后缀表达式为:"+ question);
    MyDC getanswer = new MyDC();
    getanswer.set(question);
    Rational answer = getanswer.get();
    int a = answer.getNumerator();
    int b = answer.getDenominator();
    float result = (float) a / b;
    System.out.println(" 计算出的结果为"+String.format("%.2f",result));
    out.writeUTF(String.format("%.2f",result));
    Thread.sleep(500);
    }
    } catch (IOException e) {
    System.out.println("客户已断开" + e);
    }
    }
    }

    
       - 测试结果截图
    
          - 客户端
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526192931897-1111854639.png)
    
          - 服务器
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526192937188-139023498.png)
    
    
    - 4.提交4
    
       - 实验要求
    

    1、基于Java Socket实现客户端/服务器功能,传输方式用TCP
    2、客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文发送给服务器
    3、客户端和服务器用DH算法进行3DES或AES算法的密钥交换
    4、服务器接收到后缀表达式表达式后,进行解密,然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
    5、客户端显示服务器发送过来的结果
    6、上传测试结果截图和码云链接

    
       - 实验原理
    
          - 本部分主要参考Java密码学算法中“使用密钥协定创建共享密钥”部分
          - 对博客中的代码进行了部分更改如下:
             - `Key_DH`和`KeyAgree`主类的参数改为由客户端和服务器传递,并修改方法名
                - `public static void DH(String str1,String str2) throws Exception`
                - `public static void Agree(String str1,String str2)`
             - 客户端和服务器分别产生自己的公钥和私钥,并用过字节数组的形式分别向另一方传递自己的公钥;
             - 客户端和服务器接受对方的公钥后利用自己的私钥创建共享密钥
             - 创建密钥协定对象`KeyAgreement ka=KeyAgreement.getInstance("DH")`;
             - 初始化密钥协定对象`ka.init(prk)`;
             - 执行密钥协定`ka.doPhase(pbk,true)`;
             - 生成共享信息`byte[ ] sb=ka.generateSecret()`;
             - 从文件中读取信息并给出共享密钥
    
    
       - 实验代码
    
          - 客户端Client
          ```
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.*;
    import java.security.*;
    import java.util.Scanner;
    import java.net.*;
    public class Client {
        public static void main(String[] args) {
            Socket mysocket;
            DataInputStream in=null;
            DataOutputStream out=null;
            Scanner scanner = new Scanner(System.in);
            try {
                System.out.print("输入服务器的IP:");
                String IP = scanner.nextLine();
                InetAddress address=InetAddress.getByName(IP);
                mysocket = new Socket(address, 2010);
                in = new DataInputStream(mysocket.getInputStream());
                out = new DataOutputStream(mysocket.getOutputStream());
                Key_DH.DH("Lpub.dat","Lpri.dat");
                FileInputStream my = new FileInputStream("Lpub.dat");
                ObjectInputStream mypub = new ObjectInputStream(my);
                Key kp = (Key) mypub.readObject();
                ByteArrayOutputStream DH = new ByteArrayOutputStream();
                ObjectOutputStream myDH = new ObjectOutputStream(DH);
                myDH.writeObject(kp);
                byte []pub = DH.toByteArray();
                out.writeUTF(pub.length+"");
                for(int i=0;i<pub.length;i++) {
                    out.writeUTF(pub[i]+ "");
                }
                Thread.sleep(1000);
                int length = Integer.parseInt(in.readUTF());
                byte cpub[] = new byte[length];
                for(int i=0;i<length;i++) {
                    String temp = in.readUTF();
                    cpub[i] = Byte.parseByte(temp);
                }
                ByteArrayInputStream ckey1 = new ByteArrayInputStream(cpub);
                ObjectInputStream ckey = new ObjectInputStream(ckey1);
                Key k = (Key) ckey.readObject();
                FileOutputStream f2 = new FileOutputStream("W1pub.dat");
                ObjectOutputStream b2 = new ObjectOutputStream(f2);
                b2.writeObject(k);
                KeyAgree.Agree("W1pub.dat","Lpri.dat");
                FileInputStream f = new FileInputStream("sb.dat");
                byte[] keysb = new byte[24];
                f.read(keysb);
                System.out.println("公共密钥为:");
                for (int i = 0;i<24;i++) {
                    System.out.print(keysb[i]+" ");
                }
                System.out.println("
    请输入中缀表达式:");
                while(scanner.hasNext()) {
                    String question = scanner.next();
                    MyBC change = new MyBC();
                    change.ChangeString(question);
                    String question1 = change.ChangeOrder();
                    System.out.println("后缀表达式为:" + question1);
                    SecretKeySpec k1 = new SecretKeySpec(keysb, "DESede");
                    Cipher cp = Cipher.getInstance("DESede");
                    cp.init(Cipher.ENCRYPT_MODE, k1);
                    byte ptext[] = question1.getBytes("UTF8");
                    byte ctext[] = cp.doFinal(ptext);
                    System.out.println("加密后的后缀表达式为:");
                    for (int i = 0; i < ctext.length; i++) {
                        System.out.print(ctext[i] + " ");
                    }
                    out.writeUTF(ctext.length + "");
                    for (int i = 0; i < ctext.length; i++) {
                        out.writeUTF(ctext[i] + "");
                    }
                    String s=in.readUTF(); //in读取信息,堵塞状态
                    System.out.println("
    客户收到服务器的回答:"+s);
                    Thread.sleep(500);
                    System.out.println("请输入中缀表达式:");
                }
            }
            catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    
      - 服务器Server
      ```
    

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.security.
    ;
    public class Server {
    public static void main(String[] args) throws Exception {
    ServerSocket serverForClient = null;
    Socket socketOnServer = null;
    DataOutputStream out = null;
    DataInputStream in = null;
    try {
    serverForClient = new ServerSocket(2010);
    } catch (IOException e) {
    System.out.println(e);
    }
    System.out.println("等待客户呼叫");
    try {
    socketOnServer = serverForClient.accept();
    out = new DataOutputStream(socketOnServer.getOutputStream());
    in = new DataInputStream(socketOnServer.getInputStream());
    Key_DH.DH("Wpub.dat","Wpri.dat");
    int length = Integer.parseInt(in.readUTF());
    byte cpub[] = new byte[length];
    for(int i=0;i<length;i++) {
    String temp = in.readUTF();
    cpub[i] = Byte.parseByte(temp);
    }
    ByteArrayInputStream ckey1 = new ByteArrayInputStream(cpub);
    ObjectInputStream ckey = new ObjectInputStream(ckey1);
    Key k1 = (Key) ckey.readObject();
    FileOutputStream f2 = new FileOutputStream("Lpub.dat");
    ObjectOutputStream b2 = new ObjectOutputStream(f2);
    b2.writeObject(k1);
    FileInputStream my = new FileInputStream("Wpub.dat");
    ObjectInputStream mypub = new ObjectInputStream(my);
    Key kp = (Key) mypub.readObject();
    ByteArrayOutputStream DH = new ByteArrayOutputStream();
    ObjectOutputStream myDH = new ObjectOutputStream(DH);
    myDH.writeObject(kp);
    byte []pub = DH.toByteArray();
    out.writeUTF(pub.length+"");
    for(int i=0;i<pub.length;i++) {
    out.writeUTF(pub[i]+ "");
    }
    KeyAgree.Agree("Lpub.dat","Wpri.dat");
    FileInputStream f = new FileInputStream("sb.dat");
    byte[] keysb = new byte[24];
    f.read(keysb);
    System.out.println("公共密钥为:");
    for (int i = 0;i<24;i++) {
    System.out.print(keysb[i]+" ");
    }
    while(true) {
    SecretKeySpec k = new SecretKeySpec(keysb, "DESede");
    Cipher cp = Cipher.getInstance("DESede");
    cp.init(Cipher.DECRYPT_MODE, k);
    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);
    }
    byte[] ptext = cp.doFinal(ctext);
    String question = new String(ptext, "UTF8");
    System.out.print(" 解密后的后缀表达式为:" + question);
    MyDC getanswer = new MyDC();
    getanswer.set(question);
    Rational answer = getanswer.get();
    int a = answer.getNumerator();
    int b = answer.getDenominator();
    float result = (float) a / b;
    System.out.println(" 计算出的结果为"+String.format("%.2f",result));
    out.writeUTF(String.format("%.2f",result));
    Thread.sleep(500);
    }
    }
    catch(Exception e) {
    System.out.println("客户已断开" + e);
    }
    }
    }

    
       - 测试结果截图
    
          - 客户端
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526192949092-1435488705.png)
    
          - 服务器
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526192958501-1881793802.png)
    
    - 5.任务五
    
       - 实验要求
    

    1、基于Java Socket实现客户端/服务器功能,传输方式用TCP
    2、客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器
    3、客户端和服务器用DH算法进行3DES或AES算法的密钥交换
    4、服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
    5、客户端显示服务器发送过来的结果
    6、上传测试结果截图和码云链接

    
       - 实验原理
    
          - 本部分主要参考Java密码学算法中“Java摘要算法- MD5”部分;
          - 主要在任务四基础上进行操作。
    
       - 实验代码
    
          - 客户端Client
          ```
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.*;
    import java.net.Socket;
    import java.security.*;
    import java.util.Scanner;
    import java.net.*;
    public class Client {
        public static void main(String[] args) {
            Socket mysocket;
            DataInputStream in=null;
            DataOutputStream out=null;
            Scanner scanner = new Scanner(System.in);
            try {
                System.out.print("输入服务器的IP:");
                String IP = scanner.nextLine();
                InetAddress address=InetAddress.getByName(IP);
                mysocket = new Socket(address, 2010);
                in = new DataInputStream(mysocket.getInputStream());
                out = new DataOutputStream(mysocket.getOutputStream());
                Key_DH.DH("Lpub.dat","Lpri.dat");
                FileInputStream my = new FileInputStream("Lpub.dat");
                ObjectInputStream mypub = new ObjectInputStream(my);
                Key kp = (Key) mypub.readObject();
                ByteArrayOutputStream DH = new ByteArrayOutputStream();
                ObjectOutputStream myDH = new ObjectOutputStream(DH);
                myDH.writeObject(kp);
                byte []pub = DH.toByteArray();
                out.writeUTF(pub.length+"");
                for(int i=0;i<pub.length;i++) {
                    out.writeUTF(pub[i]+ "");
                }
                Thread.sleep(1000);
                int length = Integer.parseInt(in.readUTF());
                byte cpub[] = new byte[length];
                for(int i=0;i<length;i++) {
                    String temp = in.readUTF();
                    cpub[i] = Byte.parseByte(temp);
                }
                ByteArrayInputStream ckey1 = new ByteArrayInputStream(cpub);
                ObjectInputStream ckey = new ObjectInputStream(ckey1);
                Key k = (Key) ckey.readObject();
                FileOutputStream f2 = new FileOutputStream("W1pub.dat");
                ObjectOutputStream b2 = new ObjectOutputStream(f2);
                b2.writeObject(k);
                KeyAgree.Agree("W1pub.dat","Lpri.dat");
                FileInputStream f = new FileInputStream("sb.dat");
                byte[] keysb = new byte[24];
                f.read(keysb);
                System.out.println("公共密钥为:");
                for (int i = 0;i<24;i++) {
                    System.out.print(keysb[i]+" ");
                }
                System.out.println("
    请输入中缀表达式:");
                while(scanner.hasNext()) {
                    String question = scanner.next();
                    MyBC change = new MyBC();
                    change.ChangeString(question);
                    String question1 = change.ChangeOrder();
                    System.out.println("后缀表达式为:" + question1);
                    String mtoMD5 = DigestPass.MD5(question1);
                    System.out.println("明文的MD5值为:"+mtoMD5);
                    out.writeUTF(mtoMD5);
                    SecretKeySpec k1 = new SecretKeySpec(keysb, "DESede");
                    Cipher cp = Cipher.getInstance("DESede");
                    cp.init(Cipher.ENCRYPT_MODE, k1);
                    byte ptext[] = question1.getBytes("UTF8");
                    byte ctext[] = cp.doFinal(ptext);
                    System.out.println("加密后的后缀表达式为:");
                    for (int i = 0; i < ctext.length; i++) {
                        System.out.print(ctext[i] + " ");
                    }
                    out.writeUTF(ctext.length + "");
                    for (int i = 0; i < ctext.length; i++) {
                        out.writeUTF(ctext[i] + "");
                    }
                    String s=in.readUTF(); //in读取信息,堵塞状态
                    System.out.println("
    客户收到服务器的回答:"+s);
                    Thread.sleep(500);
                    System.out.println("请输入中缀表达式:");
                }
            }
            catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    
      - 服务器Server
    
      ```
    

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.security.
    ;
    public class Server {
    public static void main(String[] args) throws Exception {
    ServerSocket serverForClient = null;
    Socket socketOnServer = null;
    DataOutputStream out = null;
    DataInputStream in = null;
    try {
    serverForClient = new ServerSocket(2010);
    } catch (IOException e) {
    System.out.println(e);
    }
    System.out.println("等待客户呼叫");
    try {
    socketOnServer = serverForClient.accept();
    out = new DataOutputStream(socketOnServer.getOutputStream());
    in = new DataInputStream(socketOnServer.getInputStream());
    Key_DH.DH("Wpub.dat","Wpri.dat");
    int length = Integer.parseInt(in.readUTF());
    byte cpub[] = new byte[length];
    for(int i=0;i<length;i++) {
    String temp = in.readUTF();
    cpub[i] = Byte.parseByte(temp);
    }
    ByteArrayInputStream ckey1 = new ByteArrayInputStream(cpub);
    ObjectInputStream ckey = new ObjectInputStream(ckey1);
    Key k1 = (Key) ckey.readObject();
    FileOutputStream f2 = new FileOutputStream("L1pub.dat");
    ObjectOutputStream b2 = new ObjectOutputStream(f2);
    b2.writeObject(k1);
    FileInputStream my = new FileInputStream("Wpub.dat");
    ObjectInputStream mypub = new ObjectInputStream(my);
    Key kp = (Key) mypub.readObject();
    ByteArrayOutputStream DH = new ByteArrayOutputStream();
    ObjectOutputStream myDH = new ObjectOutputStream(DH);
    myDH.writeObject(kp);
    byte []pub = DH.toByteArray();
    out.writeUTF(pub.length+"");
    for(int i=0;i<pub.length;i++) {
    out.writeUTF(pub[i]+ "");
    }
    KeyAgree.Agree("L1pub.dat","Wpri.dat");
    FileInputStream f = new FileInputStream("sb.dat");
    byte[] keysb = new byte[24];
    f.read(keysb);
    System.out.println("公共密钥为:");
    for (int i = 0;i<24;i++) {
    System.out.print(keysb[i]+" ");
    }
    while(true) {
    String c = in.readUTF();
    SecretKeySpec k = new SecretKeySpec(keysb, "DESede");
    Cipher cp = Cipher.getInstance("DESede");
    cp.init(Cipher.DECRYPT_MODE, k);
    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);
    }
    byte[] ptext = cp.doFinal(ctext);
    String question = new String(ptext, "UTF8");
    System.out.println(" 解密后的后缀表达式为:" + question);
    String mtoMD5 = DigestPass.MD5(question);
    System.out.println("MD5的值为"+ mtoMD5);
    if(mtoMD5.equals(c)) {
    System.out.println("传递的MD5值和解密的后缀表达式的MD5值相同,可以解密!");
    MyDC getanswer = new MyDC();
    getanswer.set(question);
    Rational answer = getanswer.get();
    int a = answer.getNumerator();
    int b = answer.getDenominator();
    float result = (float) a / b;
    System.out.println("计算出的结果为"+String.format("%.2f",result));
    out.writeUTF(String.format("%.2f",result));
    }
    else {
    System.out.println("密文有误,不能解密!");
    }
    Thread.sleep(500);
    }
    }
    catch(Exception e) {
    System.out.println("客户已断开" + e);
    }
    }
    }

    
       - 测试结果截图
    
          - 客户端
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526193019664-1810674774.png)
    
          - 服务器
    ![](https://img2018.cnblogs.com/blog/1592121/201905/1592121-20190526193012982-1744692684.png)
    
    ## 三、代码托管
    
    - 码云链接
       - [链接](https://gitee.com/fzlzc/java2019/tree/master/src/%E5%AE%9E%E9%AA%8C%E4%BA%94)
    - 伙伴的码云链接
       - [链接](https://gitee.com/wangzihong/20175209/tree/master/%E5%AE%9E%E9%AA%8C%E4%BA%94)
    
    ## 四、实验心得体会
    
    本次实验综合了教材第十三章和Java密码学的相关内容,在和伙伴的讨论中,对于java的网络和安全方面的编程有了更好地掌握。
    
    ## 五、参考资料
    
    - [2016-2017-2 《Java 程序设计》课堂实践项目-网络与安全](https://www.cnblogs.com/rocedu/p/6766748.html#SECDSA)
    - [Java密码学算法](https://www.cnblogs.com/rocedu/p/6683948.html)
  • 相关阅读:
    selenium python 启动Chrome
    安装selenium python
    网络编程32
    动态生成验证码———MVC版
    C#之发送邮件【模板】+【封装】ZJ版
    【转】解读ASP.NET 5 & MVC6系列(1):ASP.NET 5简介
    Cache,MemCache,Application,Cookie等其它缓存汇总
    点击弹出li所在的序列号
    【转】js实现复制到剪贴板功能,兼容所有浏览器
    前端效果——持续更新。。。
  • 原文地址:https://www.cnblogs.com/fzlzc/p/10927148.html
Copyright © 2020-2023  润新知