C language uses Des_ncbc_encrypt and segment errors (core dumps) to resolve __c language

Source: Internet
Author: User

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.

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.