您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 基于TCP网络加密解密
基于TCP的网络加密解密加密解密方法:替换法服务器:程序:packagehyn;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.InputStreamReader;importjava.net.ServerSocket;importjava.net.Socket;publicclassTCPServer{publicstaticvoidmain(Stringargv[])throwsException{StringclientSentence;StringcapitalizedSentence;Stringa;ServerSocketwelcomeSocket=newServerSocket(4017);while(true){SocketconnectionSocket=welcomeSocket.accept();BufferedReaderinFromClient=newBufferedReader(newInputStreamReader(connectionSocket.getInputStream()));DataOutputStreamoutToClient=newDataOutputStream(connectionSocket.getOutputStream());clientSentence=inFromClient.readLine();capitalizedSentence=clientSentence.toUpperCase()+'\n';a=capitalizedSentence.substring(0,1);//提取首位字符if(a.matches([A-Z])){加密服务:C发送一串明文给S,S返回一串密文。解密服务:C发送一串密文给S,S返回一串明文。capitalizedSentence=capitalizedSentence.replace(A,0);capitalizedSentence=capitalizedSentence.replace(B,1);capitalizedSentence=capitalizedSentence.replace(C,2);capitalizedSentence=capitalizedSentence.replace(D,3);capitalizedSentence=capitalizedSentence.replace(E,4);capitalizedSentence=capitalizedSentence.replace(F,5);capitalizedSentence=capitalizedSentence.replace(G,6);capitalizedSentence=capitalizedSentence.replace(H,7);capitalizedSentence=capitalizedSentence.replace(I,8);capitalizedSentence=capitalizedSentence.replace(J,9);capitalizedSentence=capitalizedSentence.replace(K,!);capitalizedSentence=capitalizedSentence.replace(L,@);capitalizedSentence=capitalizedSentence.replace(M,#);capitalizedSentence=capitalizedSentence.replace(N,$);capitalizedSentence=capitalizedSentence.replace(O,%);capitalizedSentence=capitalizedSentence.replace(P,^);capitalizedSentence=capitalizedSentence.replace(Q,&);capitalizedSentence=capitalizedSentence.replace(R,*);capitalizedSentence=capitalizedSentence.replace(S,();capitalizedSentence=capitalizedSentence.replace(T,));capitalizedSentence=capitalizedSentence.replace(U,-);capitalizedSentence=capitalizedSentence.replace(V,+);capitalizedSentence=capitalizedSentence.replace(W,*);capitalizedSentence=capitalizedSentence.replace(X,/);capitalizedSentence=capitalizedSentence.replace(Y,,);capitalizedSentence=capitalizedSentence.replace(Z,.);outToClient.writeBytes(capitalizedSentence);}else{capitalizedSentence=capitalizedSentence.replace(0,a);capitalizedSentence=capitalizedSentence.replace(1,b);capitalizedSentence=capitalizedSentence.replace(2,c);capitalizedSentence=capitalizedSentence.replace(3,d);capitalizedSentence=capitalizedSentence.replace(4,e);capitalizedSentence=capitalizedSentence.replace(5,f);capitalizedSentence=capitalizedSentence.replace(6,g);capitalizedSentence=capitalizedSentence.replace(7,h);capitalizedSentence=capitalizedSentence.replace(8,i);capitalizedSentence=capitalizedSentence.replace(9,j);capitalizedSentence=capitalizedSentence.replace(!,k);capitalizedSentence=capitalizedSentence.replace(@,l);capitalizedSentence=capitalizedSentence.replace(#,,);capitalizedSentence=capitalizedSentence.replace($,n);capitalizedSentence=capitalizedSentence.replace(%,o);capitalizedSentence=capitalizedSentence.replace(^,p);capitalizedSentence=capitalizedSentence.replace(&,q);capitalizedSentence=capitalizedSentence.replace(*,r);capitalizedSentence=capitalizedSentence.replace((,s);capitalizedSentence=capitalizedSentence.replace(),t);capitalizedSentence=capitalizedSentence.replace(-,u);capitalizedSentence=capitalizedSentence.replace(+,v);capitalizedSentence=capitalizedSentence.replace(*,w);capitalizedSentence=capitalizedSentence.replace(/,x);capitalizedSentence=capitalizedSentence.replace(,,y);capitalizedSentence=capitalizedSentence.replace(.,z);outToClient.writeBytes(capitalizedSentence);}}}}截图:客户端:程序:packageaaaaaaa;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.InputStreamReader;importjava.net.Socket;publicclassTCPClient{publicstaticvoidmain(Stringargv[])throwsException{Stringsentence;StringmodifiedSentence;BufferedReaderinFromUser=newBufferedReader(newInputStreamReader(System.in));SocketclientSocket=newSocket(192.168.2.170,4017);DataOutputStreamoutToServer=newDataOutputStream(clientSocket.getOutputStream());BufferedReaderinFromServer=newBufferedReader(newInputStreamReader(clientSocket.getInputStream()));sentence=inFromUser.readLine();outToServer.writeBytes(sentence+'\n');modifiedSentence=inFromServer.readLine();System.out.println(FROMSERVER:+modifiedSentence);clientSocket.close();}}
本文标题:基于TCP网络加密解密
链接地址:https://www.777doc.com/doc-2572362 .html