20175323 java实验五 网络编程与安全

任务一

①编写MyBC.java实现中缀表达式转后缀表达式的功能

②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表达式求值的功能

基本思想:把中缀式转化后缀式,再调用MyDC.java 中的evaluate方法

  • 设立一个栈,存放运算符,首先栈为空;
  • 从左到右扫描中缀式,若遇到操作数,直接输出,并输出一个空格作为两个操作数的分隔符;
  • 若遇到运算符,则与栈顶比较,比栈顶级别高则进栈,否则退出栈顶元素并输出,然后输出一个空格作分隔符;
  • 若遇到左括号,进栈;若遇到右括号,则一直退栈输出,直到退到左括号止。
  • 当栈变成空时,输出的结果即为后缀表达式。

    代码如下:

    MyBC.java
import java.util.*;

public class MyBC {
private Stack<String> stack;
private List<String> list; private String message, Message = ""; public MyBC() {
stack = new Stack<String>();//用来暂时存放运算符的栈
list = new ArrayList<String>();//用来暂时存放操作数及运算符的列表
} public void conversion(String expr) { //中缀转后缀
String token;
StringTokenizer tokenizer = new StringTokenizer(expr); while (tokenizer.hasMoreTokens()) {
//当tokenizer有下一个值时,进行循环,并把值赋给token
token = tokenizer.nextToken(); if (token.equals("(")) {
//如果是左括号,入栈
stack.push(token);
} else if (token.equals("+") || token.equals("-")) {
//如果是“+”或“-”,继续判断栈是否为空
if (!stack.empty()) {
//如果栈非空,判断栈顶元素是什么
if (stack.peek().equals("(")) {
//如果栈顶为“(”,运算符入栈
stack.push(token);
} else {
//否则先把栈顶元素移除,加到列表中,再将运算符入栈
list.add(stack.pop());
stack.push(token);
}
} else {
//若栈为空,运算符入栈
stack.push(token);
}
} else if (token.equals("*") || token.equals("÷")) {
//如果是“*”或“÷”,继续判断栈是否为空
if (!stack.empty()) {
//如果栈非空,判断栈顶元素是什么
if (stack.peek().equals("*") || stack.peek().equals("÷")) {
//如果栈顶为“*”或“÷”,先把栈顶元素移除,加到列表中,再将运算符入栈
list.add(stack.pop());
stack.push(token);
} else {
//如果栈顶为其他,运算符直接入栈
stack.push(token);
}
} else {
//如果栈为空,运算符直接入栈
stack.push(token);
}
} else if (token.equals(")")) {
//如果遇到“)”,开始循环
while (true) {
//先把栈顶元素移除并赋给A
String A = stack.pop();
if (!A.equals("(")) {
//如果A不为“(”,则加到列表
list.add(A);
} else {
//如果A为“(”,退出循环
break;
}
}
} else {
//如果为操作数,进入列表
list.add(token);
}
}
while (!stack.empty()) {
//将栈中元素取出,加到列表中,直到栈为空
list.add(stack.pop());
}
ListIterator<String> li = list.listIterator();//返回此列表元素的列表迭代器(按适当顺序)。
while (li.hasNext()) {
//将迭代器中的元素依次取出,并加上空格作为分隔符
Message += li.next() + " ";
li.remove();
}
message = Message;
} public String getMessage() {
return message;
}
}

Calculator .java

import java.util.*;
public class Calculator {
public static void main(String[] args) {
String expression;
int result;
MyBC nts = new MyBC();
MyDC value = new MyDC();
System.out.println("Please input a nifix expression");
Scanner in = new Scanner(System.in);
expression = in.nextLine();
nts.conversion(expression);
System.out.println("The postfix expression is :"+nts.getMessage());
result = value.evaluate(nts.getMessage());
System.out.println("The result is :"+result);
}
}

MyDC .java

import java.util.*;

public class MyDC {
private final char ADD = '+';
private final char SUBTRACT = '-';
private final char MUTIPLY = '*';
private final char DIVIDE = '/';
private Stack<Integer> stack;
public MyDC(){
stack = new Stack<Integer>();
}
public int evaluate(String expr){
int op1,op2,result = 0;
String token;
StringTokenizer tokenizer = new StringTokenizer(expr);
while(tokenizer.hasMoreTokens()){
token = tokenizer.nextToken();
if(isOperator(token)){
op2 = (stack.pop().intValue());
op1 = (stack.pop().intValue());
result = evalSingleOp(token.charAt(0),op1,op2);
stack.push(new Integer(result));
}
else {
stack.push(new Integer((Integer.parseInt(token))));
}
}
return result;
}
private boolean isOperator(String token){
return (token.equals("+")||token.equals("-")||token.equals("*")||token.equals("/"));
}
private int evalSingleOp(char operation,int op1,int op2) {
int result = 0;
switch (operation){
case ADD:
result = op1+op2;
break;
case SUBTRACT:
result = op1-op2;
break;
case MUTIPLY:
result = op1*op2;
break;
case DIVIDE:
result = op1/op2;
}
return result;
}
}

