Cryptographic API

例子

#include <wincrypt.h>#include <wintrust.h>#pragma comment(lib, "crypt32.lib")#include <atlstr.h>bool GetHash(int hash_type, CString& hash_result, CString& hash_message){

HCRYPTPROV hCryptProv;
HCRYPTHASH hCryptHash;

/*Note that you will get the error such as ‘Invalid Algorithm Specified’ (Error Code: 0x80090008) when you try to replace the algorithm with CALG_SHA256, CALG_SHA384  or CALG_SHA512. Because these algorithms are not supported by Microsoft Base Cryptography Provider ( PROV_RSA_FULL ).   To fix this problem you need to use the provider as PROV_RSA_AES (Microsoft Enhanced RSA and AES Cryptographic Provider) in the  CryptAcquireContext function instead of PROV_RSA_FULL.*/
if (!CryptAcquireContext(&hCryptProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0))
                    //&hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
unsigned int e = GetLastError();
CString str;
str.Format("创建CSP容器出错！错误代码为:%x！", e);
MessageBox(NULL, str, "出错啦!", MB_OK | MB_ICONERROR);
return false;
}

if (!CryptCreateHash(hCryptProv, hash_type, 0, 0, &hCryptHash))
{
DWORD e = GetLastError();
CString str;
str.Format("创建哈希句柄出错！错误代码为:%x！", e);
MessageBox(NULL, str, "出错啦!", MB_OK | MB_ICONERROR);
return false;
}

if (!CryptHashData(hCryptHash, (BYTE*)hash_message.GetBuffer(), hash_message.GetLength(), 0))
{
int e = GetLastError();
CString str;
str.Format("计算哈希值出错！错误代码为:%d！", e);
MessageBox(NULL, str, "出错啦!", MB_OK | MB_ICONERROR);
return false;
}

char hash_data[512];
DWORD hash_len = 512;
if (!CryptGetHashParam(hCryptHash, HP_HASHVAL, (BYTE*)hash_data, &hash_len, 0))
{
int e = GetLastError();
CString str;
str.Format("获取哈希值出错！错误代码为:%d！", e);
MessageBox(NULL, str, "出错啦!", MB_OK | MB_ICONERROR);
return false;
}

char hash_hex[512];
for (unsigned int i = 0; i <= hash_len - 1; i++)
{
int hash_bit = hash_data[i];
int first = (hash_bit & 0xf0) >> 4;
int second = hash_bit & 0x0f;
char tmp[2];
_itoa(first, tmp, 16);
hash_hex[i * 2] = tmp[0];
_itoa(second, tmp, 16);
hash_hex[i * 2 + 1] = tmp[0];
}

hash_hex[hash_len * 2] = '\0';
hash_result.Format("%s", hash_hex);
CryptDestroyHash(hCryptHash);
CryptReleaseContext(hCryptProv, NULL);
return true;}