取MD5
#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; }
取随机数:
srand(::GetTickCount()); //置随机数种子 在窗口创建后 执行一次即可 CString 取随机数(int 最小值, int 最大值){ CString str; int a = rand() % (最大值-最小值+1); str.Format(_T("%d"), a+最小值); return str; }