NewMyDCTestor.java

import java.util.Scanner;
public class NewMyDCTester {
public static void main(String [] args) {
String expression,again;
int result;
try {
Scanner in = new Scanner(System.in);
do {
MyDC evaluator = new MyDC();
System.out.println("Enter a valid postfix expression: ");
expression = in.nextLine();
result = evaluator.evaluate(expression);
System.out.println("That expression equals :" + result);
System.out.println("Evaluate another expression [Y/N]?");
again = in.nextLine();
System.out.println();
}
while(again.equalsIgnoreCase("y"));
}
catch (Exception IOException)
{
System.out.println("Input exception reported");
}
}
}

得到第一个提交点

2018-2019-2-20175323 java实验五 网络编程与安全-LMLPHP

任务二

  1. 注意责任归宿,要会通过测试证明自己没有问题
  2. 基于Java Socket实现客户端/服务器功能,传输方式用TCP
  3. 客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器
  4. 服务器接收到后缀表达式,调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
  5. 客户端显示服务器发送过来的结果

    实验思想

    『java.net.Socket』
  • 套接字是一个网络连接的端点。在java中,使用java.net.Socket对象来表示一个套接字。
  • 要创建一个套接字,可以使用Socket的构造方法,如:public Socket(java.lang.String host, int port)。其中,host是远程机器名或IP地址,port是远程应用程序的端口号。
  • 一旦成功创建了Socket类的一个实例,就可以使用它发送或接收字节流。要发送字节流,必须先调用Socket类的getOutputStream方法来获取一个java.io.OutputStream对象。要向远程应用程序发送文本,通常要从返回的OutputStream对象构建一个java.io.PrintWriter对象。要接收来自连接的另一端的字节流,可以调用Socket类的getInputStream方法,它返回一个java.io.InputStream。

    『java.net.ServerSocket』
  • ServerSocket是服务器套接字的一个实现。ServerSocket和Socket不同,服务器套接字的角色是,等待来自客户端的连接请求。一旦服务器套接字获得了一个连接请求,它就会创建一个Socket实例,以处理和客户端的通信。

    Address.java
import java.net.*;

public class Address {

    public static void main(String[] args) throws UnknownHostException {

        InetAddress net = InetAddress.getLocalHost();

        System.out.println(net.toString());

    }

}

得到ip地址169.254.15.205

Client.java

import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
System.out.println("客户端启动...");
//while (true) {
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
try {
mysocket = new Socket("169.254.15.205", 2010);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
System.out.println("请输入:");
String str = new BufferedReader(new InputStreamReader(System.in)).readLine();
MyBC turner = new MyBC();
turner.conversion(str);
String str1 = turner.getMessage();
out.writeUTF(str1);
String s = in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回答:" + s);
Thread.sleep(500);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
//}
}
}

Server.java

import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
int answer;
//while (true) {
ServerSocket serverForClient = null;
Socket socketOnServer = null;
DataOutputStream out = null;
DataInputStream in = null;
try {
serverForClient = new ServerSocket(2010);
} catch (IOException e1) {
System.out.println(e1);
}
try {
System.out.println("等待客户呼叫");
socketOnServer = serverForClient.accept();
//堵塞状态,除非有客户呼叫
out = new DataOutputStream(socketOnServer.getOutputStream());
in = new DataInputStream(socketOnServer.getInputStream());
String s = in.readUTF();
// in读取信息,堵塞状态
System.out.println("服务器收到客户的提问:" + s);
MyDC myDC = new MyDC();
answer = myDC.evaluate(s);
out.writeUTF(answer + "");
Thread.sleep(500);
} catch (Exception e) {
System.out.println("客户已断开" + e);
}
//}
}
}

因为我没有队友,所以客户端的ip地址加的自己的IP地址,先运行Server,再运行Client,发现似乎不能自己发送自己接收,最后改成了教材上的127.1.0.0

