user-0 提问: c实现md5加密 如何调用这段函数

回复:
user: user-1
content: #pragma comment(lib,"advapi32") #include <windows.h> #include <wincrypt.h> #include <stdio.h> #include <string.h> int main() {     int L;     HCRYPTPROV hProv = 0;     HCRYPTHASH hHash = 0;     BYTE *pbHash = NULL;     DWORD dwHashLen;     #define BUFFER_SIZE 256     BYTE pbBuffer[BUFFER_SIZE];     DWORD dwCount;     DWORD i;  //     Get a handle to the default provider.     if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {         printf("Error %x during CryptAcquireContext!\n", GetLastError());         goto done;     } //     Create a hash object.     if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {         printf("Error %x during CryptBeginHash!\n", GetLastError());         goto done;     }  //     Fill the buffer with test data.     printf("Input a string:");fflush(stdout);     fgets((char *)pbBuffer,BUFFER_SIZE,stdin);     L=strlen((char *)pbBuffer);     if ('\n'==pbBuffer[L-1]) {pbBuffer[L-1]=0;L--;}  //     Put the hash in buffer.     if(!CryptHashData(hHash, pbBuffer, L, 0)) {         printf("Error %x during CryptHashData!\n", GetLastError());         goto done;     }  //     Read the hash value size and allocate memory.     dwCount = sizeof(DWORD);     if(!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)&dwHashLen,                           &dwCount, 0)) {         printf("Error %x during reading hash size!\n", GetLastError());         goto done;     }     if((pbHash = (BYTE *)malloc(dwHashLen)) == NULL) {         printf("Out of memory!\n");         goto done;     }  //     Read the hash value.     if(!CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &dwHashLen, 0)) {         printf("Error %x during reading hash value!\n", GetLastError());         goto done;     }  //     Print the hash value.     for(i = 0 ; i < dwHashLen ; i++) {         printf("%2.2x",pbHash[i]);     }     printf("\n");  done:  //     Free memory.     if(pbHash !=NULL) free(pbHash);  //     Destroy the hash object.     if(hHash) CryptDestroyHash(hHash); //     Release the CSP handle.     if(hProv) CryptReleaseContext(hProv,0);      return 0; } //Input a string:abc //900150983cd24fb0d6963f7d28e17f72 //

