Linux c MD5 encryption using OpenSSL

Source: Internet
Author: User
Tags md5 md5 encryption openssl openssl library printf sprintf strlen linux

Method One:

#include <stdio.h>

#include <openssl/md5.h>

#include <string.h>

int main (int argc, char **argv)

{

Md5_ctx CTX;

unsigned char *data= "123";

unsigned char md[16];

Char buf[33]={' i '};

Char tmp[3]={' i '};

int i;

Md5_init (&CTX);

Md5_update (&ctx,data,strlen (data));

Md5_final (MD,&CTX);

For (i=0 i<16; i++) {

sprintf (tmp, "%02x", Md[i]);

strcat (BUF,TMP);

}

printf ("%s\n", buf);

return 0;

}

Output:

202cb962ac59075b964b07152d234b70

Method Two:

#include <stdio.h>

#include <openssl/md5.h>

#include <string.h>

int main (int argc, char **argv)

{

unsigned char *data = "123";

unsigned char md[16];

int i;

Char tmp[3]={'},buf[33]={';

MD5 (Data,strlen (data), MD);

for (i = 0; i < i++) {

sprintf (tmp, "%2.2x", Md[i]);

strcat (BUF,TMP);

}

printf ("%s\n", buf);

return 0;

}

Output:

202cb962ac59075b964b07152d234b70

Summarize:

Two ways to achieve the same result. In contrast, the second method is more intuitive and simpler.

The problems encountered:

1.sprintf format,%02x and%2.2 format is the same, forced output two bits, such as the decimal 8, hexadecimal is also 8, this format is the control output two bits, 08. If the result of the encryption to lowercase letter display, is "%2.2x", the upper case is "2.2X."

2. Be sure to use unsigned char, although it is char, is actually an int, of course, it only has one byte, and char is the difference is that unsigned char 0~255, char-127~127. Using char can cause problems. This MD5 encryption function, which returns 16 decimal digits, ranges between 0~255, and it is 32 MD5 encoded with a format of 16.

3.GCC compile, the back with the parameter-lcrypto if the system is not installed Libcrypto, this method can not be used.

If you do not want to use the OpenSSL library function, the previous article I have MD5 encrypted source code, can be used directly.

More Wonderful content: http://www.bianceng.cn/Programming/cplus/

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.