Cryptographic API

本页使用了标题或全文手工转换
维基百科,自由的百科全书

Cryptographic API (CryptoAPI) 是微软在 Windows 作业系统中添加的密码编译机能,作为资料加密与解密功能的重要基础,CryptoAPI 支援同步,非同步的金钥加密处理,以及作业系统中的数位凭证 的管理工作。从Windows NT 4.0引入此功能,并在以后版本的操作系统中不断增强。

目前的 CryptoAPI 支援下列工作[1]

  • 基础密码学函数。
    • 内文函数 (Context function)。
    • 金钥产生函数 (Key generation function)。
    • 金钥交换函数 (Key Exchange function)。
  • 凭证编码与解码函数(支援杂凑功能)。
  • 凭证储存函数。
  • 简单讯息函数。
    • 加密与解密讯息与资料。
    • 对讯息与资料进行签章。
    • 对收到的讯息与相关资料进行数位签章验证的检查。
  • 低阶讯息函数。

由于 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;
}

参考资料[编辑]

  1. ^ CryptoAPI System Architecture. [2008-09-01]. (原始内容存档于2008-11-08). 
  2. ^ Cryptography, CryptoAPI, and CAPICOM. [2008-09-01]. (原始内容存档于2008-10-26).