First, Experimental Content
1, the operation of the textbook TCP code, pairing, one-man server, one-person client;
2. Use encryption and decryption code package, compile run code, client encryption, server decryption;
3. The client encrypts plaintext and sends ciphertext through TCP;
4. Encryption uses Des,des encryption key to send to the server, using the server's public key encryption, public key algorithm using RSA, verify the integrity of the sending information use MD5
5. My pairing partner is 20135108 Li Zeyuan, I am responsible for the client, Li Zeyuan is responsible for the server
Second, Experimental Steps
1. This experiment requires the interconnection of two PC machines. A PC creates a local area network, acts as a client, connects to a local area network and queries its own IP address (ipconfig), acts as a server, and then runs server code, which opens the server. Enter the content after the connection is complete.
2. Client. Combined code, you first need to connect to the server, where you need to modify the IP address and port. Then create the key--Request the connection according to the server port number--transfer data after successful connection--read the data from the keyboard and encrypt--check the connection status--request shutdown--close.
3. Sender A —————— > receiver B
A when encrypting, use B's public key
b When decrypting, use the private key of B
Third, Client Code
File Name:ComputeTCPClient.java
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 Computetcpclient {
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 ();
String Kstr=parsebyte2hexstr (KB);
To create a socket object that connects to a specified port on a specific server
Socket socket = new Socket ("192.168.1.2", 4421);
Get the network input stream from the server side
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Obtain a network output stream from the client to the server-side output data
PrintWriter out=new PrintWriter (New BufferedWriter (OutputStreamWriter ()), true);
Create a keyboard input stream so that clients can enter information from the keyboard
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
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 ();
System.out.println ("e=" +e);
System.out.println ("n=" +n);
Byte ptext2[]=kstr.getbytes ("UTF8");
BigInteger m=new BigInteger (PTEXT2);
BigInteger C=m.modpow (e,n);
System.out.println ("c=" +c);
String cs=c.tostring ();
OUT.PRINTLN (CS); Transfer over the network to the server
System.out.print ("Please enter data to be sent:");
String S=stdin.readline (); Read the data to be sent from the keyboard
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); Transfer over the network to the server
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);
Str=in.readline ();//Read results from the network input stream
SYSTEM.OUT.PRINTLN ("The result received from the server is:" +STR); Results returned by the output server
}
catch (Exception e) {
System.out.println (e);
}
finally{
Stdin.close ();
In.close ();
Out.close ();
Socket.close ();
}
}
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 ();
}
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 * + low);
}
return result;
}
}
Iv. Results
Five, the Experiment experience
Through the experiment of the first attempt of the cryptography of the two PCs to transfer information between the use of the RSA (PUB) algorithm to the DES algorithm key encryption, using DES algorithm for plaintext encryption, the use of hash value to verify the integrity of the information. Pair programming, the realization of two PC information transmission, enhance the team awareness.
The actual meaning is greater than the experimental meaning.
Java Lab (client) 2015106 Song