md5c語言實現
① 求c語言調用md5函數的函數,注意是直接輸入字元串就能獲得的,不要輸入文件名的那種
c語言標准庫沒有這個函數,隨便給你搜了一下別人的實現
/*md5.h*/
#ifndef MD5_H
#define MD5_H
typedef struct
{
unsigned int count[2];
unsigned int state[4];
unsigned char buffer[64];
}MD5_CTX;
#define F(x,y,z) ((x & y) | (~x & z))
#define G(x,y,z) ((x & z) | (y & ~z))
#define H(x,y,z) (x^y^z)
#define I(x,y,z) (y ^ (x | ~z))
#define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
#define FF(a,b,c,d,x,s,ac) \
{ \
a += F(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define GG(a,b,c,d,x,s,ac) \
{ \
a += G(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define HH(a,b,c,d,x,s,ac) \
{ \
a += H(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define II(a,b,c,d,x,s,ac) \
{ \
a += I(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
void MD5Init(MD5_CTX *context);
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen);
void MD5Final(MD5_CTX *context,unsigned char digest[16]);
void MD5Transform(unsigned int state[4],unsigned char block[64]);
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len);
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len);
#endif
/*md5.c*/
#include <memory.h>
#include "md5.h"
unsigned char PADDING[]={0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void MD5Init(MD5_CTX *context)
{
context->count[0] = 0;
context->count[1] = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
}
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen)
{
unsigned int i = 0,index = 0,partlen = 0;
index = (context->count[0] >> 3) & 0x3F;
partlen = 64 - index;
context->count[0] += inputlen << 3;
if(context->count[0] < (inputlen << 3))
context->count[1]++;
context->count[1] += inputlen >> 29;
if(inputlen >= partlen)
{
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i = partlen;i+64 <= inputlen;i+=64)
MD5Transform(context->state,&input[i]);
index = 0;
}
else
{
i = 0;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}
void MD5Final(MD5_CTX *context,unsigned char digest[16])
{
unsigned int index = 0,padlen = 0;
unsigned char bits[8];
index = (context->count[0] >> 3) & 0x3F;
padlen = (index < 56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);
}
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[j] = input[i] & 0xFF;
output[j+1] = (input[i] >> 8) & 0xFF;
output[j+2] = (input[i] >> 16) & 0xFF;
output[j+3] = (input[i] >> 24) & 0xFF;
i++;
j+=4;
}
}
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[i] = (input[j]) |
(input[j+1] << 8) |
(input[j+2] << 16) |
(input[j+3] << 24);
i++;
j+=4;
}
}
void MD5Transform(unsigned int state[4],unsigned char block[64])
{
unsigned int a = state[0];
unsigned int b = state[1];
unsigned int c = state[2];
unsigned int d = state[3];
unsigned int x[64];
MD5Decode(x,block,64);
FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
/* Round 2 */
GG(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */
HH(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
/* Round 4 */
II(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
II(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
II(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
II(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
II(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
II(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
② C語言求文件MD5的函數用法
C語言沒有內置的MD5函數,可以在以下頁面找到C語言實現程序和MD5的演算法介紹:
http://tools.ietf.org/html/rfc1321
③ 用c語言實現python的md5功能
題中所示代碼中,python實現了計算空字元串的MD5值,並對MD5的值的十六進制的字元串所表示的位元組進行BASE64處理。
不像Python內部有實現md5功能,根據ANSI C標准,C語言的標准庫里是沒有md5功能的;
但是RFC1231規定了MD5功能的C實現並提供了附件,可以直接用,也可以直接獲取現成的實現,在編譯鏈接時指定正確的.h頭文件和.lib靜態鏈接庫文件;
這里我採取前者的做法(電腦上沒有裝VC,有VC就簡單很多,使用的是minGW)大概六七百行代碼左右。
然後這里展示不完,給個實現效果圖
BASE64的編碼原理
④ c語言實現MD5加密演算法,出現問題求解
找不到md5.h的頭文件 將你的md5.h頭文件跟你的c文件放在同一目錄下
⑤ 請教MD5演算法 用C語言實現
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifdefined(__APPLE__)
#defineCOMMON_DIGEST_FOR_OPENSSL
#include<CommonCrypto/CommonDigest.h>
#defineSHA1CC_SHA1
#else
#include<openssl/md5.h>
#endif
//這是我自己寫的函數,用於計算MD5
//參數str:要轉換的字元串
//參數lengthL:字元串的長度可以用strlen(str)直接獲取參數str的長度
//返回值:MD5字元串
char*str2md5(constchar*str,intlength){
intn;
MD5_CTXc;
unsignedchardigest[16];
char*out=(char*)malloc(33);
MD5_Init(&c);
while(length>0){
if(length>512){
MD5_Update(&c,str,512);
}else{
MD5_Update(&c,str,length);
}
length-=512;
str+=512;
}
MD5_Final(digest,&c);
for(n=0;n<16;++n){
snprintf(&(out[n*2]),16*2,"%02x",(unsignedint)digest[n]);
}
returnout;
}
intmain(intargc,char**argv){
char*output=str2md5("hello",strlen("hello"));
printf("%s ",output);
//上面會輸出hello的MD5字元串:
//
free(output);
return0;
}
⑥ 求生成MD5碼的c或c++代碼
自己用C語言寫的簡單的MD5演算法實現。
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
typedef unsigned char BYTE;
typedef unsigned int UINT;
typedef UINT MD5_SUB_ARRAY[16];
typedef UINT MD5_TRANSORM_FUNC(UINT,UINT,UINT);
typedef struct
{
UINT a;
UINT b;
UINT c;
UINT d;
MD5_SUB_ARRAY sub_array;
}MD5_TRANSFORM_PARAM;
const double MAX_INT = (double)0xFFFFFFFF + 1.0;
const UINT MD5_TRANSFORM_MATRIX[4][16][3] =
{
{
{ 0, 7, 1}, { 1,12, 2}, { 2,17, 3}, { 3,22, 4},
{ 4, 7, 5}, { 5,12, 6}, { 6,17, 7}, { 7,22, 8},
{ 8, 7, 9}, { 9,12,10}, {10,17,11}, {11,22,12},
{12, 7,13}, {13,12,14}, {14,17,15}, {15,22,16},
},
{
{ 1, 5,17}, { 6, 9,18}, {11,14,19}, { 0,20,20},
{ 5, 5,21}, {10, 9,22}, {15,14,23}, { 4,20,24},
{ 9, 5,25}, {14, 9,26}, { 3,14,27}, { 8,20,28},
{13, 5,29}, { 2, 9,30}, { 7,14,31}, {12,20,32},
},
{
{5, 4, 33}, { 8,11,34}, {11,16,35},{14, 23,36},
{1, 4, 37}, { 4,11,38}, { 7,16,39},{10, 23,40},
{13,4, 41}, { 0,11,42}, { 3,16,43},{ 6, 23,44},
{9, 4, 45}, {12,11,46}, {15,16,47},{ 2, 23,48},
},
{
{ 0,6,49}, { 7,10,50}, {14,15,51},{ 5, 21,52},
{12,6,53}, { 3,10,54}, {10,15,55},{ 1, 21,56},
{ 8,6,57}, {15,10,58}, { 6,15,59},{13, 21,60},
{ 4,6,61}, {11,10,62}, { 2,15,63},{ 9, 21,64},
},
};
static UINT MD5_TRANSFORM_ARRAY[65];
void MD5_Init()
{
int x;
for(x = 1; x <= 64; x++)
{
MD5_TRANSFORM_ARRAY[x] = (UINT)(MAX_INT * fabs(sin(x)));
}
}
UINT F(UINT x,UINT y,UINT z)
{
return ((x & y) | ((~x) & z));
}
UINT G(UINT x,UINT y,UINT z)
{
return ((x & z) | (y & (~z)));
}
UINT H(UINT x,UINT y,UINT z)
{
return (x ^ y ^ z);
}
UINT I(UINT x,UINT y,UINT z)
{
return (y ^ (x | (~z)));
}
BYTE* MD5_prepare_data(const BYTE* data,int len,int* new_len)
{
int rest,fill,size;
BYTE* new_data;
UINT bit_len;
// (1) 位元組補齊
rest = len % 56;
if (rest <= 56) fill = 56 - rest;
else fill = (64 - rest) + 56;
new_data = (BYTE*)malloc(len + fill + 8);
if (NULL == new_data) return NULL;
if (len > 0) memcpy(new_data,data,len);
if (fill > 0) memset(new_data + len,0x80,1);
if (fill > 1) memset(new_data + len + 1,0,fill - 1);
size = fill + len;
// (2) 附加數據的比特長度
bit_len = len * 8;
// (64位二進制數表示的)比特長度的低32位
memset(new_data + size + 0,(bit_len & 0x000000FF), 1);
memset(new_data + size + 1,(bit_len & 0x0000FF00) >> 8, 1);
memset(new_data + size + 2,(bit_len & 0x00FF0000) >> 16,1);
memset(new_data + size + 3,(bit_len & 0xFF000000) >> 24,1);
// 不考慮比特長度超出32位無符號數表示範圍,所以高32位總是0
memset(new_data + size + 4,0,4);
*new_len = size + 8;
return new_data;
}
void MD5_transform(MD5_TRANSFORM_PARAM* param,int ring,MD5_TRANSORM_FUNC func)
{
UINT a,b,c,d,s,k,i;
UINT abcd[4];
UINT *X,*T;
int index;
abcd[0] = param->a;
abcd[1] = param->b;
abcd[2] = param->c;
abcd[3] = param->d;
X = param->sub_array;
T = MD5_TRANSFORM_ARRAY;
for(index = 0; index < 16; index++)
{
a = abcd[(3 * index + 0) % 4];
b = abcd[(3 * index + 1) % 4];
c = abcd[(3 * index + 2) % 4];
d = abcd[(3 * index + 3) % 4];
k = MD5_TRANSFORM_MATRIX[ring][index][0];
s = MD5_TRANSFORM_MATRIX[ring][index][1];
i = MD5_TRANSFORM_MATRIX[ring][index][2];
a = a + func(b,c,d) + X[k] + T[i];
a = ( a << s) | ( a >> (32 - s)); // 循環左移
a = a + b;
abcd[(3 * index + 0) % 4] = a;
}
param->a = abcd[0];
param->b = abcd[1];
param->c = abcd[2];
param->d = abcd[3];
}
int MD5(const BYTE* data,int len)
{
int x,y,new_len;
MD5_TRANSFORM_PARAM param;
UINT AA,BB,CC,DD;
BYTE* buf;
MD5_Init();
buf = MD5_prepare_data(data,len,&new_len);
if (buf == NULL) return -1;
AA = 0x67452301;
BB = 0xefcdab89;
CC = 0x98badcfe;
DD = 0x10325476;
for(x = 0; x < new_len / 64; x++)
{
param.a = AA;
param.b = BB;
param.c = CC;
param.d = DD;
for(y = 0; y < 16; y++)
{
param.sub_array[y] = buf[64 * x + 4 * y + 0];
param.sub_array[y] += buf[64 * x + 4 * y + 1] << 8;
param.sub_array[y] += buf[64 * x + 4 * y + 2] << 16;
param.sub_array[y] += buf[64 * x + 4 * y + 3] << 24;
}
MD5_transform(¶m,0,F);
MD5_transform(¶m,1,G);
MD5_transform(¶m,2,H);
MD5_transform(¶m,3,I);
AA += param.a;
BB += param.b;
CC += param.c;
DD += param.d;
}
printf("MD5(\"%s\")=",data);
printf("%02X%02X%02X%02X",
(AA & 0x000000FF),
(AA & 0x0000FF00) >> 8,
(AA & 0x00FF0000) >> 16,
(AA & 0xFF000000) >> 24);
printf("%02X%02X%02X%02X",
(BB & 0x000000FF),
(BB & 0x0000FF00) >> 8,
(BB & 0x00FF0000) >> 16,
(BB & 0xFF000000) >> 24);
printf("%02X%02X%02X%02X",
(CC & 0x000000FF),
(CC & 0x0000FF00) >> 8,
(CC & 0x00FF0000) >> 16,
(CC & 0xFF000000) >> 24);
printf("%02X%02X%02X%02X",
(DD & 0x000000FF),
(DD & 0x0000FF00) >> 8,
(DD & 0x00FF0000) >> 16,
(DD & 0xFF000000) >> 24);
printf("\n");
return 0;
}
int main()
{
MD5("",0);
MD5("a",1);
MD5("abc",3);
MD5("message digest",14);
MD5("abcdefghijklmnopqrstuvwxyz",26);
return 0;
}
⑦ C語言怎麼實現對一長串字元進行MD5加密
#include<stdio.h>
#include<stdlib.h>
#defineARR_LEN100
#defineF(x,y,z)((x&y)|(~x&z))
#defineG(x,y,z)((x&z)|(y&~z))
#defineH(x,y,z)(x^y^z)
#defineI(x,y,z)(y^(x|~z))
#defineROTATE_LEFT(x,n)((x<<n)|(x>>(32-n)))
#defineFF(a,b,c,d,x,s,ac)
{
a+=F(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineGG(a,b,c,d,x,s,ac)
{
a+=G(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineHH(a,b,c,d,x,s,ac)
{
a+=H(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineII(a,b,c,d,x,s,ac)
{
a+=I(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
typedefstruct{
unsignedintcount[2];
unsignedintstate[4];
unsignedcharbuffer[64];
}MD5_CTX;
voidMD5Init(MD5_CTX*context);
voidMD5Update(MD5_CTX*context,unsignedchar*input,unsignedintinputlen);
voidMD5Final(MD5_CTX*context,unsignedchardigest[16]);
voidMD5Transform(unsignedintstate[4],unsignedcharblock[64]);
voidMD5Encode(unsignedchar*output,unsignedint*input,unsignedintlen);
voidMD5Decode(unsignedint*output,unsignedchar*input,unsignedintlen);
unsignedcharPADDING[]={
0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
voidMD5Init(MD5_CTX*context){
context->count[0]=0;
context->count[1]=0;
context->state[0]=0x67452301;
context->state[1]=0xEFCDAB89;
context->state[2]=0x98BADCFE;
context->state[3]=0x10325476;
}
voidMD5Update(MD5_CTX*context,unsignedchar*input,unsignedintinputlen){
unsignedinti=0,index=0,partlen=0;
index=(context->count[0]>>3)&0x3F;
partlen=64-index;
context->count[0]+=inputlen<<3;
if(context->count[0]<(inputlen<<3))
context->count[1]++;
context->count[1]+=inputlen>>29;
if(inputlen>=partlen){
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i=partlen;i+64<=inputlen;i+=64)
MD5Transform(context->state,&input[i]);
index=0;
}
else{
i=0;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}
voidMD5Final(MD5_CTX*context,unsignedchardigest[16]){
unsignedintindex=0,padlen=0;
unsignedcharbits[8];
index=(context->count[0]>>3)&0x3F;
padlen=(index<56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);
}
voidMD5Encode(unsignedchar*output,unsignedint*input,unsignedintlen){
unsignedinti=0,j=0;
while(j<len){
output[j]=input[i]&0xFF;
output[j+1]=(input[i]>>8)&0xFF;
output[j+2]=(input[i]>>16)&0xFF;
output[j+3]=(input[i]>>24)&0xFF;
i++;
j+=4;
}
}
voidMD5Decode(unsignedint*output,unsignedchar*input,unsignedintlen){
unsignedinti=0,j=0;
while(j<len){
output[i]=(input[j])|
(input[j+1]<<8)|
(input[j+2]<<16)|
(input[j+3]<<24);
i++;
j+=4;
}
}
voidMD5Transform(unsignedintstate[4],unsignedcharblock[64]){
unsignedinta=state[0];
unsignedintb=state[1];
unsignedintc=state[2];
unsignedintd=state[3];
unsignedintx[64];
MD5Decode(x,block,64);
FF(a,b,c,d,x[0],7,0xd76aa478);/*1*/
FF(d,a,b,c,x[1],12,0xe8c7b756);/*2*/
FF(c,d,a,b,x[2],17,0x242070db);/*3*/
FF(b,c,d,a,x[3],22,0xc1bdceee);/*4*/
FF(a,b,c,d,x[4],7,0xf57c0faf);/*5*/
FF(d,a,b,c,x[5],12,0x4787c62a);/*6*/
FF(c,d,a,b,x[6],17,0xa8304613);/*7*/
FF(b,c,d,a,x[7],22,0xfd469501);/*8*/
FF(a,b,c,d,x[8],7,0x698098d8);/*9*/
FF(d,a,b,c,x[9],12,0x8b44f7af);/*10*/
FF(c,d,a,b,x[10],17,0xffff5bb1);/*11*/
FF(b,c,d,a,x[11],22,0x895cd7be);/*12*/
FF(a,b,c,d,x[12],7,0x6b901122);/*13*/
FF(d,a,b,c,x[13],12,0xfd987193);/*14*/
FF(c,d,a,b,x[14],17,0xa679438e);/*15*/
FF(b,c,d,a,x[15],22,0x49b40821);/*16*/
/*Round2*/
GG(a,b,c,d,x[1],5,0xf61e2562);/*17*/
GG(d,a,b,c,x[6],9,0xc040b340);/*18*/
GG(c,d,a,b,x[11],14,0x265e5a51);/*19*/
GG(b,c,d,a,x[0],20,0xe9b6c7aa);/*20*/
GG(a,b,c,d,x[5],5,0xd62f105d);/*21*/
GG(d,a,b,c,x[10],9,0x2441453);/*22*/
GG(c,d,a,b,x[15],14,0xd8a1e681);/*23*/
GG(b,c,d,a,x[4],20,0xe7d3fbc8);/*24*/
GG(a,b,c,d,x[9],5,0x21e1cde6);/*25*/
GG(d,a,b,c,x[14],9,0xc33707d6);/*26*/
GG(c,d,a,b,x[3],14,0xf4d50d87);/*27*/
GG(b,c,d,a,x[8],20,0x455a14ed);/*28*/
GG(a,b,c,d,x[13],5,0xa9e3e905);/*29*/
GG(d,a,b,c,x[2],9,0xfcefa3f8);/*30*/
GG(c,d,a,b,x[7],14,0x676f02d9);/*31*/
GG(b,c,d,a,x[12],20,0x8d2a4c8a);/*32*/
/*Round3*/
HH(a,b,c,d,x[5],4,0xfffa3942);/*33*/
HH(d,a,b,c,x[8],11,0x8771f681);/*34*/
HH(c,d,a,b,x[11],16,0x6d9d6122);/*35*/
HH(b,c,d,a,x[14],23,0xfde5380c);/*36*/
HH(a,b,c,d,x[1],4,0xa4beea44);/*37*/
HH(d,a,b,c,x[4],11,0x4bdecfa9);/*38*/
HH(c,d,a,b,x[7],16,0xf6bb4b60);/*39*/
HH(b,c,d,a,x[10],23,0xbebfbc70);/*40*/
HH(a,b,c,d,x[13],4,0x289b7ec6);/*41*/
HH(d,a,b,c,x[0],11,0xeaa127fa);/*42*/
HH(c,d,a,b,x[3],16,0xd4ef3085);/*43*/
HH(b,c,d,a,x[6],23,0x4881d05);/*44*/
HH(a,b,c,d,x[9],4,0xd9d4d039);/*45*/
HH(d,a,b,c,x[12],11,0xe6db99e5);/*46*/
HH(c,d,a,b,x[15],16,0x1fa27cf8);/*47*/
HH(b,c,d,a,x[2],23,0xc4ac5665);/*48*/
/*Round4*/
II(a,b,c,d,x[0],6,0xf4292244);/*49*/
II(d,a,b,c,x[7],10,0x432aff97);/*50*/
II(c,d,a,b,x[14],15,0xab9423a7);/*51*/
II(b,c,d,a,x[5],21,0xfc93a039);/*52*/
II(a,b,c,d,x[12],6,0x655b59c3);/*53*/
II(d,a,b,c,x[3],10,0x8f0ccc92);/*54*/
II(c,d,a,b,x[10],15,0xffeff47d);/*55*/
II(b,c,d,a,x[1],21,0x85845dd1);/*56*/
II(a,b,c,d,x[8],6,0x6fa87e4f);/*57*/
II(d,a,b,c,x[15],10,0xfe2ce6e0);/*58*/
II(c,d,a,b,x[6],15,0xa3014314);/*59*/
II(b,c,d,a,x[13],21,0x4e0811a1);/*60*/
II(a,b,c,d,x[4],6,0xf7537e82);/*61*/
II(d,a,b,c,x[11],10,0xbd3af235);/*62*/
II(c,d,a,b,x[2],15,0x2ad7d2bb);/*63*/
II(b,c,d,a,x[9],21,0xeb86d391);/*64*/
state[0]+=a;
state[1]+=b;
state[2]+=c;
state[3]+=d;
}
intmain(intargc,char*argv[]){
inti;
unsignedcharencrypt[ARR_LEN];
unsignedchardecrypt[16];
MD5_CTXmd5;
printf("【測試1】 ");
printf("加密前:Bai ");
printf("加密後: ");
printf(" ");
printf("【測試2】 ");
printf("加密前:Bihaifeng ");
printf("加密後: ");
printf(" ");
printf("====================================================== ");
printf("請輸入需MD5加密的字元串: ");
gets(encrypt);
printf(" ");
printf("====================================================== ");
MD5Init(&md5);
MD5Update(&md5,encrypt,strlen((char*)encrypt));
MD5Final(&md5,decrypt);
printf("加密前:%s 加密後:",encrypt);
for(i=0;i<16;i++){
printf("%02x",decrypt[i]);
}
printf(" ");
getch();
return0;
}
運行結果
以上源代碼非原創~~
⑧ md5是什麼 如何計算MD5
MD5即Message-Digest Algorithm 5(信息-摘要演算法5),用於確保信息傳輸完整一致。是計算機廣泛使用的雜湊演算法之一(又譯摘要演算法、哈希演算法),主流編程語言普遍已有MD5實現。
MD5演算法具有以下特點:
1、壓縮性:任意長度的數據,算出的MD5值長度都是固定的。
2、容易計算:從原數據計算出MD5值很容易。
3、抗修改性:對原數據進行任何改動,哪怕只修改1個位元組,所得到的MD5值都有很大區別。
4、強抗碰撞:已知原數據和其MD5值,想找到一個具有相同MD5值的數據(即偽造數據)是非常困難的。
MD5的作用是讓大容量信息在用數字簽名軟體簽署私人密鑰前被"壓縮"成一種保密的格式(就是把一個任意長度的位元組串變換成一定長的16進制數字串)。
大家都知道,地球上任何人都有自己獨一無二的指紋,這常常成為司法機關鑒別罪犯身份最值得信賴的方法;與之類似,MD5就可以為任何文件(不管其大小、格式、數量)產生一個同樣獨一無二的MD5「數字指紋」,如果任何人對文件做了任何改動,其MD5也就是對應的「數字指紋」都會發生變化。