网络编程及安全
实验目的与要求
1.掌握Java网络编程的方法
2.掌握Java安全编程的方法
3.实现TCP加解密
实验内容与步骤
1 编写网络通信程序(基于TCP)
2 对通信内容使用对称加密算法进行加密
3 使用非对称算法分发对称加密中使用的密钥
客户端:
import java.net.*;
import java.security.*;
import javax.crypto.*;
import java.io.*;
public class ComputeTCPClient {
public static void main(String srgs[]) {
try {
//创建连接特定服务器的指定端口的Socket对象
Socket socket = new Socket("0.0.0.0", 4421);
//获得从服务器端来的网络输入流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获得从客户端向服务器端输出数据的网络输出流
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
FileReader f1=new FileReader("e:/muhw.txt");
BufferedReader stdin=new BufferedReader(f1);
String str=stdin.readLine();
String s=new String(str);
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[]=s.getBytes("UTF8");
byte ctext[]=cp.doFinal(ptext);
FileOutputStream f2=new FileOutputStream("SEnc.dat");
f2.write(ctext);
FileReader f3=new FileReader("SEnc.dat");
BufferedReader stdin3=new BufferedReader(f3);
String str3=stdin3.readLine();
out.println(str3); //通过网络传送密文到服务器
String str1=in.readLine();//从网络输入流读取结果
System.out.println( "服务器接收到的结果为:"+str1); //输出服务器返回的结果
}
catch (Exception e) {
System.out.println(e);
}
finally{
//stdin.close();
//in.close();
//out.close();
//socket.close();
}
}
}
服务器:
import java.net.*;
import java.security.*;
import javax.crypto.*;
import java.io.*;
public class ComputeTCPClient {
public static void main(String srgs[]) {
try {
//创建连接特定服务器的指定端口的Socket对象
Socket socket = new Socket("0.0.0.0", 4421);
//获得从服务器端来的网络输入流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获得从客户端向服务器端输出数据的网络输出流
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
FileReader f1=new FileReader("e:/muhw.txt");
BufferedReader stdin=new BufferedReader(f1);
String str=stdin.readLine();
String s=new String(str);
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[]=s.getBytes("UTF8");
byte ctext[]=cp.doFinal(ptext);
FileOutputStream f2=new FileOutputStream("SEnc.dat");
f2.write(ctext);
FileReader f3=new FileReader("SEnc.dat");
BufferedReader stdin3=new BufferedReader(f3);
String str3=stdin3.readLine();
out.println(str3); //通过网络传送密文到服务器
String str1=in.readLine();//从网络输入流读取结果
System.out.println( "服务器接收到的结果为:"+str1); //输出服务器返回的结果
}
catch (Exception e) {
System.out.println(e);
}
finally{
//stdin.close();
//in.close();
//out.close();
//socket.close();
}
}
}
DES加密:
import java.io.*;
import java.security.*;
import javax.crypto.*;
public class SEnc{
public static void main(String args[]) throws Exception{
String s="Hello world!";//需加密的明文
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[]=s.getBytes("UTF8");
for(int i=0;i<ptext.length;i++){
System.out.print(ptext[i]+",");
}
System.out.println("");
byte ctext[]=cp.doFinal(ptext);
for(int i=0;i<ctext.length;i++){
System.out.print(ctext[i] +",");
}
FileOutputStream f2=new FileOutputStream("SEnc.dat");
f2.write(ctext);
}
}
密钥加密:
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.*;
import java.io.*;
public class Enc_RSA{
public static void main(String args[]) throws Exception{
FileInputStream f1=new FileInputStream("keykb1.dat");
int num2=f1.available();
byte[ ] keykb=new byte[num2];
f1.read(keykb);
FileInputStream f=new FileInputStream("Skey_RSA_pub.dat");
ObjectInputStream b=new ObjectInputStream(f);
RSAPublicKey pbk=(RSAPublicKey)b.readObject( );
BigInteger e=pbk.getPublicExponent();
BigInteger n=pbk.getModulus();
System.out.println("e= "+e);
System.out.println("n= "+n);
//byte ptext[]=s.getBytes("UTF8");
BigInteger m=new BigInteger(keykb);
BigInteger c=m.modPow(e,n);
System.out.println("c= "+c);
String cs=c.toString( );
BufferedWriter out=
new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Enc_RSA.dat")));
out.write(cs,0,cs.length( ));
out.close( );
}
}
客户端将密文和加密密钥传给服务器端
密钥解密:
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.*;
import java.io.*;
public class Dec_RSA{
public static void main(String args[]) throws Exception{
BufferedReader in=
new BufferedReader(new InputStreamReader(new FileInputStream("Enc_RSA.dat")));
String ctext=in.readLine();
BigInteger c=new BigInteger(ctext);
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();
FileOutputStream f2=new FileOutputStream("keyb.dat");
f2.write(mt);
}
}
密文解密:
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SDec{
public static void main(String args[]) throws Exception{
FileInputStream f=new FileInputStream("SEnc.dat");
int num=f.available();
byte[ ] ctext=new byte[num];
f.read(ctext);
//FileInputStream f2=new FileInputStream("keya.dat");
FileInputStream f2=new FileInputStream("keyb.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);
String p=new String(ptext,"UTF8");
System.out.println(p);
}
}
实验测试
服务器显示:
客户端显示: