2017-2018-2 20165218 實驗五《網絡編程與安全》實驗報告

實驗五《網絡編程與安全》實驗報告

課程:java程序設計

姓名:趙冰雨

學號:20165218

指導教師:婁嘉鵬

實驗日期:2018.5.29

實驗內容、步驟與體會:

任務一

結對實現中綴表達式轉後綴表達式的功能 MyBC.java
結對實現從上面功能中獲取的表達式中實現後綴表達式求值的功能,調用MyDC.javajava

  1. 中綴轉後綴
  • 設置一個運算符棧,一個後綴表達式字符串
  • 從左到右一次對中綴表達式中的每一個字符進行以下處理:
    • 若ch是左括號(,入棧
    • 若是ch是數字,將其後數字添加到後綴表達式字符串以後,並添加空格
    • 若是ch是運算符,將棧頂若干優先級高於ch的運算符出棧,添加到後綴表達式字符串以後,再將ch入棧。當(運算符在棧中時,它的優先級最低
    • 若ch是),則若干運算符所有出棧,直到出棧的是左括號,一對括號匹配
  • 若表達式結束,將棧中運算符所有出棧,添加到後綴表達式字符串以後
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.lang.String;
import org.junit.Test;

public class MyBC{
    private static final Map<Character, Integer> basic = new HashMap<Character, Integer>();
    static {
        basic.put('-', 1);
        basic.put('+', 1);
        basic.put('*', 2);
        basic.put('/', 2);
        basic.put('(', 0);
    }

    /**
     * 將  中綴表達式  轉化爲  後綴表達式
     */
    public static String toSuffix(String infix){
        List<String> queue = new ArrayList<String>();
        List<Character> stack = new ArrayList<Character>();

        char[] charArr = infix.trim().toCharArray();
        String standard = "*/+-()";
        char ch = '&';
        int len = 0;
        for (int i = 0; i < charArr.length; i++) {

            ch = charArr[i];
            if(Character.isDigit(ch)) {
                len++;
            }else if(Character.isLetter(ch)) {
                len++;
            }else if(ch == '.'){
                len++;
            }else if(Character.isSpaceChar(ch)) {
                if(len > 0) {
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i)));
                    len = 0;
                }
                continue;
            }else if(standard.indexOf(ch) != -1) {
                if(len > 0) {
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i)));
                    len = 0;
                }
                if(ch == '(') {
                    stack.add(ch);
                    continue;
                }
                if (!stack.isEmpty()) {
                    int size = stack.size() - 1;
                    boolean flag = false;
                    while (size >= 0 && ch == ')' && stack.get(size) != '(') {
                        queue.add(String.valueOf(stack.remove(size)));
                        size--;
                        flag = true;
                    }
                    while (size >= 0 && !flag && basic.get(stack.get(size)) >= basic.get(ch)) {
                        queue.add(String.valueOf(stack.remove(size)));
                        size--;
                    }
                }
                if(ch != ')') {
                    stack.add(ch);
                } else {
                    stack.remove(stack.size() - 1);
                }
            }
            if(i == charArr.length - 1) {
                if(len > 0) {
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len+1, i+1)));
                }
                int size = stack.size() - 1;
                while (size >= 0) {
                    queue.add(String.valueOf(stack.remove(size)));
                    size--;
                }
            }
        }
        return queue.stream().collect(Collectors.joining(" "));
    }
}
  1. 後綴表達式求值
  • 設置一個操做數棧,從左向右依次對後綴表達式字符串中的每一個字符ch進行處理
  • 若ch是數字,先將其後連續若干數字轉化爲整數,再將該整數入棧;
  • 若ch是運算符,出棧兩個值進行運算,運算結果再入棧;
  • 重複以上步驟,直至後綴表達式結束,棧中最後一個數字就是所求表達式的值
import java.util.StringTokenizer;
import java.util.Stack;
public class MyDC {
    /**
     * constant for addition symbol
     */
    private final char ADD = '+';
    /**
     * constant for subtraction symbol
     */
    private final char SUBTRACT = '-';
    /**
     * constant for multiplication symbol
     */
    private final char MULTIPLY = '*';
    /**
     * constant for division symbol
     */
    private final char DIVIDE = '/';
    /**
     * the stack
     */
    private Stack<Integer> stack;

    /**
     * Sets up this evalutor by creating a new stack.
     */
    public MyDC() {
        stack = new Stack<Integer>();
    }

