20145315 《Java程序设计》实验五实验报告

20145315 《Java程序设计》实验五实验报告

实验五 Java网络编程

我负责服务器部分,王嘉澜负责客户端部分:http://www.cnblogs.com/SJZGM10/p/5467368.html

网络编程技术

  1. 客户端步骤:
  • 建立网络连接
  • 交换数据:加密使用AES或者DES/AES或者DES加密密钥key并发送
  • 关闭网络连接
  1. 服务器端步骤:
  • 监听端口
  • 获得连接
  • 交换数据:使用服务器的公钥加密/公钥算法使用RSA或DH/检验发送信息的完整性使用MD5或者SHA3
  • 关闭连接

代码:

public static void main(String args[]) {
        ServerSocket sc = null;
        ServerSocket sc1 = null;
        Socket socket=null;
        Socket socket1=null;
        try {
            sc= new ServerSocket(1125);
            sc1= new ServerSocket(1126);
            System.out.println("端口号:" + sc.getLocalPort());
            System.out.println("服务器1已经启动...");
            System.out.println("端口号:" + sc1.getLocalPort());
            System.out.println("服务器2已经启动...");
            socket = sc.accept();
            System.out.println("已经建立连接");
            socket1 = sc1.accept();
            System.out.println("已经建立1连接");

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader in1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
            System.out.print("已接收\n");

            PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            String   aline=in.readLine();
            String   aline1=in1.readLine();

            System.out.print("写入文件中...\n");
            byte [] bytes = aline.getBytes("GB2312");
            byte [] bytes1 = aline1.getBytes("GB2312");

            aline = new String(bytes, "GB2312");
            aline1 = new String(bytes1, "GB2312");
            FileOutputStream  s=new FileOutputStream("key1.dat");
            s.write(bytes);

            FileOutputStream s1=new FileOutputStream("SEnc.dat");

            s1.write(bytes1);

            System.out.print("已写入文件\n");

            FileInputStream f=new FileInputStream("key1.dat");

            ObjectInputStream b=new ObjectInputStream(f);

            Key k=(Key)b.readObject( );

            byte[ ] kb=k.getEncoded( );

            FileOutputStream  f2=new FileOutputStream("keykb1.dat");

            f2.write(kb);

            System.out.print("生成解密密钥\n");

            for(int i=0;i<kb.length;i++){

            }
            FileInputStream a=new FileInputStream("SEnc.dat");

            int num=a.available();

            byte[ ] ctext=new byte[num];

            a.read(ctext);

            FileInputStream  f1=new FileInputStream("keykb1.dat");

            int num2=f1.available();

            byte[ ] keykb=new byte[num2];

            f1.read(keykb);
            SecretKeySpec e=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,"GB2312");

            System.out.print("解密中...\n");

            System.out.println(p);
            System.out.println("从客户端接收到信息为:"+p);

            out.println(p);

            out.close();

            in.close();

            sc.close();

        } catch (Exception e) {

            System.out.println(e);

        }

    }

}

参考资料:

posted @ 2016-05-08 20:14  君知らない  阅读(163)  评论(1编辑  收藏  举报