20145124實驗五 Java網絡編程及安全

編寫代碼java

1.創建一個Socket對象,用來創建一個端口號與客戶端相連,得到網絡輸入流與輸出流對象的引用。算法

2.使用服務器端RSA的私鑰對DES的密鑰進行解密,對祕鑰進行解密以後使用DES對密文進行解密。服務器

3.計算解密後的hash值來肯定解密是否正確。網絡

注:加密算法、祕鑰、Hash函數計算過程均利用的老師提供的代碼app

客戶端代碼以下:socket

•package exp05;函數

/**測試

  • Created by Administrator on 2016/5/6.
    /
    import java.net.
    ;
    import java.io.;
    import java.security.
    ;
    import javax.crypto.;
    import javax.crypto.spec.
    ;
    import java.security.spec.;
    import javax.crypto.interfaces.
    ;
    import java.security.interfaces.;
    import java.math.
    ;
    /**
  • Created by Think on 2016/5/6.
    */
    public class Client
    {
    public static void main(String srgs[]) throws Exception
    {
    try
    {
    KeyGenerator kg = KeyGenerator.getInstance("DESede");
    kg.init(168);
    SecretKey k = kg.generateKey();
    byte[] ptext2 = k.getEncoded();加密

    // 建立鏈接特定服務器的指定端口的Socket對象
         Socket socket = new Socket("192.168.43.125", 12335);//這裏輸入的是服務器的ip地址和端口號,端口號要注意和服務器保持一致。
    
         // 得到從服務器端來的網絡輸入流
         BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
         // 得到從客戶端向服務器端輸出數據的網絡輸出流
         PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
    
         // 建立鍵盤輸入流,以便客戶端從鍵盤上輸入信息
         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    
         //RSA算法,使用服務器端的公鑰對DES的密鑰進行加密
         FileInputStream f3 = new FileInputStream("Skey_RSA_pub.dat");
         ObjectInputStream b2 = new ObjectInputStream(f3);
         RSAPublicKey pbk = (RSAPublicKey) b2.readObject();
         BigInteger e = pbk.getPublicExponent();
         BigInteger n = pbk.getModulus();
         BigInteger m = new BigInteger(ptext2);
         BigInteger c = m.modPow(e, n);
         String cs = c.toString();
         out.println(cs); // 經過網絡將加密後的祕鑰傳送到服務器
    
         //用DES加密明文獲得密文
         System.out.print("請輸入待發送的數據:");
         String s = stdin.readLine(); // 從鍵盤讀入待發送的數據
         Cipher cp = Cipher.getInstance("DESede");
         cp.init(Cipher.ENCRYPT_MODE, k);
         byte ptext[] = s.getBytes("UTF8");
         byte ctext[] = cp.doFinal(ptext);
         String str = parseByte2HexStr(ctext);
         out.println(str); // 經過網絡將密文傳送到服務器
    
         // 將客戶端明文的Hash值傳送給服務器
         String x = s;
         MessageDigest m2 = MessageDigest.getInstance("MD5");
         m2.update(x.getBytes());
         byte a[] = m2.digest();
         String result = "";
         for (int i = 0; i < a.length; i++)
         {
             result += Integer.toHexString((0x000000ff & a[i]) | 0xffffff00).substring(6);
         }
         System.out.println(result);
         out.println(result);//經過網絡將明文的Hash函數值傳送到服務器
    
         str = in.readLine();// 從網絡輸入流讀取結果
         System.out.println("從服務器接收到的結果爲:" + str); // 輸出服務器返回的結果
     }
     catch (Exception e)
     {
         System.out.println(e);//輸出異常
     }
     finally
     {
    
     }

    }.net

    public static String parseByte2HexStr(byte buf[])
    {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++)
    {
    String hex = Integer.toHexString(buf[i] & 0xFF);
    if (hex.length() == 1)
    {
    hex = '0' + hex;
    }
    sb.append(hex.toUpperCase());
    }
    return sb.toString();
    }

    public static byte[] parseHexStr2Byte(String hexStr)
    {
    if (hexStr.length() < 1)
    return null;
    byte[] result = new byte[hexStr.length() / 2];
    for (int i = 0; i < hexStr.length() / 2; i++)
    {
    int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
    int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
    16);
    result[i] = (byte) (high * 16 + low);
    }
    return result;
    }
    }

•匹配鏈接

1.運行服務器端代碼;

2.顯示「服務器已經啓動後」運行客戶端代碼;

3.顯示「已經創建鏈接」就證實雙方已經鏈接好了;

4.客戶端輸入要傳輸的信息;

5.服務器端顯示從客戶端接受到的信息;

6.雙方匹配成功,並在客戶端顯示「匹配成功」的消息。

•PSP(Personal Software Process)時間

• 步驟 耗時 百分比 需求分析 10min 12.5% 設計 20min 25% 代碼實現 20min 25% 測試 20min 25% 分析總結 10min 12.5%

相關文章
相關標籤/搜索