20145307 Fifth experimental report on Java programming
Experimental report of Beijing Institute of Electronic Technology (BESTI)
Lesson: Java Programming
Class: 1453
Instructor: Lou Jia Peng
Date of experiment: 2016.05.06
Experiment Name:
First, the contents of the experiment
1. Use the TCP code on the book to implement the server and client.
2. Client-to-server connectivity
3. Enter plaintext in the client, using DES algorithm encryption, des secret key is encrypted with the public key of the server in the RSA public key password, calculates the hash function value of the plaintext, and transmits it to the client
4. The client uses the RSA public key password in the server's private key to decrypt the DES, the secret key, with the secret key to decrypt the ciphertext, obtains the clear text. Calculates the value of the hash function for the clear text, checks whether it is consistent with the transmission, and if so, indicates a successful match.
Second, the experimental requirements
1. Students who do not have a Linux base are advised to start with the Linux basics (new version) Vim Editor course
2. Complete the experiment, write the experiment Report, the experiment report is published in the blog site blog, note that the experimental report is focused on the results of the operation, problems encountered (tool search, installation, use, program editing, commissioning, operation, etc.), solutions (empty methods such as "Check Network", "Ask classmates", "reading" 0 points) as well as analysis (what can be learned from it, what gains, lessons, etc.). The report can refer to the guidance of Fan Fei Dragon Teacher
3. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added.
Third, the experimental steps
Secure transmission based on Java sockets
Implementation of client and server based on TCP, pair programming one person is responsible for the client, one person is responsible for the server
Using Git for version control
The DES algorithm is used for data encryption and decryption.
Iv. Experimental Process
First get the IPv4 address of the server
Enter ipconfig on the command line
Run results (take this machine as an example)
To create a socket object that connects to a specified port on a specific server
Here is the server's IP address and port number, the port number should be careful and the server consistent.
Socket socket = new Socket ("192.168.253.1", 9999);
Move the public key to a sibling folder in SRC
Copy skey_rsa_priv.dat\skey_rsa_pub.dat two files to server and client
This experiment acts as a client role
The key of DES is decrypted using the private key of the server-side RSA, and the cipher is decrypted after the key is decrypted using DES.
Run on the server first, the display server is started, and then run on the client.
The experiment code is as follows
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 Tcp_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 ("192.168.253.1", 9999);
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));
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 ("Please enter data to be sent:");
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);
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 ();
SYSTEM.OUT.PRINTLN ("The result received from the server is:" + 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 ();
}
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;
}
}
20145307 fifth time Java Learning Experiment Report