    public int evaluate(String expr) {
        int op1, op2, result = 0;
        String token;
        StringTokenizer tokenizer = new StringTokenizer(expr);

        while (tokenizer.hasMoreTokens()) {
            token = tokenizer.nextToken();

            if (isOperator(token)) {  //若是是運算符,調用isOperator
                op2 = (stack.pop()).intValue(); //從棧中彈出操做數2
                op1 = (stack.pop()).intValue();//從棧中彈出操做數1
                result = evalSingleOp(token.charAt(0), op1, op2);
                //根據運算符和兩個操做數調用evalSingleOp計算result;
                stack.push(new Integer(result)); //計算result入棧;
            } else//若是是操做數

                stack.push(new Integer(Integer.parseInt(token)));        //操做數入棧;
        }

        return result;
    }

    private boolean isOperator(String token) {
        return (token.equals("+") || token.equals("-") ||
                token.equals("*") || token.equals("/"));
    }

    private int evalSingleOp(char operation, int op1, int op2) {
        int result = 0;

        switch (operation) {
            case ADD:
                result = op1 + op2;
                break;
            case SUBTRACT:
                result = op1 - op2;
                break;
            case MULTIPLY:
                result = op1 * op2;
                break;
            case DIVIDE:
                result = op1 / op2;
        }

        return result;
    }
}


任務二

