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/