20135239益西拉姆第四次实验报告

北京电子科技学院(BESTI)

实验报告

课程:JAVA第四次实验报告


班  级:


1352


姓  名:益西拉姆


学  号:20135239


成  绩:


/


指导教师:


娄嘉鹏


实验日期:


2015.06.09


实验密级:


/


预习程度:


/


实验时间:15:00--18:00


仪器组次:39


必须/选修:


选修


实验序号:04


实验名称:


第四次实验


实验仪器:


名称


型号


数量


PC机


DELL


1

实验内容:

1:编写网络通信程序。(基于TCP)

2:对通信内容使用对称加密算法进行加密。

3:使用非对称算法分发对称加密中使用的密钥

4:对通信内容进行摘要计算并验证。

服务器代码:

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

}

}

客户端代码:

// 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");//方法getInstance( )的参数为字符串类型,指定加密算法的名称

kg.init(168); //该步骤一般指定密钥的长度

SecretKey k=kg.generateKey( );//生成密钥

byte[] ptext2=k.getEncoded();

//String kstr=parseByte2HexStr(kb);

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

//Socket socket = new Socket("192.168.155.4", 4421);

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

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

}

}

实验结果截图:

二;实验中遇到的问题及其解决办法:

不知道ip地址怎么看。

之后问了同学才知道可以直接用命令行来实现。就是输入ipconfig.

就可以看了。

三:实验体会:


步骤


耗时(min)


百分比


需求分析


25


10%


设计


20


19%


代码实现


29


20%


测试


10


17%


分析总结


16


16%

时间: 2024-05-14 06:15:36

20135239益西拉姆第四次实验报告的相关文章

20135239 益西拉姆 linux内核分析 可执行程序的装载

益西拉姆 + 原创作品请勿转载 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” week 7 可执行程序的装载 1.预处理.编译.链接和目标文件的格式 从c语言到可执行程序的由来过程 可执行文件的创建——预处理.编译和链接 以helloworld为例 -s assembler 汇编 gcc -o hello hello.o -m32 是把hello.o链接成可执行文件. ELF格式的文件是怎么回事? v

20135239益西拉姆 Linux内核分析 进程的描述和进程的创建

[益西拉姆 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] 第六周 进程的描述和进程的创建 一. 进程的描述 进程控制块PCB——task_struct 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struct task_struct数据结构很庞大 Linux进程的状态与操作系统原理中的描述的进程状态似乎有所不同,比如就绪状态和运行状态都是

20135239益西拉姆 Linux内核分析 汇编一个简单的c程序并分析其指令过程

益西拉姆+<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 第一周linux内核分析 学习笔记 一.计算机是如何工作的? 什么是冯诺依曼体系结构? 简单来讲就是存储程序计算机,而存储程序计算机又是指从硬件角度来看, X86汇编基础 学习笔记 详细内容都写在笔记中.再次不多说. 总结:以前一直搞不太懂汇编代码,学的不明不白,虽然现在也是学的不是太明白,至少知道了各个代码什么意思,以及该如何表现,这让我觉得老师的课时

20135239益西拉姆第二次实验报告

实验二   java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计模式 实验步骤 (一)单元测试 (1) 三种代码 编程是智力活动,不是打字,编程前要把干什么.如何干想清楚才能把程序写对.写好.与目前不少同学一说编程就打开编辑器写代码不同,我希望同学们养成一个习惯,当你们想用程序解决问题时,要会写三种码: 伪代码 产品代码 测试代码 我们通过一个例子说明如何写

20135239 益西拉姆 linux内核分析 读书笔记之第四章

chapter 4 进程调度 4.1 多任务 多任务操作系统就是能同时并发的交互执行多个进程的操作系统. 多任务系统可以划分为两类: - 非抢占式多任务: - 进程会一直执行直到自己主动停止运行(这一步骤称为让步) - 抢占式多任务: - Linux/Unix使用的是抢占式的方式:强制的挂起进程的动作就叫做抢占.进程在被抢占之前能够运行的时间是预先设置好的(也就是进程的时间片) 4.2 linux的进程调度 O(1)调度器:对大服务器的工作负载很理想,但是缺少交互进程. 反转楼梯最后期限调度算法

20135239 益西拉姆 linux内核分析 进程的切换和系统的一般执行过程

week 8 进程的切换和系统的一般执行过程 [ 20135239 原文请转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] 一.进程调度与进程调度的时机分析 操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已.对于理解操作系统的工作机制,反而是进程的调度时机与进程的切换机制更为关键. 不同类型的进程有不同的调度需求 第一

20135239 益西拉姆 linux内核分析 跟踪分析Linux内核的启动过程

回顾 1.中断上下文的切换——保存现场&恢复现场 本节主要课程内容 Linux内核源代码简介 1.打开内核源代码页面 arch/目录:支持不同CPU的源代码:其中的X86是重点 init/目录:内核启动相关的代码基本都在该目录中(比如main.c等) start_kernel函数就相当于普通C程序的main函数 kernel/目录:Linux内核核心代码在kernel目录中 README 介绍了什么是Linux,Linux能够在哪些硬件上运行,如何安装内核源代码等 构造一个简单的linux系统m

20135239 益西拉姆 linux内核分析 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

https://drive.wps.cn/preview#l/759e32d65654419cb765da932cdf5cdc 本次直接在wps上写的,因为不能连同图片一起粘贴过来,一个一个粘比较费时,所以弄了个wps链接,只能下载之后观看,但是很快就好啦,不要介意,嘿嘿.

数据结构与算法 第四次实验报告 图

数据结构与算法 第四次实验报告 姓名:许恺 学号:2014011329 班级:计算机14-1     中国石油大学(北京)计算机科学与技术系 1.图的定义,文件为"Graph.h" #ifndef GRAPH_H//定义头文件 #define GRAPH_H #include<string>//引入标准库中的头文件 using namespace std; const int MaxSize=12; struct ArcNode//定义边表结点 { int adjvex;/