user: user-2
content: MD5 算法网上多得是，为什么要用这看起来乱糟糟的代码？ 把我自己用的贴出来，再附上一个例子： #include <stdio.h> #include <stdlib.h> #include <string.h>  /*  * MD5  */  #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) \         do { \             a += F(b, c, d) + x + ac; \             a = ROTATE_LEFT(a, s); \             a += b; \         } while (0) #define GG(a, b, c, d, x, s, ac) \         do { \             a += G(b, c, d) + x + ac; \             a = ROTATE_LEFT(a, s); \             a += b; \         } while (0) #define HH(a, b, c, d, x, s, ac) \         do { \             a += H(b, c, d) + x + ac; \             a = ROTATE_LEFT(a, s); \             a += b; \         } while (0) #define II(a, b, c, d, x, s, ac) \         do { \             a += I(b, c, d) + x + ac; \             a = ROTATE_LEFT(a, s); \             a += b; \         } while (0)  static void MD5Encode(unsigned char *output, const unsigned int *input, unsigned int len); static void MD5Decode(unsigned int *output, const unsigned char *input, unsigned int len); static void MD5Transform(unsigned int state[4], const unsigned char block[64]);  static 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 };  typedef struct {     unsigned int count[2];     unsigned int state[4];     unsigned char buffer[64]; } MD5_CTX;  void MD5Init(MD5_CTX *ctx) {     ctx->count[0] = 0;     ctx->count[1] = 0;     ctx->state[0] = 0x67452301;     ctx->state[1] = 0xEFCDAB89;     ctx->state[2] = 0x98BADCFE;     ctx->state[3] = 0x10325476; }  void MD5Update(MD5_CTX *ctx, const unsigned char *input, unsigned int len) {     unsigned int i = 0;     unsigned int index = (ctx->count[0] >> 3) & 0x3F;     unsigned int partlen = 64 - index;      ctx->count[0] += len << 3;     if (ctx->count[0] < (len << 3)) {         ctx->count[1]++;     }      ctx->count[1] += len >> 29;      if (len >= partlen) {         memcpy(&ctx->buffer[index], input, partlen);         MD5Transform(ctx->state, ctx->buffer);         for (i = partlen; i + 64 <= len; i += 64) {             MD5Transform(ctx->state, &input[i]);         }         index = 0;     } else {         i = 0;     }      memcpy(&ctx->buffer[index], &input[i], len - i); }  void MD5Final(MD5_CTX *ctx, unsigned char digest[16]) {     unsigned char bits[8] = { 0 };     unsigned int index = (ctx->count[0] >> 3) & 0x3F;     unsigned int padlen = (index < 56) ? (56 - index) : (120 - index);      MD5Encode(bits, ctx->count, 8);     MD5Update(ctx, PADDING, padlen);     MD5Update(ctx, bits, 8);     MD5Encode(digest, ctx->state, 16); }  static void MD5Encode(unsigned char *output, const unsigned int *input, unsigned int len) {     unsigned int i = 0;     unsigned int 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;     } }  static void MD5Decode(unsigned int *output, const unsigned char *input, unsigned int len) {     unsigned int i = 0;     unsigned int j = 0;     while (j < len) {         output[i] = (input[j]) | (input[j + 1] << 8) | (input[j + 2] << 16) | (input[j + 3] << 24);         i++;         j += 4;     } }  static void MD5Transform(unsigned int state[4], const 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[16] = { 0 };     MD5Decode(x, block, 64);      /* Round 1 */     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,  0x02441453); /* 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, 0x04881d05); /* 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; }  /*  * HexToStr  */  static char HexToChar(char hex) {     return (hex < 10) ? (hex + '0') : (hex - 10 + 'A'); }  char *HexToStr(char *ret, const unsigned char *data, int len) {     char *dest = ret;     if (ret != NULL && data != NULL && len > 0) {         while (len-- > 0) {             unsigned char ch = *data++;             *ret++ = HexToChar(ch >> 4);             *ret++ = HexToChar(ch & 0xF);         }         *ret = 0;     }     return dest; }  /*  * main  */  #define MAX_READ_BYTES 256  int main(int argc, char *argv[]) {     unsigned char md5data[16] = { 0 };     unsigned char readbuf[MAX_READ_BYTES] = { 0 };     char md5str[32+4] = { 0 };     char str[] = "Hello, world!";     char file[] = "C:\\WINDOWS\\Notepad.exe";     FILE *fp = NULL;     MD5_CTX ctx = { 0 };      // 计算字符串MD5     MD5Init(&ctx);     MD5Update(&ctx, (unsigned char *)str, strlen(str));     MD5Final(&ctx, md5data);     HexToStr(md5str, md5data, 16);      printf("MD5 of string %s:\n%s\n\n", str, md5str);      // 计算文件MD5     MD5Init(&ctx);     fp = fopen(file, "rb");     if (fp != NULL) {         int readlen = 0;         do {             readlen = fread(readbuf, sizeof(unsigned char), MAX_READ_BYTES, fp);             MD5Update(&ctx, (unsigned char *)readbuf, readlen);         } while (readlen == MAX_READ_BYTES);         fclose(fp);     }     MD5Final(&ctx, md5data);     HexToStr(md5str, md5data, 16);      printf("MD5 of file %s:\n%s\n\n", file, md5str);      return 0; }

user: user-1
content: VS2010

