#include <wincrypt.h> //需要引入的头文件 CString MD5(CONST CString 字符串) { CString MD5str(_T("")); CStringA tmp; tmp = 字符串; char * pp = tmp.GetBuffer(); HCRYPTPROV hProv; if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {return MD5str;} HCRYPTHASH hHash; if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) { CryptReleaseContext(hProv, 0); return MD5str; } if (!CryptHashData(hHash, (BYTE *)pp, strlen(pp), 0)) { CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return MD5str; } DWORD dwSize; DWORD dwLen = sizeof(dwSize); CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)(&dwSize), &dwLen, 0); BYTE* pHash = new BYTE[dwSize]; dwLen = dwSize; CryptGetHashParam(hHash, HP_HASHVAL, pHash, &dwLen, 0); for (DWORD i = 0; i<dwLen; i++) MD5str.AppendFormat(_T("%02X"), pHash[i]); delete[] pHash; CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); MD5str.MakeLower(); return MD5str; } //压缩版 #include <wincrypt.h> //需要引入的头文件 CString MD5(CONST CString 字符串) { CString MD5str(_T("")); CStringA tmp; tmp = 字符串; char * pp = tmp.GetBuffer(); HCRYPTPROV hProv; HCRYPTHASH hHash; if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { return MD5str; } if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) { CryptReleaseContext(hProv, 0); return MD5str; } if (!CryptHashData(hHash, (BYTE *)pp, strlen(pp), 0)) { CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return MD5str; } DWORD dwSize; DWORD dwLen = sizeof(dwSize); CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)(&dwSize), &dwLen, 0); BYTE* pHash = new BYTE[dwSize]; dwLen = dwSize; CryptGetHashParam(hHash, HP_HASHVAL, pHash, &dwLen, 0); for (DWORD i = 0; i<dwLen; i++){ MD5str.AppendFormat(_T("%02X"), pHash[i]); } delete[] pHash; CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); MD5str.MakeLower(); return MD5str; }