zoukankan      html  css  js  c++  java
  • 20145310《Java程序设计》第5次实验报告

    20145310《Java程序设计》第5次实验报告

    实验要求

    掌握Socket程序的编写;

    掌握密码技术的使用;

    设计安全传输系统。

    实验内容

    根据所学内容,编写代码实现服务器与客户端

    掌握密码技术的使用

    设计安全传输系统,客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务器的公钥加密,计算明文的Hash函数值,一起传送给客户端

    我是服务器端,我的结伴对象是20145322何志威,为客户端。

    确定服务器端IP地址和端口:打开命令提示符,输入ipconfig查询自己电脑的ip地址:

    图片

    编写代码并匹配连接。

    服务器端代码:

    import java.net.*;
    import java.io.*;
    import java.security.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.security.spec.*;
    import javax.crypto.interfaces.*;
    import java.security.interfaces.*;
    import java.math.*;
    
    
    public class Client
    {
        public static void main(String srgs[]) throws Exception
        {
            try
            {
                KeyGenerator kg = KeyGenerator.getInstance("DESede");
                kg.init(168);
                SecretKey k = kg.generateKey();
                byte[] ptext2 = k.getEncoded();// 创建连接特定服务器的指定端口的Socket对象
                Socket socket = new Socket("10.43.39.110", 8080);//这里输入的是服务器的ip地址和端口号,端口号要注意和服务器保持一致。
    
                // 获得从服务器端来的网络输入流
                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));
    
                //RSA算法,使用服务器端的公钥对DES的密钥进行加密
                FileInputStream f3 = new FileInputStream("Skey_RSA_pub.dat");
                ObjectInputStream b2 = new ObjectInputStream(f3);
                RSAPublicKey pbk = (RSAPublicKey) b2.readObject();
                BigInteger e = pbk.getPublicExponent();
                BigInteger n = pbk.getModulus();
                BigInteger m = new BigInteger(ptext2);
                BigInteger c = m.modPow(e, n);
                String cs = c.toString();
                out.println(cs); // 通过网络将加密后的秘钥传送到服务器
    
                //用DES加密明文得到密文
                System.out.print("请输入待发送的数据:");
                String s = stdin.readLine(); // 从键盘读入待发送的数据
                Cipher cp = Cipher.getInstance("DESede");
                cp.init(Cipher.ENCRYPT_MODE, k);
                byte ptext[] = s.getBytes("UTF8");
                byte ctext[] = cp.doFinal(ptext);
                String str = parseByte2HexStr(ctext);
                out.println(str); // 通过网络将密文传送到服务器
    
                // 将客户端明文的Hash值传送给服务器
                String x = s;
                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);
                out.println(result);//通过网络将明文的Hash函数值传送到服务器
    
                str = in.readLine();// 从网络输入流读取结果
                System.out.println("从服务器接收到的结果为:" + str); // 输出服务器返回的结果
            }
            catch (Exception e)
            {
                 System.out.println(e);//输出异常
            }
    
        }
    
        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();
        }
    
        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;
        }
    }
    

    实验结果截图:

    实验心得

    网络编程在我们学习中有很重要的位置,好好学习网络编程技术可以帮助我们更好地实现团队合作。

    PSP

    步骤 耗时 百分比
    需求分析 18min 15%
    设计 15min 12.5%
    代码实现 60min 50%
    测试 20min 16.7%
    分析总结 10min 8.3%
  • 相关阅读:
    Hadoop概述
    Spring Security学习总结及源码分析
    Gradle在Mac上安装及Idea中配置
    MIT-6.S081-2020实验(xv6-riscv64)一:util
    数据库理论一些疑惑点
    com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
    vue Elemente-UI 管理后台自定义 导航菜单栏
    vue 处理跨域问题 (“No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.”)
    修改伪元素content
    重写window.alert
  • 原文地址:https://www.cnblogs.com/pigeondandelion/p/5471851.html
Copyright © 2011-2022 走看看