zoukankan      html  css  js  c++  java
  • 20145235实验五

    实验内容

    掌握Socket程序的编写 掌握密码技术的使用 设计安全传输系统

    实验步骤

    • 坑货队友唐振远,我是服务器
    • 首先查找自己的IP
    • 建立一个Socket对象
    • 双方下载好秘钥
    • 运行代码
    • 连接成功,我也收到了对方发来的信息。

    代码

    import java.net.*;
    import java.io.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import javax.crypto.interfaces.*;
    import java.security.interfaces.*;
    import java.math.*;
    public class ComputeTCPServer{
        public static void main(String srgs[]) throws Exception
        {
            ServerSocket sc = null;
            Socket socket=null;
            try
            {
                sc= new ServerSocket(9999);//创建服务器套接字
                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);
    
                //使用服务器端RSA的私钥对DES的密钥进行解密
                String aline2=in.readLine();
                BigInteger c=new BigInteger(aline2);
                FileInputStream f=new FileInputStream("Skey_RSA_priv.dat");
                ObjectInputStream b=new ObjectInputStream(f);
                RSAPrivateKey prk=(RSAPrivateKey)b.readObject( );
    
                BigInteger d=prk.getPrivateExponent();
                BigInteger n=prk.getModulus();
                BigInteger m=c.modPow(d,n);
                byte[] keykb=m.toByteArray();
    
                //使用DES对密文进行解密
                String aline=in.readLine();//读取客户端传送来的数据
                byte[] ctext=parseHexStr2Byte(aline);
                Key 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); //通过网络输出流返回结果给客户端
    
                //使用Hash函数检测明文完整性
                String aline3=in.readLine();
                String x=p;
                MessageDigest m2=MessageDigest.getInstance("MD5");
                m2.update(x.getBytes( ));
                byte a[ ]=m2.digest( );
                String result="";
                for (int i=0; i<a.length; i++)
                {
                    result+=Integer.toHexString((0x000000ff & a[i]) |
                            0xffffff00).substring(6);
                }
                System.out.println(result);
    
                if(aline3.equals(result))
                {
                    System.out.println("匹配成功");
                }
    
                out.println("匹配成功");
                out.close();
                in.close();
                sc.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        public static byte[] parseHexStr2Byte(String hexStr)
        {
            if (hexStr.length() < 1)
                return null;
            byte[] result = new byte[hexStr.length()/2];
            for (int i = 0;i< hexStr.length()/2; i++)
            {
                int high = Integer.parseInt(hexStr.substring(i*2, i*2+1 ), 16);
                int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
                result[i] = (byte) (high * 16 + low);
            }
            return result;
        }
        public static String parseByte2HexStr(byte buf[]) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < buf.length; i++)
            {
                String hex = Integer.toHexString(buf[i] & 0xFF);
                if (hex.length() == 1)
                {
                    hex = '0' + hex;
                }
                sb.append(hex.toUpperCase());
            }
            return sb.toString();
        }
    
    }
  • 相关阅读:
    Java 连接 Memcached 服务
    Memcached命令-存储命令-查找命令-清理命令
    memcache安装
    Python爬虫模拟登录带验证码网站
    HashMap原理
    redis 在java中的使用
    redis 事务
    Redis命令续
    Redis命令
    ApplicationListener用法
  • 原文地址:https://www.cnblogs.com/20145235litao/p/5472264.html
Copyright © 2011-2022 走看看