This article, a jun, with everyone to achieve a simple but also very practical encryption method.

Let everyone know the process of data encryption in the actual project development.

First, a common network communication encryption process

There are many algorithms of encryption, and the actual implementation process is very different.

The following figure is a common network communication encrypted application scenario.

Some of the cryptographic machines:

The client server side can set the cryptographic machine (can be software, or a hardware, can also be on a server locally, as long as the key can be generated), the KeyGen and the sync code affect the cryptographic machine The key sequence cryptography generates the same key sequence in the same way in the keygen and synchronization code, and the additional parties need to remember the order in which the key is generated, how many data is applied for how much data is applied.

As shown in the figure above, based on the C / S architecture server and client communication model,

The following is a process of the client if the client is to send a encrypted ciphertext to the server, C / S needs to interact.

1 server-side send key ciphertext

First of all, the server, the client saves a default key

The server is randomly generated key Keygen and uses the default key to encrypt KEYGEN, and generate key ciphertext.

The client can regularly request the key ciphertext or server timing.

After the client receives the key ciphertext, you can also decrypt the clear text by the default key.

2. The client is encrypted with data

Before sending data, the client is first generated into a synchronization code.

Set the sync code and keygen to the cryptographue, then apply for a certain length to the cryptography

Encryption by certain algorithms (usually different or), generate data ciphertext by a certain algorithm

3. Client sends synchronization code and data ciphertext

The client sends the data ciphertext and sync code to the server.

Server extracts the synchronization code

4. Server-side receive data and decrypt

The server sets the keygen and the sync code to the cryptographue, and apply for a certain number of keys.

The server decrypts the cipher according to the key to obtain the corresponding plain text

Because the server and client use the same Keygen, and the sync code, the key sequence applied for both parties must be the same.

Second, the function is realized

Below is some of the function prototypes and functional descriptions of the encryption algorithm implemented, these functions are basically the functionality of the first section.

1. Apply for encryption key functions request_Key

INTREQUEST_KEY (INTSYNC, INTKEY_NUM, CHARKEY []) function: Apply to the cryptographue to the key for encrypting data, if no new Keygen is set, then the generated password will appear in order, each application key will be recorded. The offset of the key generated last time, the next time the application is applied, will continue to allocate key parameters from the previous position: SYNC: synchronization code, the cryptographic machine generates a key_num: application for the random sequence Number of key: Cache Return Values ??for the Required Key Storage: Actually Returns the number of keys

2. Set the key sequence function set_keygen

Voidset_keygen (intKey) function: Set the keygen to the cryptographue, which will affect the generated random key sequence parameters: key: Key Return Value: None

3. Generate a random number born_seed

INTBORN_SEED (INTSYNC, INTKEY) function: Generate a random key seed parameter according to the synchronization code and Keygen: SYNC: sync code KEY: Key Return Value: Seed

4. Reset Keygen Reset_Keygen ()

VoidReset_keygen () function: Reset KEYGEN, affect the generated random number sequence

Third, test code instance

The final document is as follows:

Key.cKey.hmain.c

Example 1 A random sequence generated

INTMAIN (INTI; UNSIGNEDINTLEN; INTJ, R, Key_Num; Unsigned Indinsync = 0; UnsignedCharKey [Max_Key_Request]; key_num = 10; Printf (“

– use the default Keygen sync code = 0 generation ciphertext —————-

“); reset_keygen (); MEMSET (key, 0, sizeof (key)); len = request_key (sync, key_num, key); Print_Array (” Key 0-9: “, Key, Len); MEMSET (KEY, 0, sizeof (key)); len = request_key (sync, key_num, key); Print_Array (“Key 10-19:”, Key, LEN); Printf (“

————– use keygen = 1234 sync code = 0 generation ciphertext —-

“); set_keygen (1234); MEMSET (key, 0, sizeof (key)); len = request_key (sync, key_num, key); Print_Array (” Key 0-9: “, Key, LEN); MEMSET (Key , 0, SizeOf (key)); len = request_key (sync, key_num, key); Print_Array (“Key 10-19:”, Key, LEN);} Execution:

