20155331实验五 网络编程与安全
网络编程与安全-1
两人一组结对编程:
-
参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA
-
结对实现中缀表达式转后缀表达式的功能 MyBC.java
-
结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能,调用MyDC.java
截图:
代码:
while (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();
//如果是运算符,调用isOperator
if (this.isOperator(token))
{
if(token.charAt(0)==')')
while (Nch!='(')
{
Nch=this.stack.pop();
if(Nch!='(')
Nextexpr=Nextexpr+" "+Nch;
}
Nch=' ';
ch=this.stack.pop();
this.stack.push(ch);
if(('*'==token.charAt(0)&&(ch=='+'||ch=='-'))||('*'==token.charAt(0)&&ch==' ')||('*'==token.charAt(0)&&ch=='(')||
('/'==token.charAt(0)&&(ch=='+'||ch=='-'))||('/'==token.charAt(0)&&ch==' ')||('/'==token.charAt(0)&&ch=='(')
||('-'==token.charAt(0)&&(ch=='('||ch==' '))
||('+'==token.charAt(0)&&(ch=='('||ch==' '))
|| '('==token.charAt(0))
this.stack.push(token.charAt(0));
else if(token.charAt(0)!=')'){
Nextexpr=Nextexpr+" "+this.stack.pop();
ch=this.stack.pop();
this.stack.push(ch);
if(!(('*'==token.charAt(0)&&(ch=='+'||ch=='-'))||('*'==token.charAt(0)&&ch==' ')||('*'==token.charAt(0)&&ch=='(')||
('/'==token.charAt(0)&&(ch=='+'||ch=='-'))||('/'==token.charAt(0)&&ch==' ')||('/'==token.charAt(0)&&ch=='(')
||('-'==token.charAt(0)&&(ch=='('||ch==' '))
||('+'==token.charAt(0)&&(ch=='('||ch==' '))
|| '('==token.charAt(0))){
Nextexpr=Nextexpr+" "+this.stack.pop();
}
this.stack.push(token.charAt(0));
}
}
else//如果是操作数
{
Nextexpr=Nextexpr+" "+token;
}
}
while(!this.stack.empty())
{
Nextexpr=Nextexpr+" "+this.stack.pop();
}
网络编程与安全-2
结对编程:1人负责客户端,一人负责服务器
-
注意责任归宿,要会通过测试证明自己没有问题
-
基于Java Socket实现客户端/服务器功能,传输方式用TCP
-
客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式通过网络发送给服务器
-
服务器接收到后缀表达式,调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
-
客户端显示服务器发送过来的结果
截图:
代码:
public class MyStack<T> implements SStack<T> {
private Object element[];
private int top;
public MyStack(int size){
this.element = new Object[Math.abs(size)];
this.top = -1;
}
public MyStack() {
this(64);
}
public boolean isEmpty() {
return this.top == -1;
}
public void push(T x) {
if(x==null)
return;
if(this.top == element.length-1){
Object[] temp = this.element;
this.element = new Object[temp.length*2];
for(int i = 0; i < temp.length; i++)
this.element[i] = temp[i];
}
this.top++;
this.element[this.top] = x;
}
public T pop() {
return this.top==-1 ? null:(T)this.element[this.top--];
}
public T get() {
return this.top==-1 ? null:(T)this.element[this.top];
}
}
sstack:
public interface SStack<T> {
boolean isEmpty();
void push(T x);
T pop();
T get();
}
Client:
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main(String srgs[]) {
try {
//创建连接特定服务器的指定端口的Socket对象
Socket socket = new Socket("127.0.0.1", 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));
System.out.print("请输入待发送的数据:");
String str=stdin.readLine(); //从键盘读入待发送的数据
String postfix = MyBC.toPostfix(str);
out.println(postfix); //通过网络传送到服务器
str=in.readLine();//从网络输入流读取结果
System.out.println( "从服务器接收到的结果为:"+str); //输出服务器返回的结果
}
catch (Exception e) {
System.out.println(e);
}
finally{
//stdin.close();
//in.close();
//out.close();
//socket.close();
}
}
}
Service:
import java.net.*;
import java.io.*;
public class TCPServer{
public static void main(String srgs[]) {
ServerSocket sc = null;
Socket socket=null;
try {
NewMyDC evaluator = new NewMyDC();
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 aline=in.readLine();//读取客户端传送来的数据
System.out.println("从客户端接收到信息为:"+aline); //通过网络输出流返回结果给客户端
int result = evaluator.value(aline);
out.println("Echo:" + result);
out.close();
in.close();
sc.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
网络编程与安全-3
加密结对编程:1人负责客户端,一人负责服务器
-
注意责任归宿,要会通过测试证明自己没有问题
-
基于Java Socket实现客户端/服务器功能,传输方式用TCP
-
客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器
-
服务器接收到后缀表达式表达式后,进行解密(和客户端协商密钥,可以用数组保存),然后调用MyDC.java的
功能计算后缀表达式的值,把结果发送给客户端 -
客户端显示服务器发送过来的结果
截图:
代码:
Client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.*;
import java.security.*;
import java.util.Arrays;
import javax.crypto.*;
public class SocketClient {
// 搭建客户端
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyBC mybc=new MyBC();
try {
Socket socket = new Socket("192.168.43.166",5329);
System.out.println("客户端启动成功");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter write = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
System.out.println("Client:" );
readline = br.readLine();
String line=mybc.evaluate(readline);
FileInputStream f=new FileInputStream("key1.dat");
ObjectInputStream b=new ObjectInputStream(f);
Key k=(Key)b.readObject();
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[]=line.getBytes("UTF-8");
byte ctext[]=cp.doFinal(ptext);
String Str=new String(ctext,"ISO-8859-1");
write.println(Str);
write.flush();
System.out.println("Server return:" + in.readLine());
write.close();
in.close();
socket.close();
} catch (Exception e) {
System.out.println("can not listen to:" + e);
}
}
}
Service:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SocketService {
public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
SocketService socketService = new SocketService();
try {
socketService.oneServer();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
public void oneServer() throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyDC mydc=new MyDC();
try{
ServerSocket server=null;
try{
server=new ServerSocket(5204);
System.out.println("服务器启动成功");
}catch(Exception e) {
System.out.println("没有启动监听:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer=new PrintWriter(socket.getOutputStream());
line=in.readLine();
System.out.printf("密文=%s
",line);
// 获取密钥
byte[]ctext=line.getBytes("ISO-8859-1");
FileInputStream f2=new FileInputStream("keykb1.dat");
int num2=f2.available();
byte[ ] keykb=new byte[num2];
System.out.printf("
");
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);
// 显示明文
String p=new String(ptext,"UTF8");
System.out.println("明文:"+p);
int a;
String Np="";
for(int i=1;i<p.length();i++)
Np+=p.charAt(i);
a=mydc.evaluate(Np);
writer.println(a);
writer.flush();
writer.close();
in.close();
socket.close();
server.close();
}catch(Exception e) {
System.out.println("Error."+e);
}
// 获取密文
}
}
网络编程与安全-4
密钥分发结对编程:1人负责客户端,一人负责服务器
-
注意责任归宿,要会通过测试证明自己没有问题
-
基于Java Socket实现客户端/服务器功能,传输方式用TCP
-
客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文发送给服务器
-
客户端和服务器用DH算法进行3DES或AES算法的密钥交换
-
服务器接收到后缀表达式表达式后,进行解密,然后调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
-
客户端显示服务器发送过来的结果
截图:
代码:
service:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SocketService {
public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
SocketService socketService = new SocketService();
try {
socketService.oneServer(args);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
public void oneServer(String []args) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyDC mydc=new MyDC();
try{
ServerSocket server=null;
try{
server=new ServerSocket(5329);
System.out.println("服务器启动成功");
}catch(Exception e) {
System.out.println("没有启动监听:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer=new PrintWriter(socket.getOutputStream());
line=in.readLine();
System.out.printf("密文=%s
",line);
// 获取密钥
byte[]ctext=line.getBytes("ISO-8859-1");
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream(args[0]);
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream(args[1]);
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();
byte[]ssb=new byte[24];
for(int i=0;i<24;i++)
ssb[i]=sb[i];
SecretKeySpec k=new SecretKeySpec(ssb,"DESede");
// 解密
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte []ptext=cp.doFinal(ctext);
// 显示明文
String p=new String(ptext,"UTF8");
int a;
System.out.println("明文:"+p);
String Np="";
for(int i=1;i<p.length();i++)
Np+=p.charAt(i);
a=mydc.evaluate(Np);
writer.println(a);
writer.flush();
writer.close();
in.close();
socket.close();
server.close();
}catch(Exception e) {
System.out.println("Error."+e);
}
// 获取密文
}
}
client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.*;
import java.security.*;
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class SocketClient {
// 搭建客户端
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyBC mybc=new MyBC();
try {
Socket socket = new Socket("192.168.43.252", 5219);
System.out.println("客户端启动成功");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter write = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
System.out.println("Client:" );
readline = br.readLine();
String line=mybc.evaluate(readline);
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream(args[0]);
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream(args[1]);
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();
System.out.println(sb.length);
byte[]ssb=new byte[24];
for(int i=0;i<24;i++)
ssb[i]=sb[i];
Key k=new SecretKeySpec(ssb,"DESede");
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[]=line.getBytes("UTF-8");
byte ctext[]=cp.doFinal(ptext);
String Str=new String(ctext,"ISO-8859-1");
write.println(Str);
write.flush();
System.out.println("Server return:" + in.readLine());
write.close();
in.close();
socket.close();
} catch (Exception e) {
System.out.println("can not listen to:" + e);
}
}
}
网络编程与安全-5
完整性校验结对编程:1人负责客户端,一人负责服务器
-
注意责任归宿,要会通过测试证明自己没有问题
-
基于Java Socket实现客户端/服务器功能,传输方式用TCP
-
客户端让用户输入中缀表达式,然后把中缀表达式调用MyBC.java的功能转化为后缀表达式,把后缀表达式用3DES或AES算法加密通过网络把密文和明文的MD5値发送给服务器
-
客户端和服务器用DH算法进行3DES或AES算法的密钥交换
-
服务器接收到后缀表达式表达式后,进行解密,解密后计算明文的MD5值,和客户端传来的MD5进行比较,一致则调用MyDC.java的功能计算后缀表达式的值,把结果发送给客户端
-
客户端显示服务器发送过来的结果
截图:
代码:
Service:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SocketService {
public static void main(String[] args) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
SocketService socketService = new SocketService();
try {
socketService.oneServer(args);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
}
public void oneServer(String []args) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyDC mydc=new MyDC();
try{
ServerSocket server=null;
try{
server=new ServerSocket(5329);
System.out.println("服务器启动成功");
}catch(Exception e) {
System.out.println("没有启动监听:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer=new PrintWriter(socket.getOutputStream());
line=in.readLine();
System.out.printf("密文=%s
",line);
// 获取密钥
byte[]ctext=line.getBytes("ISO-8859-1");
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream(args[0]);
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream(args[1]);
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();
byte[]ssb=new byte[24];
for(int i=0;i<24;i++)
ssb[i]=sb[i];
SecretKeySpec k=new SecretKeySpec(ssb,"DESede");
// 解密
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, k);
byte []ptext=cp.doFinal(ctext);
// 显示明文
String p=new String(ptext,"UTF8");
int a;
System.out.println("明文:"+p);
String Np="";
for(int i=1;i<p.length();i++)
Np+=p.charAt(i);
a=mydc.evaluate(Np);
writer.println(a);
writer.flush();
writer.close();
in.close();
socket.close();
server.close();
}catch(Exception e) {
System.out.println("Error."+e);
}
// 获取密文
}
}
Client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.*;
import java.security.*;
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class SocketClient {
// 搭建客户端
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MyBC mybc=new MyBC();
try {
Socket socket = new Socket("192.168.43.252", 5219);
System.out.println("客户端启动成功");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter write = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
System.out.println("Client:" );
readline = br.readLine();
String line=mybc.evaluate(readline);
// 读取对方的DH公钥
FileInputStream f1=new FileInputStream(args[0]);
ObjectInputStream b1=new ObjectInputStream(f1);
PublicKey pbk=(PublicKey)b1.readObject( );
//读取自己的DH私钥
FileInputStream f2=new FileInputStream(args[1]);
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();
System.out.println(sb.length);
byte[]ssb=new byte[24];
for(int i=0;i<24;i++)
ssb[i]=sb[i];
Key k=new SecretKeySpec(ssb,"DESede");
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[]=line.getBytes("UTF-8");
byte ctext[]=cp.doFinal(ptext);
String Str=new String(ctext,"ISO-8859-1");
write.println(Str);
write.flush();
System.out.println("Server return:" + in.readLine());
write.close();
in.close();
socket.close();
} catch (Exception e) {
System.out.println("can not listen to:" + e);
}
}
}