Experimental content
1. Master the socket program writing;
2. Mastering the use of cryptographic technology;
3. Design a secure transmission system.
Experimental steps
I was programming with 20145211 Huang Zhiyuan http://www.cnblogs.com/nostalgia-/Team, and I designed the client part.
-
Determine server-side IP address and port
Open the cmd command line with the computer of the design server, enter the ipconfig IPv4 address of the query server and let the user responsible for the client record, create a port number and the server is consistent with the client (preferably not 8080, 8030 of these common).
- will be
Skey_RSA_priv.dat与Skey_RSA_pub.dat copied to the same folder as the SRC sibling, so that the error "system cannot find the specified file" is not displayed after running.
-
Writing code
1. Create a socket object that is used to establish a port number to connect to the client and obtain a reference to the network input stream and the output stream object.
2. Use the private key of the server-side RSA to decrypt the key of DES, decrypt the secret key after decryption, and use Des to decrypt the ciphertext.
3. Calculate the decrypted hash value to determine if the decryption is correct.
Note: The code provided by the teacher is used in the cryptographic algorithm, secret key, and hash function calculation process.
The client code is as follows:
-
Package exp05;
/**
* Created by Administrator on 2016/5/6.
*/
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.*;
/**
* Created by Think on 2016/5/6.
*/
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 ();
To create a socket object that connects to a specified port on a specific server
Socket socket = new Socket ("192.168.43.125", 12335);//The IP address and port number of the server is entered here, and the port number should be kept consistent with the server.
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 (New OutputStreamWriter (Socket.getoutputstream ())), true);
Create a keyboard input stream so that clients can enter information from the keyboard
BufferedReader stdin = new BufferedReader (new InputStreamReader (system.in));
RSA algorithm that encrypts des keys using the server-side public key
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); Transfer encrypted secret keys to the server over the network
Encrypt plaintext with des to get ciphertext
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); Transferring ciphertext to the server over the network
Send the client plaintext hash value 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)//the hash function value of plaintext is transmitted to the server via the network
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);//Output exception
}
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;
}
}
-
Matching connections
1. Run server-side code;
2. Display "Server has been started" to run the client code;
3. The "Connection has been established" indicates that the two parties are connected;
4. The client enters the information to be transmitted;
5. The server side displays the information received from the client;
6. Both sides match successfully, and the client displays a "match succeeded" message.
- We set the port to: 12333. From the command line, you know that the IP of the server is: 192.168.43.125
- Server side:
- The server then clicks "Run" and the client then "Run", thus establishing a connection.
The client inputs the data sent:
- The server received the data:
- The problem in the experiment and the solution process
- Issue 1: the error "system cannot find the specified file" is displayed after running.
- Workaround:
Skey_RSA_priv.dat Copy to a folder that is similar to SRC.
- Issue 2: The client displays an error after the server starts
java.net.SocketException: Connection reset(连接重置) .
- Workaround: The last running window is not closed, the port number is occupied, and the last running window is closed.
- Issue 3: client displays error ' Java.net.ConnectException:Connection timed out:connect (connection timeout) after server startup.
-
Workaround: We infer that there may be a problem with the network connection. We initially used Edu WiFi at the same time, the result was a connection timeout, and then we connected the same phone to the hotspot, the result is still a connection timeout; Finally, I use the server side of the computer to the client to distribute WiFi, the result is a success, very magical.
- Experimental code Hosting
- Client code:
-
- PSP (Personal software Process) time
-
| Steps |
Take |
Percentage |
| Demand analysis |
10min |
12.5% |
| Design |
20min |
25% |
| Code implementation |
20min |
25% |
| Test |
20min |
25% |
| Analysis Summary |
10min |
12.5% |
20145326 "Java Program Design" experiment five--java Network programming and Security Experiment Report