码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA实验四

时间:2015-06-11 22:27:07      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

实验报告

课程:

Java程序设计

班级:

1352

姓名:

莫凡

学号:

20135225

成绩:

 

指导教师:

娄嘉鹏

实验日期:

2015.06.11

实验密级:

 

预习程度:

 

实验时间:

18:30~22:30

仪器组次:

25

必修/选修:

选修

实验序号:

实验名称:

Java面向对象程序设计

实验目的与要求:

1.掌握Java网络编程的方法;

2.掌握Java安全编程的方法;

3. 能综合使用各种技术; 

 

实验仪器:

 

名称

型号

数量

电脑

 

 

 

联想

 

 

 

1

 

 

                     

 

 

统计的PSP(Personal Software Process)时间:

步骤

耗时(min

百分比

需求分析

30

12.5%

设计

70

29.2%

代码实现

70

29.2%

测试

40

16.7%

分析总结

30

12.5%

 

实验内容一:

1.运行教材上TCP代码,结对进行,一人服务器,一人客户端。

2.利用加解密代码包,编译运行代码,客户端加密,服务器解密。

3.客户端加密明文后将密文通过TCP发送。

4.加密使用DES,DES加密密钥key发送至服务器,使用服务器的公钥加密,公钥算法使用RSA,检验发送信息的完整性使用MD5。

 

结对网络编程

小组成员:莫凡,潘俊洋:http://www.cnblogs.com/20135230pjy/

测试结果

服务器:

 技术分享

客户端:

 技术分享

实验代码:

服务器:

package Server;

 

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

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

           ServerSocket sc = null;

           Socket socket = null;

           try {

                 sc = new ServerSocket(2530);// 创建服务器套接字

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

                 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); // 通过网络输出流返回结果给客户端

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

      }

}

客户端:

package Client;

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

                 //String kstr=parseByte2HexStr(kb);

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

                 Socket socket = new Socket("10.43.49.38",2530);

            //获得从服务器端来的网络输入流

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

                 BigInteger m=new BigInteger(ptext2);

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

                 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{          

           }

          

     }

       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; 

    } 

}

实验中遇到的问题:

对网络编程的编写并不了解

解决问题的方法:

通过上网查资料,请教同学,参考课本和网上的代码,并对其进行修改,最终实现了实验内容。

 

实验体会:

通过本次实验,了解简单的网络编程方法,实现了在简单的客户机和服务器之间加密发送信息和解密获取信息。加深了对公钥密码体制的理解和提高了java的编程能力。

JAVA实验四

标签:

原文地址:http://www.cnblogs.com/Ntime/p/4570056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!