Chinaunix首页 | 论坛 | 博客
  • 博客访问: 715874
  • 博文数量: 130
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2198
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-29 12:48
个人简介

每一个“丑得人神共愤”的泡妞高 手都有一颗坚忍的心,这证明了人类 在绝境中毫不妥协的求生精神,反正丑都丑了,索性放开手脚大干一场,这就叫“无产阶级失去的是锁链,得到的是全世界”

文章分类

全部博文(130)

文章存档

2013年(130)

我的朋友

分类: LINUX

2013-08-27 14:16:33

方法一:
  1. #include<stdio.h>
  2. #include<openssl/md5.h>
  3. #include<string.h>

  4. int main( int argc, char **argv )
  5. {
  6.     MD5_CTX ctx;
  7.     unsigned char *data="123";
  8.     unsigned char md[16];
  9.     char buf[33]={'\0'};
  10.     char tmp[3]={'\0'};
  11.     int i;

  12.     MD5_Init(&ctx);
  13.     MD5_Update(&ctx,data,strlen(data));
  14.     MD5_Final(md,&ctx);

  15.     for( i=0; i<16; i++ ){
  16.     sprintf(tmp,"%02X",md[i]);
  17.     strcat(buf,tmp);
  18.     }
  19.     printf("%s\n",buf);
  20.     return 0;
  21. }
输出:
202CB962AC59075B964B07152D234B70

方法二:
  1. #include<stdio.h>
  2. #include<openssl/md5.h>
  3. #include<string.h>

  4. int main( int argc, char **argv )
  5. {
  6.     unsigned char *data = "123";
  7.     unsigned char md[16];
  8.     char buf[33]={'\0'};
  9.     char tmp[3]={'\0'};
  10.     int i;

  11.     MD5(data,strlen(data),md);

  12.     for (i = 0; i < 16; i++){
  13.     sprintf(tmp,"%2.2x",md[i]);
  14.     strcat(buf,tmp);
  15.     }
  16.     printf("%s\n",buf);
  17.     return 0;
  18. }
输出:
202cb962ac59075b964b07152d234b70


1.一定要使用unsigned char,使用char会出问题。这个md5加密函数,返回16个十进制数,范围在0~255间,把它format为十六进制就是32为md5编码了。
2.gcc编译的时候,后面加上参数 -lcrypto 如果系统没有安装libcrypto,是不能用这个方法的。

阅读(4458) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~