2018-2019-2-20175323 java实验五 网络编程与安全-LMLPHP

得到第二个提交点

2018-2019-2-20175323 java实验五 网络编程与安全-LMLPHP

任务三

  1. 一人负责客户端,一人负责服务器
  2. 注意责任归宿,要会通过测试证明自己没有问题
  3. 基于Java Socket实现客户端/服务器功能,传输方式用TCP
  4. 客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器
  5. 服务器接收到后缀表达式表达式后,进行解密(和客户端协商密钥,可以用数组保存),然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
  6. 客户端显示服务器发送过来的结果

    实验思想

    实现DES加密主要有以下几个步骤:
  • 对称密钥的生成和保存;
  • 使用对称密钥进行加密和解密;
  • 从文件中获取加密时使用的密钥,使用密钥进行解密。

    Client2.java
import javax.crypto.Cipher;
import java.io.*;
import java.net.Socket;
import java.security.Key;
import java.util.Scanner; public class Client2 {
public static void main(String args[]) {
Socket mysocket;
MyBC mybc = new MyBC();
DataInputStream in = null;
DataOutputStream out = null;
Scanner scanner = new Scanner(System.in);
String str;
try {
mysocket = new Socket("169.254.15.205", 5318);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
System.out.println("客户端启动...");
FileInputStream f = new FileInputStream("key1.dat");
ObjectInputStream b = new ObjectInputStream(f);
Key key = (Key) b.readObject();
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, key);
System.out.println("请输入中缀表达式:");
str = scanner.nextLine();
mybc.conversion(str);
String str1 = mybc.getMessage();
byte ptext[] = str1.getBytes("UTF-8");
byte ctext[] = cp.doFinal(ptext);
System.out.println("被加密的后缀表达式:");
for (int i = 0; i < ctext.length; i++) {
System.out.print(ctext[i] + ",");
}
System.out.println("");
out.writeUTF(ctext.length + "");
for (int i = 0; i < ctext.length; i++) {
out.writeUTF(ctext[i] + "");
//System.out.print();
}
String s = in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回应:" + s);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
}
}

Server2.java

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket; public class Server2 {
public static void main(String args[]) {
MyDC mydc = new MyDC();
ServerSocket serverForClient = null;
Socket socketOnServer = null;
DataOutputStream out = null;
DataInputStream in = null;
try {
serverForClient = new ServerSocket(5318);
} catch (IOException e1) {
System.out.println(e1);
}
try {
System.out.println("等待客户呼叫");
socketOnServer = serverForClient.accept(); //堵塞状态,除非有客户呼叫
System.out.println("客户已连接");
out = new DataOutputStream(socketOnServer.getOutputStream());
in = new DataInputStream(socketOnServer.getInputStream());
String leng = in.readUTF(); // in读取信息,堵塞状态
byte ctext[] = new byte[Integer.parseInt(leng)];
for (int i = 0;i<Integer.parseInt(leng);i++) {
String temp = in.readUTF();
ctext[i] = Byte.parseByte(temp);
}
// 获取密钥
FileInputStream f2 = new FileInputStream("keykb1.dat");
int num2 = f2.available();
byte[] keykb = new byte[num2];
f2.read(keykb);
SecretKeySpec k = new SecretKeySpec(keykb, "DESede");
// 解密
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte[] ptext = cp.doFinal(ctext);
/*for (int i = 0; i < ptext.length; i++) {
System.out.print(ptext[i] + ",");
}
*/
System.out.println("");
// 显示明文
String p = new String(ptext,"UTF8");
System.out.println("被解密的后缀表达式:" + p);
System.out.println("计算后缀表达式" + p);
out.writeUTF(mydc.evaluate(p)+"");
} catch (Exception e) {
System.out.println("客户已断开" + e);
}
}
}

第三个提交点

2018-2019-2-20175323 java实验五 网络编程与安全-LMLPHP

