實驗五 — — Java網路編程及安全

來源:互聯網
上載者:User

標籤:

java的第二個實驗——JAVA物件導向程式設計

 

北京電子科技學院

實     驗    報     告

                  課程:Java程式設計  班級:1352  姓名:林涵錦  學號:20135213

                      成績:        指導教師:婁嘉鵬     實驗日期:2015.6.11

                  實驗密級:      預習程度:        實驗時間:15:30~22:00

                  儀器組次:13     必修/選修:選修     實驗序號:5

 

                          實驗名稱:Java網路編程及安全

 

                              實驗目的與要求:        

目的:1.掌握Socket程式的編寫;                        

2.掌握密碼技術的使用;                     

3.設計安全                                      

要求:1.完成資訊加密                                          

2. 資訊加密後發送                               

                 

 

實驗儀器:

   

名稱

型號

數量

PC

 Acer

1

 

 

 

 

 

實驗代碼


package server;

 

import java.net.*;

import java.io.*;

import java.security.*;

import java.security.spec.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import javax.crypto.interfaces.*;

import java.security.interfaces.*;

import java.math.*;

 

public class ServerT {

public static void main(String args[]) throws Exception {

ServerSocket link = null;

Socket socket = null;

try {

link = new ServerSocket(8080);// 建立伺服器通訊端

System.out.println("連接埠號碼:" + link.getLocalPort());

System.out.println("伺服器已經啟動...");

socket = link.accept(); // 等待用戶端串連

System.out.println("已經建立串連");

//獲得網路輸入資料流對象的引用

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

//獲得網路輸出資料流對象的引用

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

 

// 使用伺服器端RSA的私密金鑰對DES的密鑰進行解密

String line = in.readLine();

BigInteger cipher = new BigInteger(line);

FileInputStream f = new FileInputStream("Skey_RSA_priv.dat");

ObjectInputStream b = new ObjectInputStream(f);

RSAPrivateKey prk = (RSAPrivateKey) b.readObject();

BigInteger d = prk.getPrivateExponent();

BigInteger n = prk.getModulus();//mod n

BigInteger m = cipher.modPow(d, n);//m=d (mod n)

byte[] keykb = m.toByteArray();

 

// 使用DES對密文進行解密

String readline = in.readLine();//讀取用戶端傳送來的資料

FileInputStream f2 = new FileInputStream("keykb1.dat");

int num2 = f2.available();

byte[] ctext = parseHexStr2Byte(readline);

Key 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); //列印解密結果

 

// 使用Hash函數檢測明文完整性

String aline3 = in.readLine();

String x = p;

MessageDigest m2 = MessageDigest.getInstance("MD5");//使用MD5演算法返回實現指定摘要演算法的 MessageDigest對象

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

if (aline3.equals(result)) {

System.out.println("匹配成功");

}

out.println("匹配成功");

out.close();

in.close();

link.close();

} catch (Exception e) {

System.out.println(e);

}

}

//二進位轉換成十六進位,防止byte[]數字轉換成string類型時造成的資料損失

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、結對編碼的PSP時間

步驟

耗時min

百分比

需求分析

60

20%

設計

60

 20%

代碼實現

120

40% 

測試

30

10%

分析總結

30

10%

 

 

2、遇到問題與解決方案:

(1)IP地址不會尋找

    解決:開啟運行,輸入cmd,然後輸入ipconfig

(2)一直顯示連線逾時

    解決:一人串連WiFi,然後開啟免費wifi開啟網路,串連成功。

 

3、感想總結

用程式解決實際問題時,合作者可以發現自己發現不了的錯誤,並提出不一樣的解決辦法,拓展思路。最後一次實驗的難度有點大,雖然基本代碼都給了,但是電腦間的網路連接、程式碼群組合等問題仍然十分棘手。十分考驗能力。

 

實驗五 — — Java網路編程及安全

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.