Java试验四
北京电子科技学院(BESTI)
实 验 报 告
课程: Java 班级:1352 姓名:朱国庆 学号:20135237
成绩: 指导教师:娄嘉鹏 实验日期:2015.6.11
实验密级: 预习程度: 实验时间:15:30~18:00
仪器组次:37 必修/选修:选修 实验序号:(四)
实验名称:Java面向对象程序设计
实验目的:
1.掌握Java网络编程的方法;
2.掌握Java安全编程的方法;
3. 能综合使用各种技术;
实验内容一:
1.运行教材上TCP代码,结对进行,一人服务器,一人客户端。
2.利用加解密代码包,编译运行代码,客户端加密,服务器解密。
3.客户端加密明文后将密文通过TCP发送。
4.加密使用DES,DES加密密钥key发送至服务器,使用服务器的公钥加密,公钥算法使用RSA,检验发送信息的完整性使用MD5。
结对网络编程
服务器:
客户端:

实验代码:
服务器:
// 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(4410);//创建服务器套接字
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 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[] mt=m.toByteArray();
//String aline3=new String(mt,"UTF8");*/
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;
}
}
客户端:
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("10.1.0.243", 4410);
//获得从服务器端来的网络输入流
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);
/*s=result;
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[]=s.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); //通过网络传送到服务器*/
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;
}
}
统计的PSP(Personal Software Process)时间:
|
步骤 |
耗时(min) |
百分比 |
|
需求分析 |
30 |
12.5% |
|
设计 |
70 |
29.2% |
|
代码实现 |
70 |
29.2% |
|
测试 |
40 |
16.7% |
|
分析总结 |
30 |
12.5% |
实验中遇到的问题:
对本次试验还是不太会,通过大量请教同学,和自己查找网上总有所获吧。在客户端和服务器设置时需要端口一致。
小组成员:
吕松鸿:http://www.cnblogs.com/lv-20135229/p/4553600.html
黄伟业:http://www.cnblogs.com/1551127024hwy/
Java试验四的更多相关文章
- Java的四种内部类
Java的四种内部类包括如下: 成员内部类 静态内部类 局部内部类 匿名内部类 成员内部类: 定义在另一个类(外部类)的内部,而且与成员方法和属性平级叫成员内部类,......相当于外部类的非静态方法 ...
- Java实验四
20145113 Java实验四 快捷键 之前没怎么记ISDEA的快捷键,但是熟练使用快捷键可以带来很多的便利,于是先开始学习一些常用的快捷键,就采用它默认的快捷键,这样后期就不会出现冲突,一些and ...
- Java中四种引用:强、软、弱、虚引用
这篇文章非常棒:http://alinazh.blog.51cto.com/5459270/1276173 Java中四种引用:强.软.弱.虚引用 1.1.强引用当我们使用new 这个关键字创建对象时 ...
- java中四种引用类型
java中四种引用类型 今天看代码,里面有一个类java.lang.ref.SoftReference把小弟弄神了,试想一下,接触java已经有3年了哇,连lang包下面的类都不了解,怎么混.后来在 ...
- 《Thinking In Java第四版》拾遗
<Thinking In Java第四版>拾遗 转自我的github(http://katsurakkkk.github.io/2016/05/Thinking-In-Java%E7%AC ...
- Java第四次上课博文动手动脑
Java第四次上课博文动手动脑 1. 查看String.equals()方法 public class StringEquals { /** * @param args the command ...
- Java进阶(四十七)Socket通信
Java进阶(四十七)Socket通信 今天讲解一个 Hello Word 级别的 Java Socket 通信的例子.具体通讯过程如下: 先启动Server端,进入一个死循环以便一直监听某端口是 ...
- Java进阶(四十三)线程与进程的区别
Java进阶(四十三)线程与进程的区别 1.线程的基本概念 概念:线程是进程中执行运算的最小单位,是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必 ...
- Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式
Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...
随机推荐
- 倒计数锁存器(CountDown Latch)和 CyclicBarrier(同步屏障)
倒计数锁存器(CountDown Latch)是异常性障碍,允许一个或多个线程等待一个或者多个其他线程来做某些事情. public static long time(Executor executor ...
- .net 读取Excel 数据时出现 “外部表不是预期的格式”的解决办法
参考网上的资料有以下2种情况: 第一:excel本身的格式不正确,用记事本打开文件,如果显示乱码证明文件没有问题,如果是html那就证明文件格式不正确. 第二:由excel版本版本导致,例如:2003 ...
- mysql数据库使用insert语句插入中文数据报错
在mysql的命令行模式中,通过insert语句插入中文数据的时候报错,类似于下面这样: Incorrect string value: '\xE7\x8F' for column 'name' at ...
- 在centos环境下编译安装myrocksdb
rocksdb(https://rocksdb.org.cn/)是脸书(facebook)公司开源的一个key-value存储引擎,基于leveldb开发.rocksdb使用的是LSM存储引擎,纯c+ ...
- PHP中的call_user_func()与call_user_func_array()简单理解
原文地址:http://small.aiweimeng.top/index.php/archives/52.html call_user_func:把一个参数作为回调函数调用 用法说明: call_u ...
- Verilog 奇数分频
代码: module odd_div( ); ; //分频系数,3即3分频 ; reg clk, rstn, clk_div_pos, clk_div_neg; wire clk_div_out; : ...
- Java序列化与反序列化(实践)
Java序列化与反序列化(实践) 基本概念:序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储和传输数据. 昨天在一本书上 ...
- 《Java 程序设计》实验报告汇总
<Java 程序设计>实验报告汇总 20145207<Java程序设计>实验一 (Java开发环境的熟悉)实验报告 20145207<Java程序设计>实验二 (J ...
- 快速搭建属于自己的mongodb数据库
前言 MongoDB 是一个基于分布式文件存储的数据库.由C++语言编写,支持Windows,Linux,OSX,Solaris等平台,默认端口为27017,是一个效率非常高的nosql数据库. 我的 ...
- PowerDesigner中翻转生成PDM图时把Name属性变成注释(转)
在pd里面运行下面这段代码'******************************************************************************'* File: ...