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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

20135313_exp5

發(fā)布時間:2024/4/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 20135313_exp5 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

課程:Java程序與設(shè)計?????班級:1353  姓 名:吳子怡?  學(xué)號:20135313

小組成員:?20135113肖昱

成績:?????????????指導(dǎo)教師:婁嘉鵬???????實驗日期:2015.6.9

實驗密級:????????? 預(yù)習(xí)程度:?????????實驗時間:15:30-18:00

儀器組次:??????????必修/選修:選修????????實驗序號:5

實驗名稱:Java網(wǎng)絡(luò)編程及安全??????????????????????????????????????

實驗?zāi)康呐c要求:結(jié)對編程,實現(xiàn)客戶端和服務(wù)器之間數(shù)據(jù)的發(fā)送與接收,實現(xiàn)加解密和驗證Hash函數(shù)值。

實驗儀器:

?

名稱

型號

數(shù)量

PC

DELL

1

Eclipse

?

1

?

?

?

一、實驗內(nèi)容

?

1.用TCP代碼,實現(xiàn)服務(wù)器與客戶端。

2.客戶端與服務(wù)器連接

3.客戶端中輸入明文,利用DES算法加密,DES的秘鑰用RSA公鑰密碼中服務(wù)器的公鑰加密.

4.客戶端用RSA公鑰密碼中服務(wù)器的私鑰解密DES的,秘鑰,用秘鑰對密文進行解密,得出明文。

?

二.實驗過程。

1.客戶端 與服務(wù)器的連接。

服務(wù)器代碼:?

?

import java.io.BufferedInputStream;

