java多线程客户端服务端实现加密解密

package com.p1;

import java.util.HashMap;
import java.util.Map;
import com.p3.Lockinterface;

/*加密解密程序*/
public class Lock implements Lockinterface{
	static Map<Character,Character> map1=new HashMap<Character, Character>();
	static Map<Character,Character> map2=new HashMap<Character, Character>();
	public Lock() {
		char[] O=original.toCharArray();
		char[] P=password.toCharArray();
		for(int i=0;i<27;i++) {
			map1.put(O[i], P[i]);
			map2.put(P[i], O[i]);
		}
	}
	 public  String Lockmethod(String S) { /*加密*/
		char[] arr=S.toCharArray();
		for(int i=0;i<arr.length;i++) {
			arr[i]=map1.get(arr[i]);
			}
	        S=String.valueOf(arr);
	        return S;
	}
	public  String UnLockmethod(String S) {/*解密*/
		char[] arr=S.toCharArray();
		for(int i=0;i<arr.length;i++) {
			arr[i]=map2.get(arr[i]);
			}
	        S=String.valueOf(arr);
	        return S;
	}
}
package com.p1;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/*
 * Tread多线程
 */
public class ServerThread extends Thread{
	Socket socket=null;
	String strSocket;
	public ServerThread(Socket socket) {
		this.socket=socket;
	}
	public void run() {
		try {
			BufferedReader brInFromClient=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			DataOutputStream dosOutToClient=new DataOutputStream(socket.getOutputStream());	
			strSocket=brInFromClient.readLine();/*第一次接收数据进行判断进行哪种操作*/
			strSocket.toLowerCase();
			if(strSocket.equals("1")) {
				do {
					if(strSocket.equals("1")) {/*第一次数据不进行加密,直接返回一个标志符*/
						strSocket="0";
						dosOutToClient.writeBytes(strSocket+'\n');
						continue;
					}
					strSocket=brInFromClient.readLine();
					strSocket.toLowerCase();
					System.out.println("需要加密的字符串:"+strSocket);				
					Lock P=new Lock();
					strSocket=P.Lockmethod(strSocket);	
					System.out.println("加密后为:"+strSocket);
					dosOutToClient.writeBytes(strSocket+'\n');
				}while(!strSocket.equals("-1"));
				brInFromClient.close();
				dosOutToClient.close();
		    }
			if(strSocket.equals("0")) {
				do {
					if(strSocket.equals("0")) {/*第一次数据不进行解密,直接返回一个标志符*/
						strSocket="1";
						dosOutToClient.writeBytes(strSocket+'\n');
						continue;
					}
					strSocket=brInFromClient.readLine();
					strSocket.toLowerCase();
					System.out.println("需要解密的字符串:"+strSocket);				
					Lock P=new Lock();
					strSocket=P.UnLockmethod(strSocket);	
					System.out.println("解密后为:"+strSocket);
					dosOutToClient.writeBytes(strSocket+'\n');
				}while(!strSocket.equals("-1"));
				brInFromClient.close();
				dosOutToClient.close();
		    }
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}

package com.p1;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/*服务器程序*/
public class TCPServer {

	public static void main(String[] args) throws IOException {		
			@SuppressWarnings("resource")
			ServerSocket ssocket=new ServerSocket(6666);
			System.out.println("服务器端启动");
		while(true) {
			Socket socketServer =ssocket.accept();
			ServerThread server=new ServerThread(socketServer);
			server.start();
		}
	}
}

package com.p2;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

/*加密客户端*/
public class TCPClientLock {

	public static void main(String[] args) throws UnknownHostException, IOException {
		String strSocket,strLocal;
	    Socket socketClient=new Socket("localhost",6666);
		BufferedReader brInFromUser=new BufferedReader(new InputStreamReader(System.in));
		BufferedReader brInFromServer=new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
		DataOutputStream dosOutToServer=new DataOutputStream(socketClient.getOutputStream());
		dosOutToServer.writeBytes("1"+'\n');
		strSocket=brInFromServer.readLine();
		do {
			System.out.println("请输入需要加密的字符串,输入-1结束");
			strLocal=brInFromUser.readLine();
			if(strLocal.equals("-1")) {
				dosOutToServer.writeBytes(strLocal);
			}else {
			dosOutToServer.writeBytes(strLocal+'\n');
			}
			strSocket=brInFromServer.readLine();
			System.out.println("加密后:"+strSocket);
			}while(!strLocal.equals("-1"));
			brInFromUser.close();
			brInFromServer.close();
			dosOutToServer.close();
			socketClient.close();
	}
}

package com.p2;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

/*
 * 解密客户端
 */
public class TCPClientUnLock {

	public static void main(String[] args) throws UnknownHostException, IOException {
		String strSocket,strLocal;
	    Socket socketClient=new Socket("localhost",6666);
		BufferedReader brInFromUser=new BufferedReader(new InputStreamReader(System.in));
		BufferedReader brInFromServer=new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
		DataOutputStream dosOutToServer=new DataOutputStream(socketClient.getOutputStream());
		dosOutToServer.writeBytes("0"+'\n');
		strSocket=brInFromServer.readLine();
		do {
			System.out.println("请输入需要解密的字符串,输入-1结束");
			strLocal=brInFromUser.readLine();
			if(strLocal.equals("-1")) {
				dosOutToServer.writeBytes(strLocal);
			}else {
			dosOutToServer.writeBytes(strLocal+'\n');
			}
			strSocket=brInFromServer.readLine();
			System.out.println("解密后:"+strSocket);
			}while(!strLocal.equals("-1"));
		brInFromUser.close();
		brInFromServer.close();
		dosOutToServer.close();
		socketClient.close();
	}

}

package com.p3;

public interface Lockinterface {
	public abstract String Lockmethod(String S);
	public abstract String UnLockmethod(String S);
	public static final String original="abcdefghijklmnopqrstuvwxyz ";
	public static final String password="veknohzf iljxdmygbrcswqupta";
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值