Symmetric encryption algorithm is an early application of encryption algorithm, the technology is mature. In the symmetric encryption algorithm, the sender of the data sends the plaintext (raw data) and the encryption key (Mi Yao) together with a special encryption algorithm, which makes it into a complex cipher cipher. After receiving the ciphertext, if you want to interpret the original text, it is necessary to decrypt the ciphertext by using the encryption key and the inverse algorithm of the same algorithm, so that it can be restored to readable plaintext. In the symmetric encryption algorithm, only one key is used, both parties use this key to encrypt and decrypt the data, which requires the decryption party must know the encryption key beforehand. The key to the symmetric encryption algorithm is the security of the key, because anyone who knows the key can get the content of the original text. The main symmetric encryption algorithms are DES/AES and so on.
Des is a packet encryption algorithm, each set of encryption, the size of each group is generally 8 bytes, for large data volume operation may have a performance problem.
In addition DES encryption includes ECB EFB CBC OFB four modes
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <openssl/des.h>
int main ()
{
Char original_data[8] = "Gaoxiang";
Char Ciphertext[8];
Char Check_data[8];
Char passwd[16];
Puts ("Please enter the key:");
Gets (passwd);
Des_key_schedule Sch;
Des_cblock K;
/******************************************************************
ECB mode
******************************************************************/
Des_string_to_key (passwd,&k); Generate key based on string
Des_set_key_unchecked (&k, &sch); Get des Key Sch via K (total 64 bits, where the last digit of each 8 bit is the check digit)
Des_ecb_encrypt ((Const_des_cblock *) Original_data,//text to be encrypted (size 8 bytes)
(Des_cblock *) ciphertext,//Encrypted text (8 bytes)
&sch, Des_encrypt); Represents the encryption process
Des_string_to_key (passwd,&k);
Des_set_key_unchecked (&k,&sch);
Des_ecb_encrypt ((Const_des_cblock *) ciphertext, (Const_des_cblock *) check_data,&sch,des_decrypt);
if (strncmp (Original_data, (const char*) Check_data, 8) ==0)
Puts ("ECB OK");
Else
Puts ("ECB bad");
/******************************************************************
CFB Mode
******************************************************************/
Des_cblock i;
memcpy (&i, "Hahahaha", 8);
Des_set_key_unchecked (&k, &sch);
Des_cfb_encrypt (original_data,ciphertext,8,
8, &sch, &i, Des_encrypt);
printf ("%s\n", ciphertext);
Des_set_key_unchecked (&k, &sch);
memcpy (&i, "Hahahaha", 8);
Des_cfb_encrypt (ciphertext, Check_data,8,
8, &sch, &i,des_decrypt);
if (strncmp (Original_data,check_data, 8) ==0)
Puts ("CFB OK");
Else
Puts ("CFB bad");
/******************************************************************
OFB mode
******************************************************************/
memcpy (&i, "Hahahaha", 8);
Des_set_key_unchecked (&k, &sch);
Des_ofb_encrypt (original_data,ciphertext,8,
8, &sch, &i);
printf ("%s\n", ciphertext);
Des_set_key_unchecked (&k, &sch);
memcpy (&i, "Hahahaha", 8);
Des_ofb_encrypt (ciphertext, Check_data,8,
8, &sch, &i);
if (strncmp (Original_data,check_data, 8) ==0)
Puts ("ofb OK");
Else
Puts ("ofb bad");
void Des_ofb_encrypt (const unsigned char *in,unsigned char *out,int numbits,long length,des_key_schedule *schedule, Des_cblock *ivec);
/******************************************************************
CBC mode
******************************************************************/
memcpy (&i, "Hahahaha", 8);
Des_set_key_unchecked (&k, &sch);
Des_ncbc_encrypt (original_data,ciphertext,8,
&sch, &i, Des_encrypt);
printf ("%s\n", ciphertext);
Des_set_key_unchecked (&k, &sch);
memcpy (&i, "Hahahaha", 8);
Des_ncbc_encrypt (ciphertext, Check_data,8,
&sch, &i,des_decrypt);
if (strncmp (Original_data,check_data, 8) ==0)
Puts ("CFC OK");
Else
Puts ("CFC bad");
}
Implementation of DES symmetric encryption algorithm