基于TCP的Socket编程:使用HashMap类来实现字符串的加密和解密

基于TCP的Socket编程:使用HashMap类来实现字符串的加密和解密

加密算法采用简单的字母映射,映射方式如下:

原文:abcdefghijklmnopqrstuvwxyz*
密文:veknohzf*iljxdmygbrcswqupta
使用HashMap类来实现字符串的加密和解密后,再利用基于TCP的Socket的编程实现客户端与服务端进一步实现如下功能:
1、客户端输入原文,服务端再次输入确定,返回客户端加密后密文并在客户端显示;
2、客户端输入密文,服务端再次输入确定,返回客户端解密后原文并在客户端显示;
程序源码如下:


接口类的实现:
public interface EnCode {
String encryp(String str);
String decryp(String str);
}




加密解密类的实现:
import java.util.HashMap;
import java.util.Map;




public class Code implements EnCode{
Mapmaph=new HashMap();
Mapmapq=new HashMap();
String strh="abcdefghijklmnopqrstuvwxyz*";
String strq="veknohzf*iljxdmygbrcswqupta";




@Override
public String encryp(String str) {

for(int i=0;i maph.put(new Character(strh.charAt(i)), new Character(strq.charAt(i)));
}
String str2;
str2=str.toLowerCase();
String str3="";

for(int i=0;i str3=str3+maph.get(str2.charAt(i));
}

return str3.toString();
}




@Override
public String decryp(String str) {

for(int i=0;i mapq.put(new Character(strq.charAt(i)), new Character(strh.charAt(i)));
}
String str4="";
for(int i=0;i str4=str4+mapq.get(str.charAt(i));
}
return str4;
}
}




客户端的实现:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;




public class TCPClient {




public static void main(String[] args) throws Exception{
String strLocal,strSocket,strLocal0;

Socket socketClient=new Socket("localhost",6789);
BufferedReader brInFromUser=new BufferedReader(new InputStreamReader(System.in));
BufferedReader brInFromUser0=new BufferedReader(new InputStreamReader(System.in));
BufferedReader brInFromServer=
new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
DataOutputStream dosOutToServer=
new DataOutputStream(socketClient.getOutputStream());
do{
System.out.println("What do you want to do,encryp,decryp or finished(88)?\n");
strLocal0=brInFromUser0.readLine();
dosOutToServer.writeBytes(strLocal0+'\n');
if((strLocal0.equals("88"))||(strLocal0.equals("finished")))
{
System.out.println("Please input 'finished' or '88' to finished:\n");
strLocal=brInFromUser.readLine();
dosOutToServer.writeBytes(strLocal+'\n');
strSocket=brInFromServer.readLine();
System.out.println("Server:"+strSocket);
break;
}
else{
if(strLocal0.equals("encryp"))
{
System.out.println("Please input the original(原文):\n");
strLocal=brInFromUser.readLine();
dosOutToServer.writeBytes(strLocal+'\n');
}
else if(strLocal0.equals("decryp"))
{
System.out.println("Please input the cipher(密文):\n");
strLocal=brInFromUser.readLine();
dosOutToServer.writeBytes(strLocal+'\n');
}
else
{System.out.println("Please input the correct option!!!");
continue;
}
strSocket=brInFromServer.readLine();
System.out.println("Server:"+strSocket);
}
}while((!strLocal0.equals("88"))||(!strLocal0.equals("finished")));
socketClient.close();
}
}
服务端的实现:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
public static void main(String[] args) throws Exception {
String strLocal,strSocket,strSocket0;
@SuppressWarnings("resource")
ServerSocket ssocketWelcome=new ServerSocket(6789);
Code code=new Code();
Socket socketServer=ssocketWelcome.accept();
BufferedReader brInFromUser=
new BufferedReader(new InputStreamReader(System.in));
BufferedReader brInFromClient=
new BufferedReader(new InputStreamReader(socketServer.getInputStream()));
DataOutputStream dosOutToClient=
new DataOutputStream(socketServer.getOutputStream());

do{
strSocket0=brInFromClient.readLine();
if(strSocket0.equals("encryp"))
{
strSocket=brInFromClient.readLine();
System.out.println("Client:"+strSocket);
strLocal=brInFromUser.readLine();
dosOutToClient.writeBytes(code.encryp(strLocal)+'\n');
}
else if(strSocket0.equals("decryp"))
{
strSocket=brInFromClient.readLine();
System.out.println("Client:"+strSocket);
strLocal=brInFromUser.readLine();
dosOutToClient.writeBytes(code.decryp(strLocal)+'\n');
}
else if(strSocket0.equals("88")||strSocket0.equals("finished"))
{
strSocket=brInFromClient.readLine();
System.out.println("Client:"+strSocket);
strLocal=brInFromUser.readLine();
dosOutToClient.writeBytes(strLocal+'\n');
break;
}
}while(!strSocket0.equals("88")||(!strSocket0.equals("finished")));
socketServer.close();

}

}


关于程序的一些问题:
先运行服务端口程序,再运行客户端口程序。
在客户端口做出功能选择,在服务端口实现,
例如:
1、加密操作:客户端做出加密选择,客户端输入原文(good),服务端再次输入原文(good)确定后直接把加密后的密文(zmmn)返回客户端显示;
2、解密操作:客户端做出解密选择,客户端输入密文(zmmn),服务端再次输入密文(zmmn)确定后直接把解密后的原文(good)返回客户端显示;
3、结束操作:客户端做出结束选择,客户端输入结束语(“finished”或“88”),服务端再次输入结束语后,直接将结束语返回客户端显示并终止客户端,然后服务端终止。

你可能感兴趣的:(Java)