Cryptographic APICryptographic API (CryptoAPI) 是微軟在 Windows 作業系統中添加的密碼編譯機能,作為資料加密與解密功能的重要基礎,CryptoAPI 支援同步,非同步的金鑰加密處理,以及作業系統中的數位憑證 的管理工作。从Windows NT 4.0引入此功能,并在以后版本的操作系统中不断增强。 目前的 CryptoAPI 支援下列工作[1]:
由於 CryptoAPI 使用上過於複雜,因此微軟另外為 CryptoAPI 開發更為容易使用的 CAPICOM 元件[2],以及 Data Protection API。从Windows Vista开始,推出了新一代密码学API Cryptography API: Next Generation。 例子#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;
}
參考資料
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia