#include ".\opencsp_m$csp.h"
OPENCSP_M$CSP* OPENCSP_M$CSP::_instance;
OPENCSP_M$CSP::OPENCSP_M$CSP(void)
{
BOOL fOK = FALSE;
fOK = CryptAcquireContext(&_hProv, TEXT("Keyset in MSCSP used by OpenCSP"),
MS_ENHANCED_PROV, PROV_RSA_FULL, 0);//获取有某个容器的CSP模块的指针
if (!fOK)
{
if (GetLastError() == NTE_BAD_KEYSET)
{
CryptAcquireContext(&_hProv, TEXT("Keyset in MSCSP used by OpenCSP"),
MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET);
HCRYPTKEY hKey = 0;
CryptGenKey(_hProv, AT_KEYEXCHANGE, RSA1024BIT_KEY | CRYPT_EXPORTABLE, &hKey);
}
}
}
OPENCSP_M$CSP::~OPENCSP_M$CSP(void)
{
CryptReleaseContext(_hProv, 0);
}
OPENCSP_M$CSP* OPENCSP_M$CSP::createM$Instance()
{
if (!_instance)
_instance = new OPENCSP_M$CSP;
return _instance;
}
DWORD OPENCSP_M$CSP::genRandom(DWORD dwLen, LPBYTE pbBuffer)
{
CryptGenRandom(_hProv, dwLen, pbBuffer);// 根据指定长度产生随机数,pbBuffer随机数的缓冲区
return GetLastError();
}
DWORD OPENCSP_M$CSP::createHash( ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, HCRYPTHASH* phHash )
{
CryptCreateHash(_hProv, Algid, hKey, dwFlags, phHash);//创建一个hash对象
return GetLastError();
}
DWORD OPENCSP_M$CSP::destroyHash( HCRYPTHASH hHash )
{
CryptDestroyHash(hHash);
return GetLastError();
}
DWORD OPENCSP_M$CSP::hashData( HCRYPTHASH hHash, const BYTE* pbData, DWORD dwDataLen, DWORD dwFlags )
{
//用hash对象把data计算,得到的hash值保存在hash对象里面hHash
CryptHashData(hHash, pbData, dwDataLen, dwFlags);
return GetLastError();
}
DWORD OPENCSP_M$CSP::setHashParam( HCRYPTHASH hHash, DWORD dwParam, const BYTE* pbData, DWORD dwFlags )
{
CryptSetHashParam(hHash, dwParam, pbData, dwFlags);
return GetLastError();
}
DWORD OPENCSP_M$CSP::getHashParam( HCRYPTHASH hHash, DWORD dwParam, BYTE* pbData,
DWORD* pdwDataLen, DWORD dwFlags )
{
CryptGetHashParam(hHash, dwParam, pbData, pdwDataLen, dwFlags);
return GetLastError();
}
DWORD OPENCSP_M$CSP::genKey(ALG_ID algId, DWORD dwFlags, HCRYPTKEY* hKey)
{
CryptGenKey(_hProv, algId, dwFlags, hKey);
return GetLastError();
}
DWORD OPENCSP_M$CSP::getKeyParam( HCRYPTKEY hKey, DWORD dwParam, BYTE* pbData,
DWORD* pdwDataLen, DWORD dwFlags )
{
CryptGetKeyParam(hKey, dwParam, pbData, pdwDataLen, dwFlags);//获取密钥参数
return GetLastError();
}
DWORD OPENCSP_M$CSP::setKeyParam( HCRYPTKEY hKey, DWORD dwParam, BYTE* pbData, DWORD dwFlags )
{
CryptSetKeyParam(hKey, dwParam, pbData, dwFlags);//设置密钥参数
return GetLastError();
}
DWORD OPENCSP_M$CSP::getUserKey( DWORD dwKeySpec, HCRYPTKEY* phUserKey )
{
CryptGetUserKey(_hProv, dwKeySpec, phUserKey);//获取密钥容器里面的密钥
return GetLastError();
}
DWORD OPENCSP_M$CSP::importKey( const BYTE* pbData, DWORD dwDataLen, HCRYPTKEY hPubKey,
DWORD dwFlags, HCRYPTKEY* phKey )
{
CryptImportKey(_hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);//导入密钥
return GetLastError();
}
DWORD OPENCSP_M$CSP::exportKey( HCRYPTKEY hKey, HCRYPTKEY hPubKey, DWORD dwBlobType,
DWORD dwFlags, BYTE* pbData, DWORD* pdwDataLen )
{
CryptExportKey(hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);//将公钥导出
return GetLastError();
}
DWORD OPENCSP_M$CSP::destroyKey( HCRYPTKEY hKey )
{
CryptDestroyKey(hKey);
return GetLastError();
}
DWORD OPENCSP_M$CSP::signHash( HCRYPTHASH hHash, DWORD dwKeySpec, LPCTSTR szDescription,
DWORD dwFlags, BYTE* pbSignature, DWORD* pdwSigLen )
{//把hHash里面的hash值进行签名,也就是使用密钥进行加密,签名后的密文就保存在了pbSignature里面
CryptSignHash(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen);
return GetLastError();
}
DWORD OPENCSP_M$CSP::verifySignature( HCRYPTHASH hHash, const BYTE* pbSignature, DWORD dwSigLen,
HCRYPTKEY hPubKey, LPCTSTR szDescription, DWORD dwFlags )
{
CryptVerifySignature(hHash, pbSignature, dwSigLen, hPubKey, szDescription, dwFlags);//验证签名
return GetLastError();
}
DWORD OPENCSP_M$CSP::encrypt( HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags,
BYTE* pbData, DWORD* pdwDataLen, DWORD dwBufLen )
{
CryptEncrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);//加密
return GetLastError();
}
DWORD OPENCSP_M$CSP::decrypt( HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags,
BYTE* pbData, DWORD* pdwDataLen )
{
CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen);//解密
return GetLastError();
}