?

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.ServerSocket; import java.net.Socket; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class ServerTest { int port = 8821; void start() { Socket s = null; try { ServerSocket ss = new ServerSocket(port); ? //創(chuàng)建一個ServerSocket套接字對象,并綁定在8821端口上 while (true) { // 選擇進行傳輸?shù)奈募?String filePath = "C:\\Users\\wzy\\Desktop\\服務(wù)器\\jiami.txt"; File fi = new File(filePath); ?//通過將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建一個新 File 實例 System.out.println("文件長度:" + (int) fi.length()); s = ss.accept(); System.out.println("建立socket鏈接"); DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream())); ? //DataInputStream:使用指定的底層 InputStream 創(chuàng)建一個 DataInputStream; dis.readByte(); ?//返回此輸入流的下一個字節(jié),以有符號 8 位 byte 的形式表示。 DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath))); DataOutputStream ps = new DataOutputStream(s.getOutputStream());//創(chuàng)建一個新的數(shù)據(jù)輸出流,將數(shù)據(jù)寫入指定基礎(chǔ)輸出流 ps.writeUTF(fi.getName()); ps.flush(); ps.writeLong((long) fi.length()); ps.flush(); int bufferSize = 8192; //緩沖區(qū),1k byte[] buf = new byte[bufferSize]; while (true) { int read = 0; if (fis != null) { read = fis.read(buf); } if (read == -1) { break; } ps.write(buf, 0, read); } ps.flush();// 直到socket超時,導(dǎo)致數(shù)據(jù)不完整。 ? ? ? ? ? ? ? ? fis.close(); s.close(); ? ? ? ? ? ? ? ? System.out.println("文件傳輸完成"); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String arg[]) { new ServerTest().start(); } }

?客戶端Socket代碼:

import java.net.*; import java.io.*; public class ClientSocket { private String ip; private int port; private Socket socket = null; DataOutputStream out = null; DataInputStream getMessageStream = null; public ClientSocket(String ip, int port) { this.ip = ip; this.port = port; } /** *//** * 創(chuàng)建socket連接 *? * @throws Exception * ? ? ? ? ? ? exception */ public void CreateConnection() throws Exception { try { socket = new Socket(ip, port); } catch (Exception e) { e.printStackTrace(); if (socket != null) socket.close(); throw e; } finally { } } public void sendMessage(String sendMessage) throws Exception { try { out = new DataOutputStream(socket.getOutputStream()); if (sendMessage.equals("Windows")) { out.writeByte(0x1); out.flush(); return; } if (sendMessage.equals("Unix")) { out.writeByte(0x2); out.flush(); return; } if (sendMessage.equals("Linux")) { out.writeByte(0x3); out.flush(); } else { out.writeUTF(sendMessage); out.flush(); } } catch (Exception e) { e.printStackTrace(); if (out != null) out.close(); throw e; } finally { } } public DataInputStream getMessageStream() throws Exception { try { getMessageStream = new DataInputStream(new BufferedInputStream(socket.getInputStream())); return getMessageStream; } catch (Exception e) { e.printStackTrace(); if (getMessageStream != null) getMessageStream.close(); throw e; } finally { } } public void shutDownConnection() { try { if (out != null) out.close(); if (getMessageStream != null) getMessageStream.close(); if (socket != null) socket.close(); } catch (Exception e) { } } }

?客戶端Test代碼:

?

import java.io.BufferedOutputStream;

?

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; public class ClientTest { private ClientSocket cs = null; private String ip = "192.168.253.1";// 設(shè)置成服務(wù)器IP private int port = 8821; private String sendMessage = "Windwos"; public ClientTest() { try { if (createConnection()) { sendMessage(); getMessage(); } } catch (Exception ex) { ex.printStackTrace(); } } private boolean createConnection() { cs = new ClientSocket(ip, port); try { cs.CreateConnection(); System.out.print("連接服務(wù)器成功!" + "\n"); return true; } catch (Exception e) { System.out.print("連接服務(wù)器失敗!" + "\n"); return false; } } private void sendMessage() { if (cs == null) return; try { cs.sendMessage(sendMessage); } catch (Exception e) { System.out.print("發(fā)送消息失敗!" + "\n"); } } private void getMessage() { if (cs == null) return; DataInputStream inputStream = null; try { inputStream = cs.getMessageStream(); } catch (Exception e) { System.out.print("接收消息緩存錯誤\n"); return; } try { //本地保存路徑,文件名會自動從服務(wù)器端繼承而來。 String savePath = "E:\\客戶端\\"; int bufferSize = 8192; byte[] buf = new byte[bufferSize]; int passedlen = 0; long len=0; savePath += inputStream.readUTF(); DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)))); len = inputStream.readLong(); System.out.println("文件的長度為:" + len + "\n"); System.out.println("開始接收文件!" + "\n"); while (true) { int read = 0; if (inputStream != null) { read = inputStream.read(buf); } passedlen += read; if (read == -1) { break; } //下面進度條本為圖形界面的prograssBar做的,這里如果是打文件,可能會重復(fù)打印出一些相同的百分比 System.out.println("文件接收了" + ?(passedlen * 100/ len) + "%\n"); fileOut.write(buf, 0, read); } System.out.println("接收完成,文件存為" + savePath + "\n"); fileOut.close(); } catch (Exception e) { System.out.println("接收消息錯誤" + "\n"); return; } } public static void main(String arg[]) { new ClientTest(); } }

?

在這只做簡單代碼展示,具體傳輸文件見下述步驟。

2.用DES加密明文,并傳輸給服務(wù)器密文。

DES加密代碼:?

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class DESJiaMi { public static void main(String[] args) throws Exception { // DES算法要求有一個可信任的隨機數(shù)源 SecureRandom sr = new SecureRandom(); // 獲得密匙數(shù)據(jù) FileInputStream fi = new FileInputStream(new File("key.txt")); byte rawKeyData[] = new byte[fi.available()]; fi.read(rawKeyData); fi.close(); // 從原始密匙數(shù)據(jù)創(chuàng)建DESKeySpec對象 DESKeySpec dks = new DESKeySpec(rawKeyData); // 創(chuàng)建一個密匙工廠,然后用它把DESKeySpec轉(zhuǎn)換成一個SecretKey對象 SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(dks); // Cipher對象實際完成加密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher對象 cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 現(xiàn)在,獲取要加密的文件數(shù)據(jù) FileInputStream fi2 = new FileInputStream(new File("lib.txt")); byte data[] = new byte[fi2.available()]; fi2.read(data); fi2.close(); // 正式執(zhí)行加密操作 byte encryptedData[] = cipher.doFinal(data); // 用加密后的數(shù)據(jù)文件 FileOutputStream fo = new FileOutputStream(new File("jiami.txt")); fo.write(encryptedData); fo.close(); new ServerTest().start(); } }

?DES解密代碼:

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; public class DESJieMi { public static void main(String[] args) throws Exception { new ClientTest(); // DES算法要求有一個可信任的隨機數(shù)源 SecureRandom sr = new SecureRandom(); // 獲得密匙數(shù)據(jù) FileInputStream fi = new FileInputStream(new File("key.txt")); byte rawKeyData[] = new byte[fi.available()];// = new byte[5]; fi.read(rawKeyData); fi.close(); // 從原始密匙數(shù)據(jù)創(chuàng)建一個DESKeySpec對象 DESKeySpec dks = new DESKeySpec(rawKeyData); // 創(chuàng)建一個密匙工廠,然后用它把DESKeySpec對象轉(zhuǎn)換成一個 SecretKey對象 SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(dks); // Cipher對象實際完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher對象 cipher.init(Cipher.DECRYPT_MODE, key, sr); // 現(xiàn)在,獲取數(shù)據(jù)并解密 FileInputStream fi2 = new FileInputStream(new File("jiami.txt")); byte encryptedData[] = new byte[fi2.available()]; fi2.read(encryptedData); fi2.close(); // 正式執(zhí)行解密操作 byte decryptedData[] = cipher.doFinal(encryptedData); // 這時把數(shù)據(jù)還原成原有的類文件 FileOutputStream fo = new FileOutputStream(new File("jiemi.txt")); fo.write(decryptedData); } } 密鑰 key.txt?

lib.txt表示明文?

通過服務(wù)器與客戶端相連,服務(wù)器將加密后的文件jiami.txt傳輸給客戶端待用。

加密后的密文 jiami.txt

?3.RSA加密密鑰,傳輸,RSA解密獲得密鑰。

服務(wù)器使用已給出的代碼加密密鑰。原理相近,不作截圖展示

代碼為老師上課在Java學(xué)習(xí)群中給出的代碼包,也不作截圖展示。

?將所加密內(nèi)容發(fā)給客戶端。并進行解密,得到客戶端解密后的密鑰。

?

?4.用密鑰解密密文。

解密代碼所生成的jiemi.txt。

?

?

?

【實驗體會】

?本次實驗中大部分代碼都是老師既定給好的,而我們的主要任務(wù)是對要實現(xiàn)的傳輸、加解密、連接等進行處理銜接,讓我們從編程一躍提升到有目的性的實戰(zhàn)。這次實驗主要分為三個部分,一是多線程客戶端和服務(wù)器的連接,而是RSA公鑰加密,三是DES加密。我認為,這些內(nèi)容都是要跨科目實現(xiàn)的,不管是密碼學(xué)還是計網(wǎng),都在這過程中起著很大的作用。然而,實驗中也遇到不少問題,也較為瑣碎。和搭檔討論后,對方給了我很多指導(dǎo),也逐漸能夠看到實驗結(jié)果離我越來越近。以后還是需要更多地尋找實驗操作,多和在Java方面學(xué)習(xí)更深入的同學(xué)探討來提升自己的能力。

?

步驟

耗時

百分比

需求分析

?30m

?30%

設(shè)計

10m

?10%

代碼實現(xiàn)

10m

?10%

測試

40m

?40%

分析總結(jié)

?10m

?10%

轉(zhuǎn)載于:https://www.cnblogs.com/paperfish/p/4575956.html

總結(jié)

以上是生活随笔為你收集整理的20135313_exp5的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。