1人負責客戶端,一人負責服務器
注意責任歸宿,要會經過測試證實本身沒有問題
基於Java Socket實現客戶端/服務器功能,傳輸方式用TCP
客戶端讓用戶輸入中綴表達式,而後把中綴表達式調用MyBC.java的功能轉化爲後綴表達式,把後綴表達式經過網絡發送給服務器
服務器接收到後綴表達式,調用MyDC.java的功能計算後綴表達式的值,把結果發送給客戶端
客戶端顯示服務器發送過來的結果git

  • 服務器代碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Service {
    public static void main(String[] args) throws IOException{
        Service socketService = new Service();
        socketService.oneServer();
    }
    public  void oneServer(){
        try{
            ServerSocket server=null;
            try{
                server=new ServerSocket(5218);
                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,line2;
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer=new PrintWriter(socket.getOutputStream());
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            line2=in.readLine();
            System.out.println("客戶端:"+line2);
            MyDC f = new MyDC();
            System.out.printf("%d",f.evaluate(line2));
            writer.println(Integer.toString(f.evaluate(line2)));
            line=br.readLine();
            while(!line.equals("end")){
                writer.println(line);
                writer.flush();
                System.out.println("服務器:"+Integer.toString(f.evaluate(in.readLine())));
                System.out.println("客戶端:"+in.readLine());
                line=br.readLine();
            }
            writer.close();
            in.close();
            socket.close();
            server.close();
        }catch(Exception e) {
            System.out.println("Error."+e);
        }
    }
}
  • 客戶端代碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;

public class Client {
    public static void main(String[] args) throws IOException {
        try {
            //Socket socket = new Socket("172.20.10.6", 5218);
            Socket socket = new Socket("172.20.10.6", 5218);
            System.out.println("客戶端啓動成功!");
            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 expression;

            MyBC bc = new MyBC();
            expression = br.readLine();
            String input =new String();
            input = bc.toSuffix("20-16/4+52-18*2");
                                //學號是:20165218(20)-(16)/4+(53)-(1)*2
            while (!expression.equals("end")) {
                write.println(input);
                write.println(expression);
                write.flush();
                System.out.println("轉化的後綴表達式爲:" + input);
                System.out.println("服務器返回值爲:" + in.readLine());
                expression = br.readLine();
            }
            write.close();
            in.close();
            socket.close();
        } catch (Exception e) {
            System.out.println("沒法監聽:" + e);
        }
    }
}


任務三

加密結對編程:1人負責客戶端,一人負責服務器算法

  1. 注意責任歸宿,要會經過測試證實本身沒有問題
  2. 基於Java Socket實現客戶端/服務器功能,傳輸方式用TCP
  3. 客戶端讓用戶輸入中綴表達式,而後把中綴表達式調用MyBC.java的功能轉化爲後綴表達式,把後綴表達式用3DES或AES算法加密後經過網絡把密文發送給服務器
  4. 服務器接收到後綴表達式表達式後,進行解密(和客戶端協商密鑰,能夠用數組保存),而後調用MyDC.java的功能計算後綴表達式的值,把結果發送給客戶端
  5. 客戶端顯示服務器發送過來的結果
  • AES算法代碼
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AesEncodeUtil {

    //初始向量
    public static final String VIPARA = "aabbccddeeffgghh";   //AES 爲16bytes. DES 爲8bytes

    //編碼方式
    public static final String bm = "UTF-8";

    //私鑰
    private static final String ASE_KEY = "aabbccddeeffgghh";   //AES固定格式爲128/192/256 bits.即:16/24/32bytes。DES固定格式爲128bits,即8bytes。

    /**
     * 加密
     *
     * @param cleartext
     * @return
     */
    public static String encrypt(String cleartext) {
        //加密方式: AES128(CBC/PKCS5Padding) + Base64, 私鑰:aabbccddeeffgghh
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            //兩個參數,第一個爲私鑰字節數組, 第二個爲加密方式 AES或者DES
            SecretKeySpec key = new SecretKeySpec(ASE_KEY.getBytes(), "AES");
            //實例化加密類,參數爲加密方式,要寫全
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //PKCS5Padding比PKCS7Padding效率高,PKCS7Padding可支持IOS加解密
            //初始化,此方法能夠採用三種方式,按加密算法要求來添加。(1)無第三個參數(2)第三個參數爲SecureRandom random = new SecureRandom();中random對象,隨機數。(AES不可採用這種方法)(3)採用此代碼中的IVParameterSpec
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            //加密操做,返回加密後的字節數組,而後須要編碼。主要編解碼方式有Base64, HEX, UUE,7bit等等。此處看服務器須要什麼編碼方式
            byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));

            return new BASE64Encoder().encode(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 解密
     *
     * @param encrypted
     * @return
     */
    public static String decrypt(String encrypted) {
        try {
            byte[] byteMi = new BASE64Decoder().decodeBuffer(encrypted);
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(
                    ASE_KEY.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            //與加密時不一樣MODE:Cipher.DECRYPT_MODE
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, bm);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
  • 服務器代碼
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServiceAes {
    public static void main(String[] args) throws IOException{
        Service socketService = new Service();
        socketService.oneServer();
    }
    public  void oneServer(){
        try{
            ServerSocket server=null;
            try{
                server=new ServerSocket(5223);
                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,line2;
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer=new PrintWriter(socket.getOutputStream());
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            line2=in.readLine();
            System.out.println("客戶端:"+line2);

            //建立對象aes,並調用 AesEncodeUtil 類中的解密方法
            AesEncodeUtil aes = new AesEncodeUtil();
            String line3 = new String();
            line3 = aes.decrypt(line2);
            //調用 MyDC 類計算值
            MyDC f = new MyDC();
            System.out.printf("%d",f.evaluate(line3));
            writer.println(Integer.toString(f.evaluate(line3)));
            line=br.readLine();
            while(!line.equals("end")){
                writer.println(line);
                writer.flush();
                System.out.println("客戶端:"+in.readLine());
                System.out.println("服務器:"+Integer.toString(f.evaluate(in.readLine())));
                line=br.readLine();
            }
            writer.close();
            in.close();
            socket.close();
            server.close();
        }catch(Exception e) {
            System.out.println("Error."+e);
        }
    }
}
  • 客戶端代碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;

public class ClientAes {
    public static void main(String[] args) throws IOException {
        try {
            //Socket socket = new Socket("10.1.1.234", 5218);
            //Socket socket = new Socket("10.1.1.230", 5223);
            Socket socket = new Socket("172.20.10.2", 5223);
            System.out.println("客戶端啓動成功!");
            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 expression;

            MyBC bc = new MyBC();
            expression = br.readLine();
            String input =new String();
            String input2 = new String();

            input = bc.toSuffix("1+(4*2)/2+5+4*3-4");
            AesEncodeUtil aes = new AesEncodeUtil();
            input2 = aes.encrypt(input);
            //建立對象aes,並調用 AesEncodeUtil 類中的加密方法
            while (!expression.equals("end")) {
                write.println(input2);
                write.println(expression);
                write.flush();
                System.out.println("aes加密的後綴表達式爲:" );
                System.out.println("服務器返回值爲:" + in.readLine());
                expression = br.readLine();
            }
            write.close();
            in.close();
            socket.close();
        } catch (Exception e) {
            System.out.println("沒法監聽:" + e);
        }
    }
}


任務四

密鑰分發結對編程:1人負責客戶端,一人負責服務器express

  1. 注意責任歸宿,要會經過測試證實本身沒有問題
  2. 基於Java Socket實現客戶端/服務器功能,傳輸方式用TCP
  3. 客戶端讓用戶輸入中綴表達式,而後把中綴表達式調用MyBC.java的功能轉化爲後綴表達式,把後綴表達式用3DES或AES算法加密經過網絡把密文發送給服務器
  4. 客戶端和服務器用DH算法進行3DES或AES算法的密鑰交換
  5. 服務器接收到後綴表達式表達式後,進行解密,而後調用MyDC.java的功能計算後綴表達式的值,把結果發送給客戶端
  6. 客戶端顯示服務器發送過來的結果
  • 執行Diffie-Hellman算法
    • 建立DH公鑰和私鑰
    • 建立共享密鑰
  • 建立DH公鑰和私鑰
public class Key_DH{
    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();

        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);
    }
}

代碼託管地址

相關文章
相關標籤/搜索