java实验五20145204

java实验 Tcp传输

实验内容

  1. 运行代码一人服务器,一人客户端。

  2. 下载加解密代码,先编译运行代码,一人加密一人解密,适当修改代码。

  3. 集成代码,一人加密后通过TCP发送,加密使用AES或DES,AES或DES加密密钥Key的发送,使用服务器的公钥加密,公钥算法用RSA或DH,发送信息的完整性验证使用MD5。

  4. 使用试验后git服务托管代码。

5.Blog。

PSP(Personal Software Process)

步骤 耗时(分钟) 百分比
需求分析 40 18.2
设计 30 13.63
代码实现 60 27.27
测试 60 27.27
分析总结 30 13.64

实验步骤

  1. 服务器端与客户端连接,当有客户端连接到达时,建立一个和客户端连接对应的Socket连 接对象.

IP地址查询:

服务器端:


import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

public class ServerTest

{

            public static final int PORT = 8081;

            public static void main(String[] args) throws IOException

            {

                       ServerSocket s = new ServerSocket(PORT);

                       System.out.println("Started:"+s);

                       try

                       {

                                   Socket socket = s.accept();

                                   try

                                   {

                                               System.out.println("Connection accepted:"+socket);

                                               BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                                               PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

                                               while(true)

                                               {

                                                          String str=in.readLine();

                                                          if(str.equals("END"))

                                                                      break;

                                                          System.out.print("Echoing:"+str);

                                                          out.println(str);

                                               }

                                   }

                                   finally

                                   {

                                               System.out.println("closing...");

                                               socket.close();

                                   }

                       }

                       finally

                       {

                                   s.close();

                       }

            }

}

结果:

客户端:

import java.io.*;

import java.net.InetAddress;

import java.net.Socket;;

public class ClientTest

{

            public static void main(String[] args) throws IOException

            {

                       InetAddress addr = InetAddress.getByName("172.16.64.27");

                       System.out.println("addr ="+addr);

                       Socket socket = new Socket(addr,ServerTest.PORT);

                       try

                       {

                                   System.out.println("socket="+socket);

                                   BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                                   PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

                                   for(int i =0;i<0;i++)

                                   {

                                               out.println("howdy"+i);

                                               String str = in.readLine();

                                               System.out.println(str);

                                   }

                                   out.println("END");

                       }

                       finally

                       {

                                   System.out.println("closing...");

                                   socket.close();

                       }

            }

 

}

结果:

  1. 运行加解密包中的程序

运行DES加密代码


import java.io.*;

import javax.crypto.*;

public class Skey_DES{ 

         public static void main(String args[]) throws Exception{

                  KeyGenerator kg=KeyGenerator.getInstance("DESede");

                  kg.init(168);

        SecretKey k=kg.generateKey( );

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

             ObjectOutputStream b=new  ObjectOutputStream(f);

             b.writeObject(k);

    }

}


import java.io.*;

import java.security.*;

 

public class Skey_kb{

   public static void main(String args[]) throws Exception{

         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);

        // 打印密钥编码中的内容

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

             System.out.print(kb[i]+",");

        }

   }

}                 

  1. 整合代码

// file name:ComputeTCPClient.java

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 ComputeTCPClient {

    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();

                 //String kstr=parseByte2HexStr(kb);

          

           //创建连接特定服务器的指定端口的Socket对象

                 Socket socket = new Socket("172.16.64.27", 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));

                

                 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();

                 //System.out.println("e= "+e);

                 //System.out.println("n= "+n);

                 //byte ptext2[]=kstr.getBytes("UTF8");

                 BigInteger m=new BigInteger(ptext2);

                 BigInteger c=m.modPow(e,n);

                 //System.out.println("c= "+c);

                 String cs=c.toString( );

            out.println(cs);  //通过网络传送到服务器

                

            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);  //通过网络传送到服务器

                

                 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);

            str=in.readLine();//从网络输入流读取结果

            System.out.println( "从服务器接收到的结果为:"+str); //输出服务器返回的结果

           }

        catch (Exception e) {

            System.out.println(e);

        }

           finally{

                 //stdin.close();

                 //in.close();

                 //out.close();

                 //socket.close();              

           }

          

     }

       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; 

    } 

}

结果:

posted @ 2016-05-08 22:06  20145204张亚军  阅读(176)  评论(1编辑  收藏  举报