实验五 — — Java网络编程及安全
java的第五個(gè)實(shí)驗(yàn)——Java網(wǎng)絡(luò)編程及安全
?
北京電子科技學(xué)院
實(shí)?????驗(yàn)????報(bào)?????告
課程:Java程序設(shè)計(jì) 班級(jí):1352 姓名:林涵錦 學(xué)號(hào):20135213
? ?成績(jī):????????指導(dǎo)教師:婁嘉鵬 ?實(shí)驗(yàn)日期:2015.6.11
實(shí)驗(yàn)密級(jí): 預(yù)習(xí)程度: ?實(shí)驗(yàn)時(shí)間:15:30~22:00
儀器組次:13 必修/選修:選修 實(shí)驗(yàn)序號(hào):5
?
實(shí)驗(yàn)名稱:Java網(wǎng)絡(luò)編程及安全
?
實(shí)驗(yàn)?zāi)康呐c要求:
目的:1.掌握Socket程序的編寫; ? ? ? ? ? ? ? ? ? ? ? ?
2.掌握密碼技術(shù)的使用; ? ? ? ? ? ? ? ? ? ??
3.設(shè)計(jì)安全 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
要求:1.完成信息加密 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
2. 信息加密后發(fā)送 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ?
?
實(shí)驗(yàn)儀器:
| 名稱 | 型號(hào) | 數(shù)量 |
| PC | ?Acer | 1 |
| ? | ? | ? |
?
?
實(shí)驗(yàn)代碼
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);// 創(chuàng)建服務(wù)器套接字
System.out.println("端口號(hào):" + link.getLocalPort());
System.out.println("服務(wù)器已經(jīng)啟動(dòng)...");
socket = link.accept(); // 等待客戶端連接
System.out.println("已經(jīng)建立連接");
//獲得網(wǎng)絡(luò)輸入流對(duì)象的引用
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//獲得網(wǎng)絡(luò)輸出流對(duì)象的引用
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
?
// 使用服務(wù)器端RSA的私鑰對(duì)DES的密鑰進(jìn)行解密
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對(duì)密文進(jìn)行解密
String readline = in.readLine();//讀取客戶端傳送來的數(shù)據(jù)
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");//編碼轉(zhuǎn)換
System.out.println("從客戶端接收到信息為:" + p); //打印解密結(jié)果
?
// 使用Hash函數(shù)檢測(cè)明文完整性
String aline3 = in.readLine();
String x = p;
MessageDigest m2 = MessageDigest.getInstance("MD5");//使用MD5算法返回實(shí)現(xiàn)指定摘要算法的 MessageDigest對(duì)象
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);
}
}
//二進(jìn)制轉(zhuǎn)換成十六進(jìn)制,防止byte[]數(shù)字轉(zhuǎn)換成string類型時(shí)造成的數(shù)據(jù)損失
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());//將字符串中的小寫字母轉(zhuǎn)換成大寫字母,然后加在字符串上
}
return sb.toString();
}
//將十六進(jìn)制轉(zhuǎn)換為二進(jìn)制
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;
}
}
實(shí)測(cè)運(yùn)行圖:
?
?
實(shí)驗(yàn)總結(jié)
1、結(jié)對(duì)編碼的PSP時(shí)間
| 步驟 | 耗時(shí)min | 百分比 |
| 需求分析 | 60 | 20% |
| 設(shè)計(jì) | 60 | ?20% |
| 代碼實(shí)現(xiàn) | 120 | 40%? |
| 測(cè)試 | 30 | 10% |
| 分析總結(jié) | 30 | 10% |
?
?
2、遇到問題與解決方法:
(1)IP地址不會(huì)查找
解決:打開運(yùn)行,輸入cmd,然后輸入ipconfig
(2)一直顯示連接超時(shí)
解決:一人連接WiFi,然后打開免費(fèi)wifi開啟網(wǎng)絡(luò),連接成功。
?
3、感想總結(jié)
用程序解決實(shí)際問題時(shí),合作者可以發(fā)現(xiàn)自己發(fā)現(xiàn)不了的錯(cuò)誤,并提出不一樣的解決辦法,拓展思路。最后一次實(shí)驗(yàn)的難度有點(diǎn)大,雖然基本代碼都給了,但是電腦間的網(wǎng)絡(luò)連接、代碼組合等問題仍然十分棘手。十分考驗(yàn)?zāi)芰Α?/span>
?
轉(zhuǎn)載于:https://www.cnblogs.com/20135213lhj/p/4570680.html
總結(jié)
以上是生活随笔為你收集整理的实验五 — — Java网络编程及安全的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于mysql内存表的一个帖子(转载)
- 下一篇: Java知多少(28)super关键字