20135234mqy 实验四
北京電子科技學院(BESTI)
實???? 驗??? 報???? 告
課程:java程序設計 班級:1352? 姓名:mqy? 學號:20135234
成績:?????????? ?指導教師:婁嘉鵬???? ? ???實驗日期:2015.6.10
實驗密級:?????? 預習程度:?????????? 實驗時間:15:30---17:20
儀器組次:34 ? ? ? 必修/選修:選修???????????? ? 實驗序號:04
實驗名稱:網(wǎng)絡編程(簡單的服務器與客戶端)
實驗目的與要求:
1.掌握Socket的基本使用方法,學會建立Socket連接;
2.掌握簡單的服務器和客戶端程序,實現(xiàn)客戶端和服務器通信;
3.掌握Java網(wǎng)絡編程的方法;
?
實驗儀器:
|   名稱  |   型號  |   數(shù)量  | 
|   筆記本電腦  |   DELL  |   1  | 
|   ?  |   ?  |   ?  | 
?
?
?
?
?
?
一、??? 實驗內(nèi)容
1、??? 運行教材上TCP代碼,結對進行,一人服務器,一人客戶端;
2、??? 利用加解密代碼包,編譯運行代碼,客戶端加密,服務器解密;
3、??? 客戶端加密明文后將密文通過TCP發(fā)送;
4、??? 加密使用DES,DES加密密鑰key發(fā)送至服務器,使用服務器的公鑰加密,公鑰算法使用RSA,檢驗發(fā)送信息的完整性使用MD5
實驗人員:
服務器 ?:20135223hwq?http://www.cnblogs.com/20135223heweiqin/
客戶端 ?:20135234mqy?
二、??? 實驗步驟
1. 組員一用計算機1創(chuàng)建局域網(wǎng),充當客戶端;
2.組員二使用計算機連入局域網(wǎng)并查詢自己的IP地址,充當服務器,然后運行服務器代碼,即打開服務器;
3.組員一進行客戶端組合代碼,首先需要連入服務器,按照本機上的IP地址修改代碼中的IP地址和端口。然后創(chuàng)建密鑰,按照服務器端口號請求連接,連接成功后組員一向組員二發(fā)送數(shù)據(jù);,從鍵盤讀入數(shù)據(jù)并加密,檢查連接狀態(tài)。
三、??? 客戶端代碼
// file name:ComputeTCPClient.java
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);
??????????
?????????? //創(chuàng)建連接特定服務器的指定端口的Socket對象
???????????????? Socket socket = new Socket("127.28.133.94", 4421);
??????????? //獲得從服務器端來的網(wǎng)絡輸入流
??????????? BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
??????????? //獲得從客戶端向服務器端輸出數(shù)據(jù)的網(wǎng)絡輸出流
??????????? PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
??????????? //創(chuàng)建鍵盤輸入流,以便客戶端從鍵盤上輸入信息
??????????? 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);? //通過網(wǎng)絡傳送到服務器
????????????????
??????????? System.out.print("請輸入待發(fā)送的數(shù)據(jù):");?
??????????? String s=stdin.readLine(); //從鍵盤讀入待發(fā)送的數(shù)據(jù)
???????????????? 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);? //通過網(wǎng)絡傳送到服務器
????????????????
???????????????? 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);? //通過網(wǎng)絡傳送到服務器*/
????????????????
??????????? str=in.readLine();//從網(wǎng)絡輸入流讀取結果
??????????? 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;?
??? }?
}
四.實驗過程截圖
?
?
?
四、實驗體會
本次實驗極大的擴展了我們的視野,使我了解到編寫程序不僅僅是一個電腦單機操作的事情,也可以上升到網(wǎng)絡層次,引起了我對JAVA極大的興趣。在實驗中,我明白怎樣創(chuàng)建客戶端和服務器,利用out.println()和in.readline()函數(shù)使其進行數(shù)據(jù)連接和傳輸。理解了如何對數(shù)據(jù)進行加密,然后進行傳輸。
統(tǒng)計的PSP時間:
|   步驟  |   耗時(min)  |   百分比  | 
|   需求分析  |   20  |   10%  | 
|   設計  |   30  |   20%  | 
|   代碼實現(xiàn)  |   50  |   40%  | 
|   測試  |   20  |   10%  | 
|   分析總結  |   30  |   ?20% ?  | 
? ? ?
轉載于:https://www.cnblogs.com/mqy123/p/4570563.html
總結
以上是生活随笔為你收集整理的20135234mqy 实验四的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Mongodb地理空间索引
 - 下一篇: 苦茶种子多少钱一斤?