標籤:
實 驗 報 告
課程:Java 班級: 1353 姓名:李海空 學號:20135329
成績: 指導教師:婁嘉鵬 實驗日期:2015.6.12
實驗密級: 預習程度: 實驗時間:15:30~18:00
儀器組次: 必修/選修:選修 實驗序號:05
實驗名稱: 網路編程與安全
實驗目的與要求:
1.掌握Java網路編程的方法;
2.掌握Java安全編程的方法;
3.能綜合使用各種技術。
實驗儀器:
隊友 郭皓 http://www.cnblogs.com/20135327leme/ 負責用戶端一方的工作,同時協助我解決伺服器方面遇到的各種問題。
實驗內容:
1 編寫網路通訊程式(基於TCP)
2 對通訊內容使用對稱式加密演算法進行加密
3 使用非對稱演算法分發對稱式加密中使用的密鑰
4 對通訊內容進行摘要計算並驗證
5 其他安全措施
實驗步驟
伺服器:
import java.net.*;
import java.io.*;
public class Server {
public static void main(String srgs[]) throws Exception {
ServerSocket sc = null;
Socket socket = null;
try {
sc = new ServerSocket(4421);//建立伺服器通訊端
System.out.println("連接埠號碼:" + sc.getLocalPort());
System.out.println("伺服器已經啟動...");
socket = sc.accept(); //等待用戶端串連
System.out.println("已經建立串連");
//獲得網路輸入資料流對象的引用
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
////獲得網路輸出資料流對象的引用
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
String key = in.readLine();
System.out.println("從用戶端收到的加密秘鑰為:" + key);
byte[] keykb = new Dec_RSA().Dec(key);
String ctext = in.readLine();//讀取用戶端傳送來的資料
System.out.println("從用戶端收到的密文為:" + ctext);
String result=SDec.des(ctext, keykb);
String ha = in.readLine();
String sa = DigestCalc.hash(result);
boolean q = Compare.compare(sa, ha);
System.out.println("程式是否完整:" + q);
out.close();
in.cl
ose();
sc.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
============================================================
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.*;
import java.io.*;
public class Enc_RSA{
public static void main(String args[]) throws Exception{
String s="guohao";
FileInputStream f=new FileInputStream("Skey_RSA_pub.dat");
ObjectInputStream b=new ObjectInputStream(f);
RSAPublicKey pbk=(RSAPublicKey)b.readObject( );
BigInteger e=pbk.getPublicExponent();
BigInteger n=pbk.getModulus();
System.out.println("e= "+e);
System.out.println("n= "+n);
byte ptext[]=s.getBytes("UTF8");
BigInteger m=new BigInteger(ptext);
BigInteger c=m.modPow(e,n);
System.out.println("c= "+c);
String cs=c.toString( );
BufferedWriter out=
new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Enc_RSA.dat")));
out.write(cs,0,cs.length( ));
out.close( );
}
public String Enc() throws Exception{
// TODO Auto-generated method stub
String s="guohao";
FileInputStream f=new FileInputStream("Skey_RSA_pub.dat");
ObjectInputStream b=new ObjectInputStream(f);
RSAPublicKey pbk=(RSAPublicKey)b.readObject( );
BigInteger e=pbk.getPublicExponent();
BigInteger n=pbk.getModulus();
System.out.println("e= "+e);
System.out.println("n= "+n);
byte ptext[]=s.getBytes("UTF8");
BigInteger m=new BigInteger(ptext);
BigInteger c=m.modPow(e,n);
System.out.println("c= "+c);
String cs=c.toString( );
BufferedWriter out=
new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Enc_RSA.dat")));
out.write(cs,0,cs.length( ));
out.close( );
return cs ;
}
}
import java.security.*;
public class DigestCalc{
public static void main(String args[ ]) throws Exception{
String x=args[0];
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(x.getBytes( ));
byte s[ ]=m.digest( );
String result="";
for (int i=0; i<s.length; i++){
result+=Integer.toHexString((0x000000ff & s[i]) |
0xffffff00).substring(6);
}
System.out.println(result);
}
public static String hash(String s)throws Exception {
// TODO Auto-generated method stub
String x=s;
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(x.getBytes( ));
byte s1[ ]=m.digest( );
String result="";
for (int i=0; i<s1.length; i++){
result+=Integer.toHexString((0x000000ff & s1[i]) |
0xffffff00).substring(6);
}
return result;
}
}
步驟 |
耗時(min) |
百分比 |
需求分析 |
30 |
17.14% |
設計 |
15 |
8.57% |
代碼實現 |
60 |
34.29% |
測試 |
60 |
34.29% |
分析總結 |
10 |
5.71% |
二、實驗中遇到的問題及其解決方案
1.發送經過DES加密後的密文時採用的是將其轉化為字串的方式,在這裡用戶端採用的是toString()函數,伺服器採用的是getBytes()函數,傳輸後的密文出現錯誤。最後進過討論使用了二進位轉十六進位輸出密文。代碼如下:
public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); }
2.兩台電腦一直無法聯通
在實驗室時我們起初是從寢室區域網路上用自己的IPv4地址,然後顯示連線逾時。
我們認為可能是兩台電腦沒有在同一個網路裡而不發連結成功,嘗試著用一台電腦串連網路然後wifi給另一台電腦使用,然後再次串連兩台電腦,終於連通成功了。
並不知道為什麼,可能是因為區域網路的原因吧!
Java程式設計 實驗五