Java's second experiment--java object-oriented programming
Beijing Institute of Electronic Technology
Lab Report
Course: Java Programming Class: 1352 name: Linhan Kam No.: 20135213
Score: Instructor: Lou Jia Peng Experimental Date: 2015.6.11
Experiment level: Preview degree: Experiment time: 15:30~22:00
Instrument Group: 13 Compulsory/elective: Elective experiment number: 5
Experiment Name: Java Network programming and security
Experimental purposes and requirements:
Objective: 1. Master the socket program writing;
2. Mastering the use of cryptographic technology;
3. Design Safety
Requirements: 1. Complete information encryption
2. Send information after encryption
Experimental instrument:
|
Name |
Model |
Number |
|
Pc |
Acer |
1 |
|
|
|
|
Experiment Code
Package server;
Import java.net.*;
Import java.io.*;
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.*;
public class Servert {
public static void Main (String args[]) throws Exception {
ServerSocket link = null;
Socket socket = NULL;
try {
link = new ServerSocket (8080);//Create a server socket
SYSTEM.OUT.PRINTLN ("Port number:" + link.getlocalport ());
SYSTEM.OUT.PRINTLN ("Server has started ...");
Socket = Link.accept (); Waiting for client connections
SYSTEM.OUT.PRINTLN ("Connection already established");
Get a reference to a network input stream object
BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Get a reference to a network output stream object
PrintWriter out = new PrintWriter (new BufferedWriter (New OutputStreamWriter (Socket.getoutputstream ())), true);
Decryption of DES keys using the private key of the server-side RSA
String line = In.readline ();
BigInteger cipher = new BigInteger (line);
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 ();//mod n
BigInteger m = Cipher.modpow (d, N);//m=d (mod n)
byte[] keykb = M.tobytearray ();
Use Des to decrypt ciphertext
String ReadLine = In.readline ();//Read data sent by the client
FileInputStream F2 = new FileInputStream ("Keykb1.dat");
int num2 = f2.available ();
byte[] Ctext = Parsehexstr2byte (ReadLine);
Key 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");//Encoding conversion
System.out.println ("received from the client information:" + P); Print decryption results
Detecting plaintext integrity using the hash function
String Aline3 = In.readline ();
String x = p;
MessageDigest m2 = messagedigest.getinstance ("MD5");//Use MD5 algorithm to return the MessageDigest object that implements the specified digest algorithm
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);
if (aline3.equals (result)) {
System.out.println ("match success");
}
Out.println ("match success");
Out.close ();
In.close ();
Link.close ();
} catch (Exception e) {
System.out.println (e);
}
}
Binary conversion to 16, prevents byte[] data loss caused by conversion of number to string type
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 ());//Convert lowercase letters in a string to uppercase letters and then add them to the string
}
return sb.tostring ();
}
Converting 16 binary to binary
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;
}
}
Measured operation diagram:
Experimental summary
1, pair-coded PSP time
|
step |
time-consuming min |
percent |
|
demand analysis |
60 |
20% |
|
design |
60 |
20% |
|
code implementation |
120 |
40%&NBSP; |
|
test |
30 |
10% |
|
Analysis summary |
30 |
10% |
2, encountered problems and solutions:
(1) IP address does not look up
Workaround: Open run, enter cmd, and enter ipconfig
(2) Always show connection timeout
Solution: One person connected to WiFi, then open the free WiFi network, the connection is successful.
3. Summary of Feelings
When solving practical problems with the program, the collaborators can find the mistakes they can't find, and propose different solutions and expand their thinking. The last experiment is a little bit difficult, although the basic code is given, but the network connection between computers, code combinations and other issues are still very tricky. Very test ability.
Experiment five--java network programming and security