课程:Java程序设计实验 班级:1352 姓名: 万子惠 学号:20135214
成绩: 指导教师:娄嘉鹏 实验日期及时间:2015.06.09
座位号: 必修/选修:选修 实验序号:05
实验名称:Java网络编程及安全
实验仪器:
| 名称 | 型号 | 数量 | 
| 计算机 | 
 | 1 | 
| 实验楼 | 
 | 1 | 
实验partner:于佳心20135206http://www.cnblogs.com/javablack/
实验要求:
1.先运行教材上TCP代码,一人服务器,一人客户端。
2.下载加解密代码,先编译运行代码,一人加密一人解密。
3.然后集成代码,一人加密后通过后通过TCP发送,加密使用AES或DES加密密钥key的发送,使用服务器的公钥加密。
公钥算法使用RSA或DH
发送信息的完整性验证使用MD5或SHA3
负责服务器部分:
代码测试情况:


代码情况:
package ljp;
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(8001);//创建服务器套接字
            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();
			//System.out.println("d= "+d);
			//System.out.println("n= "+n);
			BigInteger m=c.modPow(d,n);
			//System.out.println("m= "+m);
			byte[] keykb=m.toByteArray();
			//String aline3=new String(mt,"UTF8");
			//String aline3=parseByte2HexStr(byte buf[]);
			
			//使用DES对密文进行解密
			String aline=in.readLine();//读取客户端传送来的数据
			//FileInputStream  f2=new FileInputStream("keykb1.dat");
			//int num2=f2.available();
			//byte[] keykb=new byte[num2];          
			//f2.read(keykb);
			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 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;  
    }  
}
实验体会:
老师为这次实验提供了代码,对我们的编程提供了方便。
起初并不会使用代码,在不断尝试之后,解决了IP地址的查询问题(命令行中ipconfig操作),逐渐顺利起来。
自己摸索很花时间,但是连接成功感觉很棒。
| 步骤 | 耗时 | 百分比 | 
| 需求分析 | 50min | 25% | 
| 设计 | 30min | 15% | 
| 代码实现 | 50min | 25% | 
| 测试 | 20min | 10% | 
| 分析总结 | 50min | 25% 
 | 
原文:http://www.cnblogs.com/midori/p/4570332.html