1. Install SSL library files first
sudo apt-get install Libssl-dev
Introducing Header Files
#include <openssl/des.h>
2. Define calling Method
void Call_cbc () {Des_cblock key; Des_random_key (&key); Random number algorithm produces 64 random bit des_string_to_key ("pass", &key);//String generates key Des_key_schedule schedule;
Literal meaning is cipher table des_set_key_checked (&key, &schedule);
printf ("CBC mode encryption and decryption: \ n"); unsigned char *input = "My name is Xuhuayong";//Encrypted input string long len = (strlen (input) +7)/8*8;//length printf ("Before Encryption:%d\n", Len
);
unsigned char *output= (char*) malloc (len);
Des_cblock Ivec;
memset ((char*) &ivec, 0, sizeof (IVEC));//ivec Clear 0 printf ("Before Encryption:%s\n", input);
Des_ncbc_encrypt (input, output, strlen (input), &schedule, &ivec, Des_encrypt);
printf ("Encrypted:");
int i;
for (i = 0; i < strlen (output); i++) printf ("%02x", output[i);//Print two-bit 16-digit printf ("\ n") per character;
memset ((char*) &ivec, 0, sizeof (IVEC)); input= (char*) malloc (len);
。。。。。。
Des_ncbc_encrypt (output, input, Len, &schedule, &ivec, Des_decrypt);
printf ("After decryption:%s\n", input); }
If the above program does not annotate the sentence, there will be "paragraph error (core has been dumped)", meaning that the input pointer to a fixed allocation of space,
Content cannot be changed to raise a segment error, and the annotation section is allocated space again, you can assign a value.
For example:
unsigned char *input1 = "My name is Xuhuayong";//Encrypted input string
unsigned char input2[21] = "My name is Xuhuayong";//Encrypted input string
s trcpy (INPUT1, "ABCD");
strcpy use assignment also appears "segment error (Core dump)", but instead of allocating space for it or changing it to input2, there is no error.