标签:test put vax 算法 void sage socket add 中学
1 import java.util.StringTokenizer;
2 import java.util.Stack;
3
4 public class MyDC
5 {
6 /** constant for addition symbol */
7 private final char ADD = ‘+‘;
8 /** constant for subtraction symbol */
9 private final char SUBTRACT = ‘-‘;
10 /** constant for multiplication symbol */
11 private final char MULTIPLY = ‘*‘;
12 /** constant for division symbol */
13 private final char DIVIDE = ‘/‘;
14 /** the stack */
15 private Stack<Integer> stack;
16
17 /**
18 * Sets up this evalutor by creating a new stack.
19 */
20 public MyDC()
21 {
22 stack = new Stack<Integer>();
23 }
24
25 public int evaluate (String expr)
26 {
27 int op1, op2, result = 0;
28 String token;
29 StringTokenizer tokenizer = new StringTokenizer (expr);
30
31 while (tokenizer.hasMoreTokens())
32 {
33 token = tokenizer.nextToken();
34
35 if (isOperator(token))
36 {
37 op2 = (stack.pop()).intValue();
38 op1 = (stack.pop()).intValue();
39 result = evalSingleOp (token.charAt(0), op1, op2);
40 stack.push (new Integer(result));
41 }
42 else
43 stack.push (new Integer(Integer.parseInt(token)));
44 }
45
46 return result;
47 }
48
49 private boolean isOperator (String token)
50 {
51 return ( token.equals("+") || token.equals("-") ||
52 token.equals("*") || token.equals("/") );
53 }
54
55 private int evalSingleOp (char operation, int op1, int op2)
56 {
57 int result = 0;
58
59 switch (operation)
60 {
61 case ADD:
62 result = op1 + op2;
63 break;
64 case SUBTRACT:
65 result = op1 - op2;
66 break;
67 case MULTIPLY:
68 result = op1 * op2;
69 break;
70 case DIVIDE:
71 result = op1 / op2;
72 }
73
74 return result;
75 }
76 }44 }
45
46 return result;
47 }
48
49 private boolean isOperator (String token)
50 {
51 return ( token.equals("+") || token.equals("-") ||
52 token.equals("*") || token.equals("/") );
53 }
54
55 private int evalSingleOp (char operation, int op1, int op2)
56 {
57 int result = 0;
58
59 switch (operation)
60 {
61 case ADD:
62 result = op1 + op2;
63 break;
64 case SUBTRACT:
65 result = op1 - op2;
66 break;
67 case MULTIPLY:
68 result = op1 * op2;
69 break;
70 case DIVIDE:
71 result = op1 / op2;
72 }
73
74 return result;
75 }
76 }
测试代码:
1 import java.util.Scanner;
2
3 public class MyDCTester {
4
5 public static void main (String[] args) {
6
7 String expression, again;
8
9 int result;
10
11 try
12 {
13 Scanner in = new Scanner(System.in);
14
15 do
16 {
17 MyDC evaluator = new MyDC();
18 System.out.println ("Enter a valid postfix expression: ");
19 expression = in.nextLine();
20
21 result = evaluator.evaluate (expression);
22 System.out.println();
23 System.out.println ("That expression equals " + result);
24
25 System.out.print ("Evaluate another expression [Y/N]? ");
26 again = in.nextLine();
27 System.out.println();
28 }
29 while (again.equalsIgnoreCase("y"));
30 }
31 catch (Exception IOException)
32 {
33 System.out.println("Input exception reported");
34 }
35 }
36 }
运行结果:
服务器代码:
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(10001);//创建服务器套接字
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<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 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 Client
{
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();
Socket socket = new Socket("10.43.62.8", 10001);
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));
//RSA算法,使用服务器端的公钥对DES的密钥进行加密
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();
BigInteger m = new BigInteger(ptext2);
BigInteger c = m.modPow(e, n);
String cs = c.toString();
out.println(cs); // 通过网络将加密后的秘钥传送到服务器
System.out.print("请输入待发送的数据:");
//用DES加密明文得到密文
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); // 通过网络将密文传送到服务器
// 将客户端明文的Hash值传送给服务器
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);//通过网络将明文的Hash函数值传送到服务器
str = in.readLine();// 从网络输入流读取结果
System.out.println("从服务器接收到的结果为:" + str); // 输出服务器返回的结果
}
catch (Exception e)
{
System.out.println(e);//输出异常
}
finally
{
}
}
//将十六进制转换成二进制
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();
}
}
步骤 | 耗时 | 百分比 |
---|---|---|
需求分析 | 10min | 9.1% |
设计 | 15min | 13.6% |
代码实现 | 40min | 36.4% |
测试 | 25min | 22.7% |
分析总结 | 20min | 18.2% |
这次实验是我们的最后一次实验了,感觉5次实验做下来之后自己的收获真的是特别大,深刻体会到了老师口中的做中学是什么意思,懂得了把知识运用到实践之中去,感觉自己已经入了一点门,我下学期一定会更加努力的。
标签:test put vax 算法 void sage socket add 中学
原文地址:http://www.cnblogs.com/xuzihan/p/6939664.html