1.掌握Socket程序的編寫;java
2.掌握密碼技術的使用;算法
3.設計安全傳輸系統編程
4.結隊夥伴:20145302張薇
博客地址:http//www.cnblog.com/5302v/安全
5.分工:本身負責服務端,夥伴負責客戶端服務器
1.基於Java Socket實現安全傳輸網絡
2.基於TCP實現客戶端和服務器,結對編程一人負責客戶端,一人負責服務器app
3.使用Git進行版本控制socket
4.選擇對稱算法進行數據加解密.函數
5.選擇非對稱算法對對稱加密密鑰進行密鑰分發學習
信息安全傳送:
接收方B接收到信息後,用RSA解密DES密鑰信息,再用RSA解密獲取到的密鑰信息解密密文信息,最終就能夠獲得咱們要的信息(明文)。用hash函數對解出的明文進行驗證,與發送過來的hash值相等,驗證經過。
實驗代碼:
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 Server { public static void main(String args[]) throws Exception { ServerSocket link = null; Socket socket = null; try { link = new ServerSocket(1236);// 建立服務器套接字 System.out.println("端口號:" + link.getLocalPort()); System.out.println("服務器已經啓動..."); socket = link.accept(); // 等待客戶端鏈接 System.out.println("已經創建鏈接"); //得到網絡輸入流對象的引用 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //得到網絡輸出流對象的引用 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); // 使用服務器端RSA的私鑰對DES的密鑰進行解密 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) System.out.println("d= " + d); System.out.println("n= " + n); System.out.println("m= " + m); byte[] keykb = m.toByteArray(); // 使用DES對密文進行解密 String readline = in.readLine();//讀取客戶端傳送來的數據 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");//編碼轉換 System.out.println("從客戶端接收到信息爲:" + p); //打印解密結果 // 使用Hash函數檢測明文完整性 String aline3 = in.readLine(); String x = p; MessageDigest m2 = MessageDigest.getInstance("MD5");//使用MD5算法返回實現指定摘要算法的 MessageDigest對象 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); } } //二進制轉換成十六進制,防止byte[]數字轉換成string類型時形成的數據損失 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; } }
經過這次實驗,嘗試了用Java語言進行加密和解密,服務器和客戶端。學習了一些網絡編程的知識,經過實踐,對其有了更深的印象。