• 20155309实验五 网络编程与安全


    实验内容
    1.了解计算机网络基础

    2.掌握Java Socket编程

    3.理解混合密码系统

    4.掌握Java 密码技术相关API的使用

    任务一

    1.参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA

    2.结对实现中缀表达式转后缀表达式的功能 MyBC.java

    3.结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能,调用MyDC.java

    实验代码

    while (tokenizer.hasMoreTokens())
            {
                token = tokenizer.nextToken();
    
                //如果是运算符,调用isOperator
                if (this.isOperator(token))
                {
                    if(token.charAt(0)==')')
                        while (Nch!='(')
                        {
                            Nch=this.stack.pop();
                            if(Nch!='(')
                                Nextexpr=Nextexpr+" "+Nch;
                        }
                    Nch=' ';
                    ch=this.stack.pop();
                    this.stack.push(ch);
                    if(('*'==token.charAt(0)&&(ch=='+'||ch=='-'))||('*'==token.charAt(0)&&ch==' ')||('*'==token.charAt(0)&&ch=='(')||
                            ('/'==token.charAt(0)&&(ch=='+'||ch=='-'))||('/'==token.charAt(0)&&ch==' ')||('/'==token.charAt(0)&&ch=='(')
                            ||('-'==token.charAt(0)&&(ch=='('||ch==' '))
                            ||('+'==token.charAt(0)&&(ch=='('||ch==' '))
                            || '('==token.charAt(0))
                        this.stack.push(token.charAt(0));
                    else if(token.charAt(0)!=')'){
                        Nextexpr=Nextexpr+" "+this.stack.pop();
                        ch=this.stack.pop();
                        this.stack.push(ch);
                        if(!(('*'==token.charAt(0)&&(ch=='+'||ch=='-'))||('*'==token.charAt(0)&&ch==' ')||('*'==token.charAt(0)&&ch=='(')||
                                ('/'==token.charAt(0)&&(ch=='+'||ch=='-'))||('/'==token.charAt(0)&&ch==' ')||('/'==token.charAt(0)&&ch=='(')
                                ||('-'==token.charAt(0)&&(ch=='('||ch==' '))
                                ||('+'==token.charAt(0)&&(ch=='('||ch==' '))
                                || '('==token.charAt(0))){
                            Nextexpr=Nextexpr+" "+this.stack.pop();
                        }
    
                        this.stack.push(token.charAt(0));
                    }
                }
                else//如果是操作数
                {
                    Nextexpr=Nextexpr+" "+token;
                }
    
            }
            while(!this.stack.empty())
            {
                Nextexpr=Nextexpr+" "+this.stack.pop();
            }
    

    任务二

    结对编程:1人负责客户端,一人负责服务器

    1.注意责任归宿,要会通过测试证明自己没有问题

    2.基于Java Socket实现客户端/服务器功能,传输方式用TCP

    3.客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器

    4.服务器接收到后缀表达式,调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
    客户端显示服务器发送过来的结果

    实验截图

    实验代码
    stack:

    public class MyStack<T> implements SStack<T> {
        private Object element[];
        private int top;
        public MyStack(int size){
            this.element = new Object[Math.abs(size)];
            this.top = -1;
        }
        public MyStack() {
            this(64);
        }
        public boolean isEmpty() {
            return this.top == -1;
        }
        public void push(T x) {
            if(x==null)
                return;
            if(this.top == element.length-1){
                Object[] temp = this.element;
                this.element = new Object[temp.length*2];
                for(int i = 0; i < temp.length; i++)
                    this.element[i] = temp[i];
            }
            this.top++;
            this.element[this.top] = x;
        }
        public T pop() {
            return this.top==-1 ? null:(T)this.element[this.top--];
        }
        public T get() {
            return this.top==-1 ? null:(T)this.element[this.top];
        }
    }
    

    继续:sttack

    public interface SStack<T> {
            boolean isEmpty();
            void push(T x);
            T pop();
            T get();
        }
    

    其次:Client:

    import java.net.*;
    import java.io.*;
    public class TCPClient {
        public static void main(String srgs[]) {
            try {
                //创建连接特定服务器的指定端口的Socket对象
                Socket socket = new Socket("127.0.0.1", 4421);
                //获得从服务器端来的网络输入流
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //获得从客户端向服务器端输出数据的网络输出流
                PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                //创建键盘输入流,以便客户端从键盘上输入信息
                BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                System.out.print("请输入待发送的数据:");
                String str=stdin.readLine(); //从键盘读入待发送的数据
                String postfix = MyBC.toPostfix(str);
                out.println(postfix);  //通过网络传送到服务器
                str=in.readLine();//从网络输入流读取结果
                System.out.println( "从服务器接收到的结果为:"+str); //输出服务器返回的结果
            }
            catch (Exception e) {
                System.out.println(e);
            }
            finally{
                //stdin.close();
                //in.close();
                //out.close();
                //socket.close();
            }
        }
    }
    

    接着:Service:

    import java.net.*;
    import java.io.*;
    public class TCPServer{
        public static void main(String srgs[]) {
            ServerSocket sc = null;
            Socket socket=null;
            try {
                NewMyDC evaluator = new NewMyDC();
                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); //通过网络输出流返回结果给客户端
                int result = evaluator.value(aline);
                out.println("Echo:" + result);
                out.close();
                in.close();
                sc.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    
    任务三
    

    加密结对编程:1人负责客户端,一人负责服务器

    1.注意责任归宿,要会通过测试证明自己没有问题
    2.基于Java Socket实现客户端/服务器功能,传输方式用TCP
    3.客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器
    4.服务器接收到后缀表达式表达式后,进行解密(和客户端协商密钥,可以用数组保存),然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
    5.客户端显示服务器发送过来的结果

    实验截图

    实验代码

    Client:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    import java.io.*;
    import java.security.*;
    import java.util.Arrays;
    import javax.crypto.*;
    
    public class SocketClient {
        // 搭建客户端
        public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
            MyBC mybc=new MyBC();
            try {
                Socket socket = new Socket("192.168.43.166",5329);
                System.out.println("客户端启动成功");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                PrintWriter write = new PrintWriter(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String readline;
                System.out.println("Client:" );
                readline = br.readLine();
                String line=mybc.evaluate(readline);
                FileInputStream f=new FileInputStream("key1.dat");
                ObjectInputStream b=new ObjectInputStream(f);
                Key k=(Key)b.readObject();
                Cipher cp=Cipher.getInstance("DESede");
                cp.init(Cipher.ENCRYPT_MODE, k);
                byte ptext[]=line.getBytes("UTF-8");
    
    
                byte ctext[]=cp.doFinal(ptext);
                String Str=new String(ctext,"ISO-8859-1");
    
                write.println(Str);
                write.flush();
    
    
    
    
    
    
                    System.out.println("Server return:" + in.readLine());
    
                write.close();
                in.close();
                socket.close();
    
            } catch (Exception e) {
                System.out.println("can not listen to:" + e);
            }
    
        }
    
    }
    
    接着:
    

    service:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import java.io.*;
    import java.security.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    
    public class SocketService {
        public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
            SocketService socketService = new SocketService();
            try {
                socketService.oneServer();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            }
        }
        public  void oneServer() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
            MyDC mydc=new MyDC();
    
            try{
                ServerSocket server=null;
                try{
                    server=new ServerSocket(5204);
                    System.out.println("服务器启动成功");
                }catch(Exception e) {
                    System.out.println("没有启动监听:"+e);
                }
                Socket socket=null;
                try{
                    socket=server.accept();
                }catch(Exception e) {
                    System.out.println("Error."+e);
                }
                String line;
                BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter writer=new PrintWriter(socket.getOutputStream());
    
                line=in.readLine();
                System.out.printf("密文=%s
    ",line);
                // 获取密钥
                byte[]ctext=line.getBytes("ISO-8859-1");
                FileInputStream  f2=new FileInputStream("keykb1.dat");
                int num2=f2.available();
                byte[ ] keykb=new byte[num2];
                System.out.printf("
    ");
                f2.read(keykb);
                SecretKeySpec k=new  SecretKeySpec(keykb,"DESede");
                // 解密
                Cipher cp=Cipher.getInstance("DESede");
                cp.init(Cipher.DECRYPT_MODE, k);
                byte []ptext=cp.doFinal(ctext);
                // 显示明文
    
                String p=new String(ptext,"UTF8");
    
    
    
    
                System.out.println("明文:"+p);
                int a;
                String Np="";
                for(int i=1;i<p.length();i++)
                    Np+=p.charAt(i);
                a=mydc.evaluate(Np);
    
                    writer.println(a);
                    writer.flush();
    
                writer.close();
                in.close();
                socket.close();
                server.close();
            }catch(Exception e) {
                System.out.println("Error."+e);
            }
            // 获取密文
    
        }
    
    }
    

    接着
    client:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    import java.io.*;
    import java.security.*;
    import java.util.Arrays;
    import javax.crypto.*;
    import javax.crypto.spec.SecretKeySpec;
    
    public class SocketClient {
        // 搭建客户端
        public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
            MyBC mybc=new MyBC();
            try {
                Socket socket = new Socket("192.168.43.252", 5219);
                System.out.println("客户端启动成功");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                PrintWriter write = new PrintWriter(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String readline;
                System.out.println("Client:" );
                readline = br.readLine();
                String line=mybc.evaluate(readline);
    
                // 读取对方的DH公钥
                FileInputStream f1=new FileInputStream(args[0]);
                ObjectInputStream b1=new ObjectInputStream(f1);
                PublicKey  pbk=(PublicKey)b1.readObject( );
    //读取自己的DH私钥
                FileInputStream f2=new FileInputStream(args[1]);
                ObjectInputStream b2=new ObjectInputStream(f2);
                PrivateKey  prk=(PrivateKey)b2.readObject( );
                // 执行密钥协定
                KeyAgreement ka=KeyAgreement.getInstance("DH");
                ka.init(prk);
                ka.doPhase(pbk,true);
                //生成共享信息
                byte[ ] sb=ka.generateSecret();
                System.out.println(sb.length);
                byte[]ssb=new byte[24];
                for(int i=0;i<24;i++)
                    ssb[i]=sb[i];
                Key k=new SecretKeySpec(ssb,"DESede");
    
    
    
                Cipher cp=Cipher.getInstance("DESede");
                cp.init(Cipher.ENCRYPT_MODE, k);
                byte ptext[]=line.getBytes("UTF-8");
    
    
                byte ctext[]=cp.doFinal(ptext);
                String Str=new String(ctext,"ISO-8859-1");
    
                write.println(Str);
                write.flush();
    
    
    
    
    
    
                System.out.println("Server return:" + in.readLine());
    
                write.close();
                in.close();
                socket.close();
    
            } catch (Exception e) {
                System.out.println("can not listen to:" + e);
            }
    
        }
    
    }
    
    任务五
    

    完整性校验结对编程:1人负责客户端,一人负责服务器

    1.注意责任归宿,要会通过测试证明自己没有问题

    2.基于Java Socket实现客户端/服务器功能,传输方式用TCP

    3.客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器

    4.客户端和服务器用DH算法进行3DES或AES算法的密钥交换

    5.服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端

    6.客户端显示服务器发送过来的结果

    7.上传测试结果截图和码云链接

    实验截图

    实验代码

    Service:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import java.io.*;
    import java.security.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    
    public class SocketService {
        public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
            SocketService socketService = new SocketService();
            try {
                socketService.oneServer(args);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            }
        }
        public  void oneServer(String []args) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
            MyDC mydc=new MyDC();
    
            try{
                ServerSocket server=null;
                try{
                    server=new ServerSocket(5329);
                    System.out.println("服务器启动成功");
                }catch(Exception e) {
                    System.out.println("没有启动监听:"+e);
                }
                Socket socket=null;
                try{
                    socket=server.accept();
                }catch(Exception e) {
                    System.out.println("Error."+e);
                }
                String line;
                BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter writer=new PrintWriter(socket.getOutputStream());
    
                line=in.readLine();
                System.out.printf("密文=%s
    ",line);
                // 获取密钥
                byte[]ctext=line.getBytes("ISO-8859-1");
                // 读取对方的DH公钥
                FileInputStream f1=new FileInputStream(args[0]);
                ObjectInputStream b1=new ObjectInputStream(f1);
                PublicKey  pbk=(PublicKey)b1.readObject( );
    //读取自己的DH私钥
                FileInputStream f2=new FileInputStream(args[1]);
                ObjectInputStream b2=new ObjectInputStream(f2);
                PrivateKey  prk=(PrivateKey)b2.readObject( );
                // 执行密钥协定
                KeyAgreement ka=KeyAgreement.getInstance("DH");
                ka.init(prk);
                ka.doPhase(pbk,true);
                //生成共享信息
                byte[ ] sb=ka.generateSecret();
                byte[]ssb=new byte[24];
                for(int i=0;i<24;i++)
                    ssb[i]=sb[i];
                SecretKeySpec k=new  SecretKeySpec(ssb,"DESede");
    
                // 解密
                Cipher cp=Cipher.getInstance("DESede");
                cp.init(Cipher.DECRYPT_MODE, k);
                byte []ptext=cp.doFinal(ctext);
                // 显示明文
    
                String p=new String(ptext,"UTF8");
    
                int a;
    
    
                System.out.println("明文:"+p);
                String Np="";
                for(int i=1;i<p.length();i++)
                    Np+=p.charAt(i);
                a=mydc.evaluate(Np);
    
                writer.println(a);
                writer.flush();
    
                writer.close();
                in.close();
                socket.close();
                server.close();
            }catch(Exception e) {
                System.out.println("Error."+e);
            }
            // 获取密文
    
        }
    
    }
    

    Client:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    import java.io.*;
    import java.security.*;
    import java.util.Arrays;
    import javax.crypto.*;
    import javax.crypto.spec.SecretKeySpec;
    
    public class SocketClient {
        // 搭建客户端
        public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
            MyBC mybc=new MyBC();
            try {
                Socket socket = new Socket("192.168.43.252", 5219);
                System.out.println("客户端启动成功");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                PrintWriter write = new PrintWriter(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String readline;
                System.out.println("Client:" );
                readline = br.readLine();
                String line=mybc.evaluate(readline);
    
                // 读取对方的DH公钥
                FileInputStream f1=new FileInputStream(args[0]);
                ObjectInputStream b1=new ObjectInputStream(f1);
                PublicKey  pbk=(PublicKey)b1.readObject( );
    //读取自己的DH私钥
                FileInputStream f2=new FileInputStream(args[1]);
                ObjectInputStream b2=new ObjectInputStream(f2);
                PrivateKey  prk=(PrivateKey)b2.readObject( );
                // 执行密钥协定
                KeyAgreement ka=KeyAgreement.getInstance("DH");
                ka.init(prk);
                ka.doPhase(pbk,true);
                //生成共享信息
                byte[ ] sb=ka.generateSecret();
                System.out.println(sb.length);
                byte[]ssb=new byte[24];
                for(int i=0;i<24;i++)
                    ssb[i]=sb[i];
                Key k=new SecretKeySpec(ssb,"DESede");
    
    
    
                Cipher cp=Cipher.getInstance("DESede");
                cp.init(Cipher.ENCRYPT_MODE, k);
                byte ptext[]=line.getBytes("UTF-8");
    
    
                byte ctext[]=cp.doFinal(ptext);
                String Str=new String(ctext,"ISO-8859-1");
    
                write.println(Str);
                write.flush();
    
    
    
    
    
    
                System.out.println("Server return:" + in.readLine());
    
                write.close();
                in.close();
                socket.close();
    
            } catch (Exception e) {
                System.out.println("can not listen to:" + e);
            }
    
        }
    
    }
    

    任务五

    完整性校验结对编程:1人负责客户端,一人负责服务器

    1.注意责任归宿,要会通过测试证明自己没有问题

    2.基于Java Socket实现客户端/服务器功能,传输方式用TCP

    3.客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器

    4.客户端和服务器用DH算法进行3DES或AES算法的密钥交换

    5.服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端

    6.客户端显示服务器发送过来的结果

    7.上传测试结果截图和码云链接

    实验截图

    实验代码

    Service:

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.io.PrintWriter;
        import java.net.ServerSocket;
        import java.net.Socket;
        
        import java.io.*;
        import java.security.*;
        import javax.crypto.*;
        import javax.crypto.spec.*;
        
        public class SocketService {
            public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
                SocketService socketService = new SocketService();
                try {
                    socketService.oneServer();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
            }
            public  void oneServer() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
                MyDC mydc=new MyDC();
        
                try{
                    ServerSocket server=null;
                    try{
                        server=new ServerSocket(5329);
                        System.out.println("服务器启动成功");
                    }catch(Exception e) {
                        System.out.println("没有启动监听:"+e);
                    }
                    Socket socket=null;
                    try{
                        socket=server.accept();
                    }catch(Exception e) {
                        System.out.println("Error."+e);
                    }
                    String line;
                    BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    PrintWriter writer=new PrintWriter(socket.getOutputStream());
        
                    line=in.readLine();
                    String line1=in.readLine();
                    System.out.printf("密文=%s
    ",line);
                    // 获取密钥
                    byte[]ctext=line.getBytes("ISO-8859-1");
                    FileInputStream  f2=new FileInputStream("keykb1.dat");
                    int num2=f2.available();
                    byte[ ] keykb=new byte[num2];
                    System.out.printf("
    ");
                    f2.read(keykb);
                    SecretKeySpec k=new  SecretKeySpec(keykb,"DESede");
                    // 解密
                    Cipher cp=Cipher.getInstance("DESede");
                    cp.init(Cipher.DECRYPT_MODE, k);
                    byte []ptext=cp.doFinal(ctext);
                    // 显示明文
        
                    String p=new String(ptext,"UTF8");
        
                    int a;
        
        
                    System.out.println("明文:"+p);
                    String Np="";
                    for(int i=1;i<p.length();i++)
                        Np+=p.charAt(i);
                    a=mydc.evaluate(Np);
                    String x=p;
                    MessageDigest m=MessageDigest.getInstance("MD5");
                    m.update(x.getBytes("UTF8"));
                    byte s[ ]=m.digest( );
                    String result="";
                    for (int i=0; i<s.length; i++){
                        result+=Integer.toHexString((0x000000ff & s[i]) |
                                0xffffff00).substring(6);
                    }
                    if(!(line1.equals(result)))System.out.printf("MD5比对正确!
    ");
        
                    writer.println(a);
                    writer.flush();
        
                    writer.close();
                    in.close();
                    socket.close();
                    server.close();
                }catch(Exception e) {
                    System.out.println("Error."+e);
                }
                // 获取密文
        
            }
            
    Client:
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    import java.io.*;
    import java.security.*;
    import java.util.Arrays;
    import javax.crypto.*;
    
    public class SocketClient {
        // 搭建客户端
        public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
            MyBC mybc=new MyBC();
            try {
                Socket socket = new Socket("192.168.43.252", 5209);
                System.out.println("客户端启动成功");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                PrintWriter write = new PrintWriter(socket.getOutputStream());
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String readline;
                System.out.println("Client:" );
                readline = br.readLine();
                String line=mybc.evaluate(readline);
                FileInputStream f=new FileInputStream("key1.dat");
                ObjectInputStream b=new ObjectInputStream(f);
                Key k=(Key)b.readObject( );
                Cipher cp=Cipher.getInstance("DESede");
                cp.init(Cipher.ENCRYPT_MODE, k);
                byte ptext[]=line.getBytes("UTF-8");
    
    
                byte ctext[]=cp.doFinal(ptext);
                String Str=new String(ctext,"ISO-8859-1");
    
                write.println(Str);
                write.flush();
    
                String x=readline;
                MessageDigest m=MessageDigest.getInstance("MD5");
                m.update(x.getBytes("UTF8"));
                byte s[ ]=m.digest( );
                String result="";
                for (int i=0; i<s.length; i++){
                    result+=Integer.toHexString((0x000000ff & s[i]) |
                            0xffffff00).substring(6);
                }
                write.println(result);
                write.flush();
    
    
    
    
                System.out.println("Server return:" + in.readLine());
    
                write.close();
                in.close();
                socket.close();
    
            } catch (Exception e) {
                System.out.println("can not listen to:" + e);
            }
    
        }
    
    }
    

    实验总结与体会
    本次实验没有教程做起来难度较大,幸好在和结对同伴的讨论下成功做了出来,自己的Java水平还不够,在以后的日子里还要勤加练习。

  • 相关阅读:
    【bzoj4591】[Shoi2015]超能粒子炮·改 Lucas定理
    【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set
    十分钟看懂图像语义分割技术
    命令行执行python模块时提示ImportError: No module named xxx
    python json与字典对象互相转换
    C#中json字符串的序列化和反序列化
    Python当前线程休眠1秒钟
    python之bytes和string
    Win32 基本文件读写操作
    C# 字符串与字节数组相互转换
  • 原文地址:https://www.cnblogs.com/nhx19970709/p/6941497.html
Copyright © 2020-2023  润新知