任务四

  1. 一人负责客户端,一人负责服务器
  2. 注意责任归宿,要会通过测试证明自己没有问题
  3. 基于Java Socket实现客户端/服务器功能,传输方式用TCP
  4. 客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文发送给服务器
  5. 客户端和服务器用DH算法进行3DES或AES算法的密钥交换
  6. 服务器接收到后缀表达式表达式后,进行解密,然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
  7. 客户端显示服务器发送过来的结果

    实验思想
  • 执行密钥协定的标准算法是DH算法(Diffie-Hellman算法),分为以下两步:

    • 创建DH公钥和私钥;
    • 创建共享密钥。

      Key_DH.java
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
public class Key_DH{
private static final byte skip1024ModulusBytes[] = {
(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,
(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,
(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,
(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,
(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,
(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,
(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,
(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,
(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,
(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,
(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,
(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,
(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,
(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,
(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,
(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,
(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,
(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,
(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,
(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,
(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,
(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,
(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,
(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,
(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,
(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,
(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,
(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,
(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,
(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,
(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,
(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7
};
private static final BigInteger skip1024Modulus = new BigInteger(1, skip1024ModulusBytes);
private static final BigInteger skip1024Base = BigInteger.valueOf(2);
public static void fun(String s1,String s2) throws Exception{
DHParameterSpec DHP= new DHParameterSpec(skip1024Modulus,skip1024Base);
KeyPairGenerator kpg= KeyPairGenerator.getInstance("DH");
kpg.initialize(DHP);
KeyPair kp=kpg.genKeyPair();
PublicKey pbk=kp.getPublic();
PrivateKey prk=kp.getPrivate();
// 湫Կ
FileOutputStream f1=new FileOutputStream(s1);
ObjectOutputStream b1=new ObjectOutputStream(f1);
b1.writeObject(pbk);
// ˽Կ
FileOutputStream f2=new FileOutputStream(s2);
ObjectOutputStream b2=new ObjectOutputStream(f2);
b2.writeObject(prk);
}
}

KeyAgree.java

import java.io.*;
import java.math.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
public class KeyAgree{
public static void fun(String s1,String s2) throws Exception{
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream(s1);
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream(s2);
ObjectInputStream b2=new ObjectInputStream(f2);
PrivateKey prk=(PrivateKey)b2.readObject( );
// 执行密钥协定
KeyAgreement ka=KeyAgreement.getInstance("DH");
ka.init(prk);
ka.doPhase(pbk,true);
//生成共享信息
byte[ ] sb=ka.generateSecret();
FileOutputStream fsb = new FileOutputStream("sb.txt");
fsb.write(sb);
}
}

第四个提交点

2018-2019-2-20175323 java实验五 网络编程与安全-LMLPHP

任务五

  1. 一人负责客户端,一人负责服务器
  2. 注意责任归宿,要会通过测试证明自己没有问题
  3. 基于Java Socket实现客户端/服务器功能,传输方式用TCP
  4. 客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器
  5. 客户端和服务器用DH算法进行3DES或AES算法的密钥交换
  6. 服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
  7. 客户端显示服务器发送过来的结果

    实验思想
  • 可以使用Java计算指定字符串的消息摘要。java.security包中的MessageDigest类提供了计算消息摘要的方法,首先生成对象,执行其update()方法可以将原始数据传递给该对象,然后执行其digest( )方法即可得到消息摘要。

    Client_4.java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.Socket;
import java.security.Key;
import java.util.Scanner; public class Client_4 {
public static void main(String args[]) {
Socket mysocket;
DataInputStream in = null;
DataOutputStream out = null;
Scanner scanner = new Scanner(System.in);
String str;
try {
mysocket = new Socket("192.168.1.125", 3001);
in = new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
System.out.println("已连接服务器,请输入中缀表达式:");
str = scanner.nextLine(); Key_DH.fun("Cpub.txt","Cpri.txt");
FileInputStream fp = new FileInputStream("Cpub.txt");
ObjectInputStream bp = new ObjectInputStream(fp);
Key kp = (Key) bp.readObject();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(kp);
byte[] kb = baos.toByteArray();
out.writeUTF(kb.length + "");
for (int i = 0; i < kb.length; i++) {
out.writeUTF(kb[i] + "");
}
Thread.sleep(500);
int len = Integer.parseInt(in.readUTF());
byte np[] = new byte[len];
for (int i = 0;i<len;i++) {
String temp = in.readUTF();
np[i] = Byte.parseByte(temp);
}
ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (np));
Key k2 = (Key)ois.readObject();;
FileOutputStream f2 = new FileOutputStream("Spub.txt");
ObjectOutputStream b2 = new ObjectOutputStream(f2);
b2.writeObject(k2); KeyAgree.fun("Spub.txt","Cpri.txt");
FileInputStream f = new FileInputStream("sb.txt");
byte[] keysb = new byte[24];
f.read(keysb);
System.out.println("公共密钥:");
for (int i = 0;i<24;i++) {
System.out.print(keysb[i]+",");
}
System.out.println("");
SecretKeySpec k = new SecretKeySpec(keysb, "DESede");
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k); byte ptext[] = MyBC.tran(new StringBuffer(str)).toString().getBytes("UTF-8");
String ptextMd5 = DigestPass.fun(MyBC.tran(new StringBuffer(str)).toString());
System.out.println("明文的MD5值:"+ptextMd5);
byte ctext[] = cp.doFinal(ptext);
System.out.println("后缀表达式已被加密:");
for (int i = 0; i < ctext.length; i++) {
System.out.print(ctext[i] + ",");
}
System.out.println("");
out.writeUTF(ctext.length + "");
for (int i = 0; i < ctext.length; i++) {
out.writeUTF(ctext[i] + "");
}
out.writeUTF(ptextMd5);
String s = in.readUTF(); //in读取信息,堵塞状态
System.out.println("客户收到服务器的回应:" + s);
} catch (Exception e) {
System.out.println("服务器已断开" + e);
}
}
}

Server_4.java

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.Key;
public class Server_4 {
public static void main(String args[]) {
ServerSocket serverForClient = null;
Socket socketOnServer = null;
DataOutputStream out = null;
DataInputStream in = null;
try {
serverForClient = new ServerSocket(3001);
} catch (IOException e1) {
System.out.println(e1);
}
try {
System.out.println("等待客户呼叫");
socketOnServer = serverForClient.accept(); //堵塞状态,除非有客户呼叫
System.out.println("客户已连接");
out = new DataOutputStream(socketOnServer.getOutputStream());
in = new DataInputStream(socketOnServer.getInputStream()); Key_DH.fun("Spub.txt","Spri.txt");
int len = Integer.parseInt(in.readUTF());
byte np[] = new byte[len];
for (int i = 0;i<len;i++) {
String temp = in.readUTF();
np[i] = Byte.parseByte(temp);
}
ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream(np));
Key k2 = (Key)ois.readObject();;
FileOutputStream f2 = new FileOutputStream("Cpub.txt");
ObjectOutputStream b2 = new ObjectOutputStream(f2);
b2.writeObject(k2); FileInputStream fp = new FileInputStream("Spub.txt");
ObjectInputStream bp = new ObjectInputStream(fp);
Key kp = (Key) bp.readObject();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(kp);
byte[] kb = baos.toByteArray();
out.writeUTF(kb.length + "");
for (int i = 0; i < kb.length; i++) {
out.writeUTF(kb[i] + "");
} KeyAgree.fun("Cpub.txt","Spri.txt"); String leng = in.readUTF(); // in读取信息,堵塞状态
byte ctext[] = new byte[Integer.parseInt(leng)];
for (int i = 0;i<Integer.parseInt(leng);i++) {
String temp = in.readUTF();
ctext[i] = Byte.parseByte(temp);
}
String check = in.readUTF();
// 获取密钥
FileInputStream f = new FileInputStream("sb.txt");
byte[] keysb = new byte[24];
f.read(keysb);
System.out.println("公共密钥:");
for (int i = 0;i<24;i++) {
System.out.print(keysb[i]+",");
}
System.out.println("");
SecretKeySpec k = new SecretKeySpec(keysb, "DESede");
// 解密
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte[] ptext = cp.doFinal(ctext);
System.out.println("后缀表达式已被解密:");
for (int i = 0; i < ptext.length; i++) {
System.out.print(ptext[i] + ",");
}
System.out.println("");
// 显示明文
String p = new String(ptext, "UTF8");
String pMd5 = DigestPass.fun(p);
System.out.println("解密后明文的MD5值:"+pMd5);
if (pMd5.equals(check)){
System.out.println("和客户端的MD5值一致");
System.out.println("服务器收到客户的请求:计算后缀表达式" + p);
out.writeUTF(MyDC.jisuan(new StringBuffer(p))+"");
}
else {
System.out.println("警告:和客户端的MD5值不一致!");
}
} catch (Exception e) {
System.out.println("客户已断开" + e);
}
}
}

DigestPass.java

import java.security.*;
public class DigestPass{
public static String fun(String ss) throws Exception{
String x=ss;
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(x.getBytes("UTF8"));
byte s[ ]=m.digest( );
String result="";
for (int i=0; i<s.length; i++){
result+=Integer.toHexString((0x000000ff & s[i]) |
0xffffff00).substring(6);
}
return result;
}
}

第五个提交点

2018-2019-2-20175323 java实验五 网络编程与安全-LMLPHP

2018-2019-2-20175323 java实验五 网络编程与安全-LMLPHP

05-11 08:15