标签:
北京电子科技学院(BESTI)
实 验 报 告
课程名称:Java程序设计实验 班级:1352 姓名:洪韶武 学号:20135219
成绩: 指导教师:娄嘉鹏 实验日期:2015.06.09
实验密级: 预习程度: 实验时间: 15:30~18:00
仪器组次: 19 必修/选修:选修 实验序号:04
实验名称: java开发环境的熟悉
实验目的与要求:
1.掌握Java网络编程的方法;
2.掌握Java安全编程的方法;
3.能综合使用各种技术。
实验仪器:
名 称 | 型 号 | 数 量 |
PC | MAC | 1 |
实验内容:
1.运行教材上TCP代码,结对进行,一人服务器,一人客户端;
2.利用加解密代码包,编译运行代码,一人加密,一人解密;
3.集成代码,一人加密后通过TCP发送;
注:加密使用AES或者DES/AES或者DES加密密钥key并发送,使用服务器的公钥加密/公钥算法使用RSA或DH/检验发送信息的完整性使用MD5或者SHA3;
4.用Git进行版本控制。
5.完成Blog
实验步骤:
1、编写网络通信程序(基于TCP)
2、对通信内容使用对称加密算法进行加密
3 、使用非对称算法分发对称加密中使用的密钥
4、对通信内容进行摘要计算并验证
代码:
客户端:
// 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");
SecretKey k=kg.generateKey( );
byte[] ptext2=k.getEncoded();
//String kstr=parseByte2HexStr(kb);
//Socket socket = new Socket("192.168.155.4", 4421);
Socket socket = new Socket("127.0.0.1", 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;
}
}
服务器:
// file name£ºComputeTCPServer.java
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(4421);
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();
//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[]);
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);
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;
}
}
实验结果截图:
实验中遇到的问题及其解决方法:
1.将DES或AES算法加入socket文件中对输入及输出进行加解密时,一直不知道怎样将密码算法与原程序结合,对获取输入进行加密。
后经过对密码算法的研究,利用java自带的很多库和方法,用比特数组存储结果,还要进行进制转化,终于完成加解密和传输工作。
如下:
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();
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);
2.客户端和服务器连接时对socket的host理解不够,之前出现连接失败的地方,后来放弃了书上getbyname的方法,自己设置了127.0.0.1本机地址,也可以实现两机间通信。
三、实验体会:
步骤 |
耗时(min) |
百分比 |
需求分析 |
20 |
15% |
设计 |
20 |
15% |
代码实现 |
60 |
46% |
测试 |
20 |
15% |
分析总结 |
10 |
8%
|
标签:
原文地址:http://www.cnblogs.com/5219hsw/p/4570785.html