客戶端與服務器鏈接,並實現數據交互。java
package Server; import java.net.*; import java.io.*; public class ComputeTCPServer{ public static void main(String srgs[]) { 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 aline=in.readLine(); System.out.println("已加密數據:"+aline); Dec_RSA decode=new Dec_RSA(aline); aline=decode.decodestring; System.out.println("接收到客戶機消息:"+aline); out.println("Echo:" + aline); out.close(); in.close(); sc.close(); } catch (Exception e) { System.out.println(e); } } }
package Server; import java.io.FileInputStream; import java.io.ObjectInputStream; import java.math.BigInteger; import java.security.interfaces.RSAPrivateKey; public class Dec_RSA{ public String decodestring=""; public Dec_RSA(String ctext) throws Exception{ BigInteger c=new BigInteger(ctext); 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(); BigInteger m=c.modPow(d,n); byte[] mt=m.toByteArray(); for(int i=0;i<mt.length;i++){ decodestring+=(char)(mt[i]); } } }
如下爲組員博客連接:算法
如圖:安全
- 右鍵建立的網絡查看屬性,能夠看到IP地址爲IP4(上文圖中可見)
步驟 | 耗時 | 百分比 |
---|---|---|
需求分析 | 5分鐘 | 6.25% |
設計 | 20分鐘 | 25% |
代碼實現 | 20分鐘 | 25% |
測試 | 25分鐘 | 31.25% |
分析總結 | 10分鐘 | 12.5% |