java的网络编程设计报告_20145211 《Java程序设计》实验报告五————Java网络编程及安全实验报告...

实验内容

1.掌握Socket程序的编写;

2. 掌握密码技术的使用;

3. 设计安全传输系统。

实验步骤

这一部分是与我的partner合作的,详见他的博客- [20145326 《Java程序设计》实验五——Java网络编程及安全实验报告](http://www.cnblogs.com/cxy1616/p/5471028.html)

我设计客户端部分

确定服务器端IP地址和端口

用设计服务器的电脑打开cmd命令行,在其中输入ipconfig查询服务器的IPv4地址并让负责客户端的用户记录下来,创立一个端口号且服务器和客户端一致(最好不要为8080、8030这些常见的)。

663405a1d5f958844ad3bc8b9b1febb6.png

现场

将Skey_RSA_priv.dat与Skey_RSA_pub.dat拷贝至与src同级的文件夹内,这样运行后才不会显示错误“系统找不到指定文件”。

编写代码

建立一个Socket对象,用来建立一个端口号与客户端相连,获得网络输入流与输出流对象的引用。

使用服务器端RSA的私钥对DES的密钥进行解密,对秘钥进行解密之后使用DES对密文进行解密。

计算解密后的hash值来确定解密是否正确。

进行匹配连接。

①运行服务器端代码

②显示“服务器已经启动后”运行客户端代码

③显示“已经建立连接”就证明双方已经连接好了

④客户端输入要传输的信息

⑤服务器端显示从客户端接受到的信息

⑥双方匹配成功,并在客户端显示“匹配成功”的消息

5cfaa1feaab6f1b7539e220c57185802.png

5add91c2ef048c31440c0569f2b26321.png

实验代码如下

package DO5;

/**

* Created by Cai Ye on 2016/5/5.

*/

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(12333);//创建服务器套接字

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

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

byte[] keykb=m.toByteArray();

//使用DES对密文进行解密

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

//使用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

{

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

}

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

}

}

实验中的问题和解决过程

问题1:运行后显示错误“系统找不到指定文件”。

解决:将Skey_RSA_priv.dat拷贝至与src同级的文件夹内。

问题2:服务器启动后客户端显示错误java.net.SocketException: Connection reset(连接重置)。

解决:没有关闭上次运行的窗口,端口号被占用,关闭上次运行的窗口后就可以成功了。

问题3:服务器启动后客户端显示错误`java.net.ConnectException: Connection timed out: connect(连接超时).

解决:我们推断可能是网络连接有问题。我们最初同时使用edu的wifi,结果为连接超时;然后,我们连接同一台手机散发的热点,结果仍然为连接超时;最后,我用服务器端的电脑给客户端散发WiFi,结果就成功了,很神奇。

实验代码托管截图

13001fa5537a34e6ef72b5a799af55c9.png

PSP(Personal Software Process)时间:

PSP(Personal Software Process)时间:

步骤

耗时

百分比

需求分析

10min

5%

设计

10min

5%

代码实现

60min

32%

测试

75min

40%

分析总结

30min

16%

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值