user: user-0
content: 引用 10 楼 hashcat的回复:[quote=引用 9 楼 zhao4zhong1 的回复:] 将_asm改为__asm再试试。 VC6中编译毫无违和感。 不同编译器对代码的识别有区别？？ 额.....报错 \5\18346.cpp||In function 'void func(long unsigned int&, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, int, int)':| \5\18346.cpp|46|error: expected '(' before '{' token| \5\18346.cpp|47|error: 'mov' was not declared in this scope| \5\18346.cpp|47|error: expected ';' before 'ecx'| ||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|  [/quote] 话说，正打算换个编译器试试，现在用的codeblocks感觉很别扭，vc太新的我怕跑不了老的标准的代码，太老的我怕跑不了新的，有啥推荐的吗....

user: user-0
content: 引用 9 楼 zhao4zhong1 的回复:将_asm改为__asm再试试。 VC6中编译毫无违和感。 不同编译器对代码的识别有区别？？ 额.....报错 \5\18346.cpp||In function 'void func(long unsigned int&, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, int, int)':| \5\18346.cpp|46|error: expected '(' before '{' token| \5\18346.cpp|47|error: 'mov' was not declared in this scope| \5\18346.cpp|47|error: expected ';' before 'ecx'| ||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

user: user-1
content: 将_asm改为__asm再试试。 VC6中编译毫无违和感。

user: user-0
content: 引用 6 楼 zhao4zhong1 的回复:引用只有在C++中有，将源代码后缀由.c改为.cpp  或者用这个我刚改的.c版本：#include <stdio.h> #include <math.h> #include <string.h> #include <memory.h> const unsigned int s[4][4]={{7,12,17,22},{5,9,14,20},{4,11,16,23},{6,10,15,21}}; const unsigned long t[64]={//t[i]=4294967296*fabs(sin(i+1));  0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,  0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821,  0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,  0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,  0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,  0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,  0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,  0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 }; const int serial[64]={  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,  1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12,  5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2,  0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9 }; void func(unsigned long *a,           unsigned long b,           unsigned long c,           unsigned long d,           unsigned long M,           unsigned long t,           int s,           int turn) {     unsigned long temp;     switch(turn) {     case 0:         temp=(b&c)|((~b)&d);         break;     case 1:         temp=(d&b)|((~d)&c);         break;     case 2:         temp=b^c^d;         break;     case 3:         temp=c^(b|(~d));         break;     }     temp+=M+t+*a;     _asm {         mov ecx,s         rol temp,cl     }     *a=b+temp; } void MD512(const unsigned long M[16],unsigned long hash[4]) {     int i,j,index=0;     for (i=0;i<4;i++)         for (j=0;j<4;j++) {             func(&hash[0],hash[1],hash[2],hash[3],M[serial[index]],t[index],s[i][0],i);             index++;             func(&hash[3],hash[0],hash[1],hash[2],M[serial[index]],t[index],s[i][1],i);             index++;             func(&hash[2],hash[3],hash[0],hash[1],M[serial[index]],t[index],s[i][2],i);             index++;             func(&hash[1],hash[2],hash[3],hash[0],M[serial[index]],t[index],s[i][3],i);             index++;         } }  void MD5(char* M,int nLen,unsigned long output[4]) {     int i,j;     unsigned long Hash[4]={0x67452301,0xefcdab89,0x98badcfe,0x10325476};     unsigned long hash[4];      //填充     __int64 BitsLen=nLen*8;     int oldlen=nLen;     while(nLen%64!=56) {         M[nLen++]=0;     }     M[oldlen]=0x80u;     *(__int64*)(M+nLen)=BitsLen;     nLen+=8;      //开始处理分组     for (i=0;i<nLen;i+=64) {         memcpy(hash,Hash,sizeof(long)*4);         MD512((const unsigned long*)&M[i],hash);//处理512bits分组         for (j=0;j<4;j++)             Hash[j]+=hash[j];     }      //处理输出。     for (i=0;i<4;i++)         for (j=3;j>=0;j--) {             *((char*)(output+i)+j)=*((char*)(Hash+i)+3-j);         } } int main() {     int i;     char szMessage[1000]={0};     unsigned long output[4];      printf("input a string:\n>>>");     scanf("%s",szMessage);     MD5(szMessage,strlen(szMessage),output);     for (i=0;i<4;i++)         printf("%08lx",output[i]);     printf("\n");     return 0; }   我把之前那个改成.cpp了，可是还是在46行报错... 这是老版本的报错 \5\18346.cpp||In function 'void func(long unsigned int&, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, int, int)':| \5\18346.cpp|46|error: '_asm' was not declared in this scope| \5\18346.cpp|46|error: expected ';' before '{' token| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|  这是新版本的报错 5\828.c||In function 'func':| 5\828.c|46|error: '_asm' undeclared (first use in this function)| 5\828.c|46|note: each undeclared identifier is reported only once for each function it appears in| 5\828.c|46|error: expected ';' before '{' token| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

