使用Socket模拟服务端和客户端进行MD5校验

因为都是使用的java自己的类,所以搭建普通的java工程即可

1、MD5的工具类

import java.security.MessageDigest;

public class MD5Utils {

    public static String strMD5ByUTF8(String originString){
        if (originString != null) {
            try {
                MessageDigest md= MessageDigest.getInstance("MD5");
                byte[] results=md.digest(originString.getBytes("UTF-8"));
                String resultString=byteArrayToHexString(results);
                return resultString.toUpperCase();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb=new StringBuffer();
        for (int i = 0; i < b.length; i ++){
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }
    private static String byteToHexString(byte b) {
        int n=b;
        if(n<0){
            n=256+n;
        }
        int d1=n/16;
        int d2=n%16;
        return hexDigits[d1]+hexDigits[d2];
    }
    private final static String[] hexDigits={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
}

2、客户端

import java.io.*;
import java.net.Socket;

public class Sender {

    public static void main(String[] args) {

        System.out.println("----------Client----------");

        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        BufferedReader br =null;
        try {
            // 与服务端建立连接
            socket = new Socket("localhost", 8001);
            os = socket.getOutputStream();
            is = socket.getInputStream();
            
            String plainText = "abc"; // 明文串            
            String key = "key"; // 秘钥            
            String keyPlainText = key + plainText; // 组装成要加密的串

            // MD5加密
            MD5Utils md5Utils = new MD5Utils();
            String cipherText = md5Utils.strMD5ByUTF8(keyPlainText); // 密文串
            System.out.println("客户端本次上送的明文串为:" + plainText + ",本次上送的密文串为:" + cipherText);

            // 向服务端传送密文串和明文串
            String sendData = cipherText + "###" + plainText;
            os.write(sendData.getBytes());
            socket.shutdownOutput();

            // 读取服务器端返回的消息
            br = new BufferedReader(new InputStreamReader(is));
            String backMessage = br.readLine();
            System.out.println("服务器端返回本次MD5发送信息的结果:" + backMessage);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (br != null) {
                    br.close();
                }
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3、服务器端:

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

public class Receiver {

    public static void main(String[] args) {

        System.out.println("----------Server----------");

        ServerSocket server = null;
        Socket socket = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            server = new ServerSocket(8001);
            System.out.println("启动服务端....");

            // 建立连接
            socket = server.accept();

            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String getData = br.readLine();
            String [] data = getData.split("###");
            String cipherText = data[0]; // 密文串
            String plainText = data[1]; // 明文串
            System.out.println("客户端本次上送的明文串为:" + plainText + ",本次上送的密文串为:" + cipherText);

            String key = "key"; // 秘钥
            String keyPlainText = key + plainText; // 组装成要加密的串

            // 将keyPlainText进行md5加密
            MD5Utils md5Utils = new MD5Utils();
            String sign = md5Utils.strMD5ByUTF8(keyPlainText);
            System.out.println("重新加密后的密文串:" + sign);

            // 将上送的密文串和再次加密后的密文串做对比,得到结果
            Boolean result = sign.equals(cipherText);
            System.out.println("本次MD5发送信息结果:" + result);

            // 将结果返回给客户端
            bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(result.toString());
            bw.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (bw != null) {
                    bw.close();
                }
                if (br != null) {
                    br.close();
                }
                if (socket != null) {
                    socket.close();
                }
                if (server != null) {
                    server.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

4、执行顺序

先启动服务端等待客户端信息的发送

----------Server----------
启动服务端....

再启动客户端

----------Client----------
客户端本次上送的明文串为:abc,本次上送的密文串为:52878F125814206651A24111280EC873
服务器端返回本次MD5发送信息的结果:true

再返回查看服务端的日志

----------Server----------
启动服务端....
客户端本次上送的明文串为:abc,本次上送的密文串为:52878F125814206651A24111280EC873
重新加密后的密文串:52878F125814206651A24111280EC873
本次MD5发送信息结果:true

 

你可能感兴趣的:(数据结构和算法,socket,md5)