Java程式設計第四次實驗報告

來源:互聯網
上載者:User

標籤:

北京電子科技學院(BESTI)

實     驗    報     告

課程:java程式設計 班級:1352  姓名:何偉欽  學號:20135223

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

實驗密級:       預習程度:           實驗時間:15:30---17:20

儀器組次:23       必修/選修:選修   實驗序號:05

實驗名稱:網路編程(簡單的伺服器與用戶端)

實驗目的與要求:

1.掌握Socket的基本使用方法,學會建立Socket串連;

2.掌握簡單的伺服器和用戶端程式,實現用戶端和伺服器通訊;

3.掌握Java網路編程的方法;

 

實驗儀器:

名稱

型號

數量

膝上型電腦

戴爾14z-5423

1

 

 

 

 

 

 

 

 

一、 實驗內容

1、 運行教材上TCP代碼,結對進行,一人伺服器,一人用戶端;

2、 利用加解密程式碼封裝,編譯運行代碼,用戶端加密,伺服器解密;

3、 用戶端加密明文後將密文通過TCP發送;

4、 加密使用DES,DES加密金鑰key發送至伺服器,使用伺服器的公開金鑰加密,公開金鑰演算法使用RSA,檢驗發送資訊的完整性使用MD5

 

二、 實驗步驟

      結對網路編程

     小組成員:何偉欽 馬啟揚:http://www.cnblogs.com/mqy123/ 

  1. 組員一用電腦1建立區域網路,充當用戶端,

   2組員二使用電腦連入區域網路並查詢自己的IP地址,充當伺服器,然後運行伺服器代碼,即開啟伺服器;

3.組員一進行用戶端組合代碼,首先需要連入伺服器,按照本機上的IP地址修改代碼中的IP地址和連接埠。然後建立密鑰,按照伺服器連接埠號碼請求串連,串連成功後組員一向組員二發送資料;,從鍵盤讀入資料並加密,檢查串連狀態。

三、實驗代碼

(一)用戶端代碼

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.*;
public class ComputeTCPClient {
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();
//String kstr=parseByte2HexStr(kb);

//建立串連特定伺服器的指定連接埠的Socket對象
Socket socket = new Socket("192.168.1.109",8642);
//獲得從伺服器端來的網路輸入資料流
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));

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();
//System.out.println("e= "+e);
//System.out.println("n= "+n);
//byte ptext2[]=kstr.getBytes("UTF8");
BigInteger m=new BigInteger(ptext2);
BigInteger c=m.modPow(e,n);
//System.out.println("c= "+c);
String cs=c.toString( );
out.println(cs); //通過網路傳送到伺服器

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); //通過網路傳送到伺服器

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

/*s=result;
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();
//System.out.println("e= "+e);
//System.out.println("n= "+n);
byte ptext2[]=s.getBytes("UTF8");
BigInteger m=new BigInteger(ptext2);
BigInteger c=m.modPow(e,n);
//System.out.println("c= "+c);
String cs=c.toString( );
out.println(cs); //通過網路傳送到伺服器*/

str=in.readLine();//從網路輸入資料流讀取結果
System.out.println( "從伺服器接收到的結果為:"+str); //輸出伺服器返回的結果
}
catch (Exception e) {
System.out.println(e);
}
finally{
//stdin.close();
//in.close();
//out.close();
//socket.close();
}

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

 

(二)伺服器代碼

packet 20135223

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 ComputeTCPServer{
public static void main(String srgs[]) throws Exception {
ServerSocket sc = null;
Socket socket=null;
try {
sc= new ServerSocket(8642);//建立伺服器通訊端
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 aline2=in.readLine();
BigInteger c=new BigInteger(aline2);
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();
//System.out.println("d= "+d);
//System.out.println("n= "+n);
BigInteger m=c.modPow(d,n);
//System.out.println("m= "+m);
byte[] keykb=m.toByteArray();
//String aline3=new String(mt,"UTF8");
//String aline3=parseByte2HexStr(byte buf[]);

String aline=in.readLine();//讀取用戶端傳送來的資料
//FileInputStream f2=new FileInputStream("keykb1.dat");
//int num2=f2.available();
//byte[] keykb=new byte[num2]; 
//f2.read(keykb);
byte[] ctext=parseHexStr2Byte(aline);
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); //通過網路輸出資料流返回結果給用戶端

/*String aline2=in.readLine();
BigInteger c=new BigInteger(aline2);
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();
//System.out.println("d= "+d);
//System.out.println("n= "+n);
BigInteger m=c.modPow(d,n);
//System.out.println("m= "+m);
byte[] mt=m.toByteArray();
//String aline3=new String(mt,"UTF8");*/

String aline3=in.readLine();
String x=p;
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);

if(aline3.equals(result)){
System.out.println("匹配成功");
}

out.println("匹配成功");
out.close();
in.close();
sc.close();
} catch (Exception e) {
System.out.println(e);

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

}

 

四.實驗過程

 

 

四.遇到的問題

問題一:雖然伺服器已經啟動,建立連結,但無法與用戶端配備成功

問題所在:實驗代碼中IP地址按照與本機上的IP地址設定

如何解決:修改IP地址

 

 

五、實驗體會

1.結對程式設計,實現了兩台PC的資訊互傳,增強了團隊意識和合作能力;

   2.基本瞭解到簡單的伺服器和用戶端程式,如何?用戶端和伺服器通訊; 

   3.基本理解了如何對資料進行加密,然後進行傳輸;

這次實驗意義重大,激發了我對java程式開發的興趣,發現只要用心學,會發現其中很多有趣的事情。

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.