user: user-0
content: 引用 2 楼 walkiz 的回复:参考http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 里面的c文件，报错 c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.8.1\..\..\..\libmingw32.a(main.o):main.c:(.text.startup+0xa7)||undefined reference to `WinMain@16'|  然后我加了个 int main(){             } 然后就报错C:\md5.c|94|error: invalid storage class for function 'body'|

user: user-1
content: 引用只有在C++中有，将源代码后缀由.c改为.cpp  或者用这个我刚改的.c版本：#include <stdio.h> #include <math.h> #include <string.h> #include <memory.h> const unsigned int s[4][4]={{7,12,17,22},{5,9,14,20},{4,11,16,23},{6,10,15,21}}; const unsigned long t[64]={//t[i]=4294967296*fabs(sin(i+1));  0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,  0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821,  0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,  0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,  0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,  0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,  0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,  0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 }; const int serial[64]={  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,  1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12,  5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2,  0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9 }; void func(unsigned long *a,           unsigned long b,           unsigned long c,           unsigned long d,           unsigned long M,           unsigned long t,           int s,           int turn) {     unsigned long temp;     switch(turn) {     case 0:         temp=(b&c)|((~b)&d);         break;     case 1:         temp=(d&b)|((~d)&c);         break;     case 2:         temp=b^c^d;         break;     case 3:         temp=c^(b|(~d));         break;     }     temp+=M+t+*a;     _asm {         mov ecx,s         rol temp,cl     }     *a=b+temp; } void MD512(const unsigned long M[16],unsigned long hash[4]) {     int i,j,index=0;     for (i=0;i<4;i++)         for (j=0;j<4;j++) {             func(&hash[0],hash[1],hash[2],hash[3],M[serial[index]],t[index],s[i][0],i);             index++;             func(&hash[3],hash[0],hash[1],hash[2],M[serial[index]],t[index],s[i][1],i);             index++;             func(&hash[2],hash[3],hash[0],hash[1],M[serial[index]],t[index],s[i][2],i);             index++;             func(&hash[1],hash[2],hash[3],hash[0],M[serial[index]],t[index],s[i][3],i);             index++;         } }  void MD5(char* M,int nLen,unsigned long output[4]) {     int i,j;     unsigned long Hash[4]={0x67452301,0xefcdab89,0x98badcfe,0x10325476};     unsigned long hash[4];      //填充     __int64 BitsLen=nLen*8;     int oldlen=nLen;     while(nLen%64!=56) {         M[nLen++]=0;     }     M[oldlen]=0x80u;     *(__int64*)(M+nLen)=BitsLen;     nLen+=8;      //开始处理分组     for (i=0;i<nLen;i+=64) {         memcpy(hash,Hash,sizeof(long)*4);         MD512((const unsigned long*)&M[i],hash);//处理512bits分组         for (j=0;j<4;j++)             Hash[j]+=hash[j];     }      //处理输出。     for (i=0;i<4;i++)         for (j=3;j>=0;j--) {             *((char*)(output+i)+j)=*((char*)(Hash+i)+3-j);         } } int main() {     int i;     char szMessage[1000]={0};     unsigned long output[4];      printf("input a string:\n>>>");     scanf("%s",szMessage);     MD5(szMessage,strlen(szMessage),output);     for (i=0;i<4;i++)         printf("%08lx",output[i]);     printf("\n");     return 0; }

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:仅供参考：#include <stdio.h> #include <math.h> #include <string.h> #include <memory.h> const unsigned int s[4][4]={{7,12,17,22},{5,9,14,20},{4,11,16,23},{6,10,15,21}}; const unsigned long t[64]={//t[i]=4294967296*fabs(sin(i+1));  0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,  0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821,  0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,  0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,  0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,  0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,  0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,  0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 }; const int serial[64]={  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,  1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12,  5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2,  0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9 }; void func(unsigned long& a,           unsigned long b,           unsigned long c,           unsigned long d,           unsigned long M,           unsigned long t,           int s,           int turn) {     unsigned long temp;     switch(turn) {     case 0:         temp=(b&c)|((~b)&d);         break;     case 1:         temp=(d&b)|((~d)&c);         break;     case 2:         temp=b^c^d;         break;     case 3:         temp=c^(b|(~d));         break;     }     temp+=M+t+a;     _asm {         mov ecx,s         rol temp,cl     }     a=b+temp; } void MD512(const unsigned long M[16],unsigned long hash[4]) {     int i,j,index=0;     for (i=0;i<4;i++)         for (j=0;j<4;j++) {             func(hash[0],hash[1],hash[2],hash[3],M[serial[index]],t[index],s[i][0],i);             index++;             func(hash[3],hash[0],hash[1],hash[2],M[serial[index]],t[index],s[i][1],i);             index++;             func(hash[2],hash[3],hash[0],hash[1],M[serial[index]],t[index],s[i][2],i);             index++;             func(hash[1],hash[2],hash[3],hash[0],M[serial[index]],t[index],s[i][3],i);             index++;         } }  void MD5(char* M,int nLen,unsigned long output[4]) {     int i,j;     unsigned long Hash[4]={0x67452301,0xefcdab89,0x98badcfe,0x10325476};     unsigned long hash[4];      //填充     __int64 BitsLen=nLen*8;     int oldlen=nLen;     while(nLen%64!=56) {         M[nLen++]=0;     }     M[oldlen]=0x80u;     *(__int64*)(M+nLen)=BitsLen;     nLen+=8;      //开始处理分组     for (i=0;i<nLen;i+=64) {         memcpy(hash,Hash,sizeof(long)*4);         MD512((const unsigned long*)&M[i],hash);//处理512bits分组         for (j=0;j<4;j++)             Hash[j]+=hash[j];     }      //处理输出。     for (i=0;i<4;i++)         for (j=3;j>=0;j--) {             *((char*)(output+i)+j)=*((char*)(Hash+i)+3-j);         } } int main() {     int i;     char szMessage[1000]={0};     printf("input a string:\n>>>");     scanf("%s",szMessage);     unsigned long output[4];     MD5(szMessage,strlen(szMessage),output);     for (i=0;i<4;i++)         printf("%08lx",output[i]);     printf("\n");     return 0; }  出现F:\18346.c|22|error: expected ';', ',' or ')' before '&' token|咋办....

user: user-0
content: 引用 1 楼 zxhuser-4 的回复:上面不都有注释说明了吗 是啊，可是我根据他的注释，不会写啊，我写的这个有问题，虽然不报错...但是出来的命令行窗口里就一个 Process returned 1 (0x1)   execution time : 0.458 s Press any key to continue. 我是这么写的。  #include <stdio.h> #include <stdlib.h> #define MD5_ROTL(a,b) (MD5_tmp=(a),((MD5_tmp>>(32-b))&(0x7fffffff>>(31-b)))|(MD5_tmp<<b))   //循环左移 #define MD5_F(X,Y,Z) ((X&Y)|((~X)&Z)) #define MD5_G(X,Y,Z) ((X&Z)|(Y&(~Z))) #define MD5_H(X,Y,Z) (X^Y^Z) #define MD5_I(X,Y,Z) (Y^(X|(~Z))) #define MD5_FF(a,b,c,d,Mj,s,ti) a=b+MD5_ROTL((a+MD5_F(b,c,d)+Mj+ti),s) #define MD5_GG(a,b,c,d,Mj,s,ti) a=b+MD5_ROTL((a+MD5_G(b,c,d)+Mj+ti),s) #define MD5_HH(a,b,c,d,Mj,s,ti) a=b+MD5_ROTL((a+MD5_H(b,c,d)+Mj+ti),s) #define MD5_II(a,b,c,d,Mj,s,ti) a=b+MD5_ROTL((a+MD5_I(b,c,d)+Mj+ti),s) #define MD5main \ {MD5_FF(a, b, c, d, M[0], 7, 0xd76aa478);\ MD5_FF(d, a, b, c, M[1], 12, 0xe8c7b756);\ MD5_FF(c, d, a, b, M[2], 17, 0x242070db);\ MD5_FF(b, c, d, a, M[3], 22, 0xc1bdceee);\ MD5_FF(a, b, c, d, M[4], 7, 0xf57c0faf);\ MD5_FF(d, a, b, c, M[5], 12, 0x4787c62a);\ MD5_FF(c, d, a, b, M[6], 17, 0xa8304613);\ MD5_FF(b, c, d, a, M[7], 22, 0xfd469501);\ MD5_FF(a, b, c, d, M[8], 7, 0x698098d8);\ MD5_FF(d, a, b, c, M[9], 12, 0x8b44f7af);\ MD5_FF(c, d, a, b, M[10], 17, 0xffff5bb1);\ MD5_FF(b, c, d, a, M[11], 22, 0x895cd7be);\ MD5_FF(a, b, c, d, M[12], 7, 0x6b901122);\ MD5_FF(d, a, b, c, M[13], 12, 0xfd987193);\ MD5_FF(c, d, a, b, M[14], 17, 0xa679438e);\ MD5_FF(b, c, d, a, M[15], 22, 0x49b40821);\ MD5_GG(a, b, c, d, M[1], 5, 0xf61e2562);\ MD5_GG(d, a, b, c, M[6], 9, 0xc040b340);\ MD5_GG(c, d, a, b, M[11], 14, 0x265e5a51);\ MD5_GG(b, c, d, a, M[0], 20, 0xe9b6c7aa);\ MD5_GG(a, b, c, d, M[5], 5, 0xd62f105d);\ MD5_GG(d, a, b, c, M[10], 9, 0x02441453);\ MD5_GG(c, d, a, b, M[15], 14, 0xd8a1e681);\ MD5_GG(b, c, d, a, M[4], 20, 0xe7d3fbc8);\ MD5_GG(a, b, c, d, M[9], 5, 0x21e1cde6);\ MD5_GG(d, a, b, c, M[14], 9, 0xc33707d6);\ MD5_GG(c, d, a, b, M[3], 14, 0xf4d50d87);\ MD5_GG(b, c, d, a, M[8], 20, 0x455a14ed);\ MD5_GG(a, b, c, d, M[13], 5, 0xa9e3e905);\ MD5_GG(d, a, b, c, M[2], 9, 0xfcefa3f8);\ MD5_GG(c, d, a, b, M[7], 14, 0x676f02d9);\ MD5_GG(b, c, d, a, M[12], 20, 0x8d2a4c8a);\ MD5_HH(a, b, c, d, M[5], 4, 0xfffa3942);\ MD5_HH(d, a, b, c, M[8], 11, 0x8771f681);\ MD5_HH(c, d, a, b, M[11], 16, 0x6d9d6122);\ MD5_HH(b, c, d, a, M[14], 23, 0xfde5380c);\ MD5_HH(a, b, c, d, M[1], 4, 0xa4beea44);\ MD5_HH(d, a, b, c, M[4], 11, 0x4bdecfa9);\ MD5_HH(c, d, a, b, M[7], 16, 0xf6bb4b60);\ MD5_HH(b, c, d, a, M[10], 23, 0xbebfbc70);\ MD5_HH(a, b, c, d, M[13], 4, 0x289b7ec6);\ MD5_HH(d, a, b, c, M[0], 11, 0xeaa127fa);\ MD5_HH(c, d, a, b, M[3], 16, 0xd4ef3085);\ MD5_HH(b, c, d, a, M[6], 23, 0x04881d05);\ MD5_HH(a, b, c, d, M[9], 4, 0xd9d4d039);\ MD5_HH(d, a, b, c, M[12], 11, 0xe6db99e5);\ MD5_HH(c, d, a, b, M[15], 16, 0x1fa27cf8);\ MD5_HH(b, c, d, a, M[2], 23, 0xc4ac5665);\ MD5_II(a, b, c, d, M[0], 6, 0xf4292244);\ MD5_II(d, a, b, c, M[7], 10, 0x432aff97);\ MD5_II(c, d, a, b, M[14], 15, 0xab9423a7);\ MD5_II(b, c, d, a, M[5], 21, 0xfc93a039);\ MD5_II(a, b, c, d, M[12], 6, 0x655b59c3);\ MD5_II(d, a, b, c, M[3], 10, 0x8f0ccc92);\ MD5_II(c, d, a, b, M[10], 15, 0xffeff47d);\ MD5_II(b, c, d, a, M[1], 21, 0x85845dd1);\ MD5_II(a, b, c, d, M[8], 6, 0x6fa87e4f);\ MD5_II(d, a, b, c, M[15], 10, 0xfe2ce6e0);\ MD5_II(c, d, a, b, M[6], 15, 0xa3014314);\ MD5_II(b, c, d, a, M[13], 21, 0x4e0811a1);\ MD5_II(a, b, c, d, M[4], 6, 0xf7537e82);\ MD5_II(d, a, b, c, M[11], 10, 0xbd3af235);\ MD5_II(c, d, a, b, M[2], 15, 0x2ad7d2bb);\ MD5_II(b, c, d, a, M[9], 21, 0xeb86d391);\ A = a += A, B = b += B, C = c += C, D = d += D;}  int main(){  long MD5_tmp;   char* StrMD5(const char* str, long length, char* md5){     /*     计算字符串MD5     参数说明：         str         字符串指针         length      字符串长度         md5         用于保存MD5的字符串指针     返回值为参数md5     */     char *pp; printf("\n请你输入一个字符: ");  scanf("%c",&str);  printf("你输入的字符是： %c\n",str);      long l, i, *M, abcdtmp[4], A, B, C, D, a, b, c, d;     a = A = 0x67452301, b = B = 0xEFCDAB89, c = C = 0x98BADCFE, d = D = 0x10325476;     l = length + ((length % 64 > 56) ? (128 - length % 64) : (64 - length % 64));     if(!(pp = (char*)malloc((unsigned long)l))) return 0;     for (i = 0; i < length; pp[i]=str[i],i++);     for (pp[i++] = 128; i < l; pp[i++] = 0);     *((long*)(pp + l - 8)) = length << 3;     for (i = 0; i < l; i += 64){         M = (long*)pp;         MD5main     }     free(pp);     abcdtmp[0] = a, abcdtmp[1] = b, abcdtmp[2] = c, abcdtmp[3] = d;     for (pp = (char*)abcdtmp, i = 0; i < 16; sprintf((md5 + 2 * i), "%02X", (unsigned char)pp[i]), i++);     return md5;   }   char* FileMD5(const char* file, char* md5){     /*     计算文件MD5     参数说明：     file        文件路径字符串指针     md5         用于保存MD5的字符串指针     返回值为参数md5     */      FILE* fh;     char* addlp;     long* temp;     long addlsize, j, M[16], abcdtmp[4], A, B, C, D, a, b, c, d;     long long length, i, cpys;     void *pp, *ppend;      fh = fopen(file, "rb");     fseek(fh, 0, SEEK_END);     length = _ftelli64(fh);     a = A = 0x67452301, b = B = 0xEFCDAB89, c = C = 0x98BADCFE, d = D = 0x10325476;     addlsize = (56 - length % 64 > 0) ? (64) : (128);     if (!(addlp = (char*)malloc(addlsize))) return 0;     cpys = ((length - (56 - length % 64)) > 0) ? (length - length % 64) : (0);     j = (long)(length - cpys);     fseek(fh, -j, SEEK_END);     fread(addlp, 1, j, fh);     for (addlp[j++] = 128; j < addlsize; addlp[j++] = 0);     pp = addlp + addlsize - 8;     *((long long *)pp) = length << 3;     for (rewind(fh); 16 == fread(M, 4, 16, fh);)         MD5main      for (temp = (long*)addlp, ppend = addlp + addlsize; temp < ppend; temp += 16) {         for (i = 0; i < 16; M[i] = ((long*)temp)[i], i++);         MD5main     }     free(addlp);fclose(fh);     abcdtmp[0] = a, abcdtmp[1] = b, abcdtmp[2] = c, abcdtmp[3] = d;     for (addlp = (char*)abcdtmp, i = 0; i < 16; sprintf((md5 + 2 * i), "%02X", (unsigned char)addlp[i]), i++);     printf(md5);     return md5;  }  }

user: user-1
content: 仅供参考：#include <stdio.h> #include <math.h> #include <string.h> #include <memory.h> const unsigned int s[4][4]={{7,12,17,22},{5,9,14,20},{4,11,16,23},{6,10,15,21}}; const unsigned long t[64]={//t[i]=4294967296*fabs(sin(i+1));  0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,  0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821,  0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,  0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,  0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,  0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,  0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,  0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 }; const int serial[64]={  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,  1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12,  5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2,  0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9 }; void func(unsigned long& a,           unsigned long b,           unsigned long c,           unsigned long d,           unsigned long M,           unsigned long t,           int s,           int turn) {     unsigned long temp;     switch(turn) {     case 0:         temp=(b&c)|((~b)&d);         break;     case 1:         temp=(d&b)|((~d)&c);         break;     case 2:         temp=b^c^d;         break;     case 3:         temp=c^(b|(~d));         break;     }     temp+=M+t+a;     _asm {         mov ecx,s         rol temp,cl     }     a=b+temp; } void MD512(const unsigned long M[16],unsigned long hash[4]) {     int i,j,index=0;     for (i=0;i<4;i++)         for (j=0;j<4;j++) {             func(hash[0],hash[1],hash[2],hash[3],M[serial[index]],t[index],s[i][0],i);             index++;             func(hash[3],hash[0],hash[1],hash[2],M[serial[index]],t[index],s[i][1],i);             index++;             func(hash[2],hash[3],hash[0],hash[1],M[serial[index]],t[index],s[i][2],i);             index++;             func(hash[1],hash[2],hash[3],hash[0],M[serial[index]],t[index],s[i][3],i);             index++;         } }  void MD5(char* M,int nLen,unsigned long output[4]) {     int i,j;     unsigned long Hash[4]={0x67452301,0xefcdab89,0x98badcfe,0x10325476};     unsigned long hash[4];      //填充     __int64 BitsLen=nLen*8;     int oldlen=nLen;     while(nLen%64!=56) {         M[nLen++]=0;     }     M[oldlen]=0x80u;     *(__int64*)(M+nLen)=BitsLen;     nLen+=8;      //开始处理分组     for (i=0;i<nLen;i+=64) {         memcpy(hash,Hash,sizeof(long)*4);         MD512((const unsigned long*)&M[i],hash);//处理512bits分组         for (j=0;j<4;j++)             Hash[j]+=hash[j];     }      //处理输出。     for (i=0;i<4;i++)         for (j=3;j>=0;j--) {             *((char*)(output+i)+j)=*((char*)(Hash+i)+3-j);         } } int main() {     int i;     char szMessage[1000]={0};     printf("input a string:\n>>>");     scanf("%s",szMessage);     unsigned long output[4];     MD5(szMessage,strlen(szMessage),output);     for (i=0;i<4;i++)         printf("%08lx",output[i]);     printf("\n");     return 0; }

user: user-3
content: 参考http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5

user: user-4
content: 上面不都有注释说明了吗