日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

实验五 — — Java网络编程及安全

發布時間:2024/7/19 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实验五 — — Java网络编程及安全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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、感想總結

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

?

轉載于:https://www.cnblogs.com/20135213lhj/p/4570680.html

總結

以上是生活随笔為你收集整理的实验五 — — Java网络编程及安全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。