使用windows的动态链接库advapi32.dll.

此动态链接库提供以下md5函数:

MD5Init(&ctx);
MD5Update(&ctx, buf, len);
MD5Final(&ctx);

具体使用如下

新建文件 common.h

#include <windows.h> /* Data structure for MD5 (Message-Digest) computation */ typedef struct {  ULONG i[2];                          /* number of _bits_ handled mod 2^64 */  ULONG buf[4];                                           /* scratch buffer */  unsigned char in[64];                                     /* input buffer */  unsigned char digest[16];            /* actual digest after MD5Final call */ } MD5_CTX;   #define MD5DIGESTLEN 16  #define PROTO_LIST(list)    list   /* * MTS: Each of these assumes MD5_CTX is locked against simultaneous use. */ typedef void (WINAPI* PMD5Init) PROTO_LIST ((MD5_CTX *)); typedef void (WINAPI* PMD5Update) PROTO_LIST ((MD5_CTX *, const unsigned char *, unsigned int)); typedef void (WINAPI* PMD5Final )PROTO_LIST ((MD5_CTX *));  //end globle define        class Ccommon  { public:   Ccommon();  virtual ~Ccommon();  const char * md5(const char * str);  const char * Hex2ASC(const BYTE *Hex, int Len);  PMD5Init MD5Init;  PMD5Update MD5Update;  PMD5Final MD5Final;  };

新建文件 common.cpp

#include "stdafx.h" #include "common.h"  ////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////  Ccommon::Ccommon() {  }  Ccommon::~Ccommon() {  }  const char * Ccommon::Hex2ASC(const BYTE *Hex, int Len) { 	static char  ASC[4096 * 2]; 	int    i;  	for (i = 0; i < Len; i++) 	{ 		ASC[i * 2] = "0123456789abcdef"[Hex[i] >> 4]; 		ASC[i * 2 + 1] = "0123456789abcdef"[Hex[i] & 0x0F]; 	} 	ASC[i * 2] = 0; 	return ASC; }   const char * Ccommon::md5(const char * str) { 	MD5_CTX ctx; 	const unsigned char * buf = reinterpret_cast<const unsigned char *>(str); 	int len = strlen(str); 	HINSTANCE hDLL; 	if ( (hDLL = LoadLibraryA("advapi32.dll")) > 0 ) 	{  		MD5Init = (PMD5Init)GetProcAddress(hDLL,"MD5Init"); 		MD5Update = (PMD5Update)GetProcAddress(hDLL,"MD5Update"); 		MD5Final = (PMD5Final)GetProcAddress(hDLL,"MD5Final");  		MD5Init(&ctx); 		MD5Update(&ctx, buf, len); 		MD5Final(&ctx); 	} 	return Hex2ASC(ctx.digest, 16); } 

调用方法

//引入头文件  #include "common.h" //声明类 Ccommon md5Class; name=md5Class.md5(name);  

注意事项

MD5加密后的大小写是由 Hex2ASC函数里

ASC[i * 2] = "0123456789abcdef"[Hex[i] >> 4]; ASC[i * 2 + 1] = "0123456789abcdef"[Hex[i] & 0x0F];
这两句代码控制的 这里我改为了小写 默认应该是大写 看大家需求而定

如果需要大写改为:

ASC[i * 2] = "0123456789ABCDEF"[Hex[i] >> 4]; ASC[i * 2 + 1] = "0123456789ABCDEF"[Hex[i] & 0x0F];