Experiment five TCP transmission and encryption

Source: Internet
Author: User

Experimental content:

1. Run the textbook on TCP code, pair, one-person server, one-person client;

2. The use of encryption and decryption code package, compile and run code, one person encryption, one person decryption;

3. Integrated code, one person encrypted after sending via TCP;

Note: Encryption uses AES or Des/aes or DES encryption key keys and sends, using the server's public key cryptography/Public key algorithm using RSA or dh/to verify the integrity of sending information using MD5 or SHA3;

4. Use Git for version control.

5. Complete the Blog

Code:

Import java.net.*;

Import java.io.*;

Import java.security.*;

Import javax.crypto.*;

Import javax.crypto.spec.*;

public class MyServer {

/**

* @param args the command line arguments

*/

public static void Main (String args[]) {

ServerSocket sc = null;

ServerSocket SC1 = null;

Socket Socket=null;

Socket Socket1=null;

try {

sc= new ServerSocket (4431);//Create a server socket

sc1= new ServerSocket (4430);//Create a server socket

SYSTEM.OUT.PRINTLN ("Port number:" + sc.getlocalport ());

SYSTEM.OUT.PRINTLN ("Server 1 has started ...");

SYSTEM.OUT.PRINTLN ("Port number:" + sc1.getlocalport ());

SYSTEM.OUT.PRINTLN ("Server 2 has started ...");

Socket = Sc.accept (); Waiting for client connections

SYSTEM.OUT.PRINTLN ("Connection already established");

Socket1 = Sc1.accept (); Waiting for client connections

System.out.println ("1 connections already established");

Get a reference to a network input stream object

BufferedReader in = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));

BufferedReader in1 = new BufferedReader (New InputStreamReader (Socket1.getinputstream ()));

Get a reference to a network output stream object

System.out.print ("Received \ n");

PrintWriter out=new PrintWriter (New BufferedWriter (OutputStreamWriter ()), true);

String aline=in.readline ();//Read data sent by the client

String Aline1=in1.readline ();

System.out.print ("write to file ... \ n");

byte [] bytes = Aline.getbytes ("GB2312");

byte [] bytes1 = Aline1.getbytes ("GB2312");

Aline = new String (bytes, "GB2312");

Aline1 = new String (bytes1, "GB2312");

FileOutputStream s=new FileOutputStream ("Key1.dat");

S.write (bytes);

FileOutputStream s1=new FileOutputStream ("SEnc.dat");

S1.write (BYTES1);

System.out.print ("Write file \ n");

Generate decryption key

FileInputStream f=new FileInputStream ("Key1.dat");

ObjectInputStream b=new ObjectInputStream (f);

Key k= (Key) B.readobject ();

Byte[] kb=k.getencoded ();

FileOutputStream f2=new FileOutputStream ("Keykb1.dat");

F2.write (KB);

System.out.print ("Generate decryption key \ n");

Print the contents of the key encoding

for (int i=0;i<kb.length;i++) {

System.out.print (kb[i]+ ",");

}

Decrypt

FileInputStream a=new FileInputStream ("SEnc.dat");

int num=a.available ();

Byte[] Ctext=new Byte[num];

A.read (Ctext);

FileInputStream f1=new FileInputStream ("Keykb1.dat");

int num2=f1.available ();

Byte[] Keykb=new byte[num2];

F1.read (KEYKB);

Secretkeyspec e=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, "GB2312");

System.out.print ("Decrypt ... \ n");

SYSTEM.OUT.PRINTLN (P);

Return

System.out.println ("received from the client information:" +p); Returns results to the client over a network output stream

OUT.PRINTLN (P);

Out.close ();

In.close ();

Sc.close ();

} catch (Exception e) {

System.out.println (e);

}

}

}

Operation Result:

Problems encountered:

1, the package in the integration of the program is often prone to redefinition and variable name occupation of the problem, at this time only need to write off the duplicate definition of the part or change the volume name, but note that later the reference section also needs to change the variable name.

2. When the encryption key and ciphertext file are packaged and sent, the server cannot distinguish two data streams. Redefine a new port 4430, sending two files through two different ports 4431 and 4430 respectively.

3. String data cannot be converted to FileOutputStream type data after the file is sent and cannot be written to the file. By querying the data, the bytes are obtained according to GB2312 (get multibyte string)

byte [] bytes = String.getbytes ("GB2312");

Get UNICODE strings by GB2312 from bytes

string = new string (bytes, "GB2312");

Writes a byte string that has been converted to a specified encoding by using the Stream class

OutputStream OS = new FileOutputStream ("1.txt");

Os.write (bytes);

Os.close ();

Experiment Experience:

This experiment is my partner and the completion of the pair, although most of the code is ready, but the integration is very troublesome, to the DES encryption, RSA Encryption code in a program and to compile the success is a very difficult thing, accidentally will be flawed, and in the network connection, transmission, Feedback also has a lot of difficult places, encountered problems often need to surf the internet to find a suitable solution, in the Code of continuous debugging, improvement, debugging, improvement, the final compilation success.

Experiment five TCP transmission and encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.