————– use the default keygen sync code = 0 generation ciphertext —————- key 0-9: —- [10] A552C8145DF7465B8942 Key 10-19: —- [10] 38696fa608d26939cd29 ————– use keygen = 1234 sync code = 0 generation ciphertext ——- ——— Key 0-9: —- [10] 0e830B73ECF54B4A7435 Key 10-19: —- [10] E7F10641C86BAADF0C3D

It can be seen that the random sequence produced by different Keygen is different.

If you set a different sync code, still sequences will be different.

Example 2 With the default Keygen, add confusion

Chardata0 [10] = {0X1, 0X2, 0X3, 0X4, 0X5, 0X6, 0X7, 0X8, 0X9, 0X10,}; IntMain (intargc, char * argv []) {inti; unsignedinturn; intj, r, key_num; unsignedintsync = 0; UnsignedcharKey [max_key_request]; charbuf [120] = {0}; key_num = 10; Printf (“

————– Use the default KeyGen to start encryption —————-

“); reset_keygen (); print_Array (“

: “, Data0, Key_Num); Memset (Key, 0, Sizeof (key)); len = request_key (sync, key_num, key); Print_Array (” Key: “, Key, Len); for (i = 0 i

Ciphertext: “, BUF, LEN); Printf (“

——————– Start Decryption ——————–

“); reset_keygen (); MEMSET (key, 0, sizeof (key)); len = request_key (sync, key_num, key); for (i = 0; i

Plate: “, BUF, LEN);

————– Use the default Keygen start encryption — [10] 01020304050607080910 Key: – – [10] A552C8145DF7465B8942 Cipheet: —- [10] A450CB1058F141538052 ——————– Start Decryption -[10] 01020304050607080910

Example 3 Decryption with different Keygen and synchronous code

intmain (intargc, char * argv []) {inti; unsignedintlen; intj, r, key_num; unsignedintsync = 0; unsignedcharkey [MAX_KEY_REQUEST]; charbuf [120] = {0}; unsignedintmykeygen; if (! argc = 4) {fprintf (stderr, “USAGE:% s

“, argv [0]); exit_failure;} sync = ATOI (Argv [1]); key_num = atoi (argv [2]); mykeygen = ATOI (Argv [3]); Printf (“

————– use custom Keygen, start encryption with the synchronization code ——————————————————————————

“); set_keygen; print_Array (“

: “, Data0, Key_Num); Memset (Key, 0, Sizeof (key)); len = request_key (sync, key_num, key); Print_Array (” Key: “, Key, Len); for (i = 0 i

Ciphertext: “, BUF, LEN); Printf (“

——————– Start Decryption ——————–

“); set_keygen (myKeygen); MEMSET (key, 0, sizeof (key)); len = request_key (sync, key_num, key); for (i = 0; i

: “, BUF, LEN); EXIT (EXIT_SUCCESS);} The results of the execution are as follows:

————– use custom Keygen, start encryption with synchronization code ————— : —- [10] 01020304050607080910 Key: —- [10] 530029CD27EBCC801AD7 cps: —- [10] 52022ac922edcb8813c7 ——————– Start Decryption —— ————– : —- [10] 01020304050607080910

It can be seen that we do implement data encryption and decryption.

Fourth, the actual use of data encryption

Assume that we use the above instance code to transplant the corresponding functions to both ends of C / S.

So complete data encryption and data transmission reference process is as follows:

Remember, as long as the two parties set the same Keygen and the synchronization code, the key to spit out of the password is the same sequence.

When the client sends a message, you send your own plaintext synchronization code to the server.

The server can achieve decryption operations according to the keygen and synchronization code sent to the client in advance.

Although you can see the synchronization code of the plain text,

However, it is also necessary to crack the cryptographic algorithm, and the server issued by the server.

5, principle

The main problem with the encryption algorithm is how to generate a random sequence as a key.

This example is the borrow library function RAND () prototype as follows:

#includeInTrand (Void);

Although the random sequence can be generated, the sequence produced is the same.

#include main () {inti = 0; for (i = 0; i