问题描述
我正在使用PUBLICKEYSTRUC和CryptImportKey导入用户已知的密钥。 有时它会起作用,有时却不起作用。 它似乎与密钥加载的内存位置有关。 这是一个纯文本密钥,不是
编码的(这不是在证书中编码的公钥)。 我的代码如下:
//示例键16字节末尾为空
char szKey [17] = {0x0C,0x42,0x11,0x87,0x46,0x19,0x83,0x77,0x57,0x98,0x12,0x68,0x21,0x64,0x34,0x71,0x00};   
// sample IV省略,因为格式相同。
PUBLICKEYSTRUC * pMyKeyBlob;
CONST BYTE szDesKeyBlob [1024];
DWORD dwAlgoId;
BYTE szBinDataSize [] = {0x10,0x00,0x00,0x00};
memset(szDesKeyBlob,0x00,sizeof(szDesKeyBlob));
$
if(!CryptAcquireContext(& hCryptProv," MyData") ,MS_ENH_RSA_AES_PROV,PROV_RSA_AES,0))
{
dwError = GetLastError();
  ; if(dwError = NTE_BAD_KEYSET)
if(!CryptAcquireContext(& hCryptProv," MyData",MS_ENH_RSA_AES_PROV,PROV_RSA_AES,CRYPT_NEWKEYSET))
{
sprintf(szErrorMessage," Error%08X",dwError);
Messag ePopup(" Error",szErrorMessage);
}
}
pMyKeyBlob =(PUBLICKEYSTRUC *)szDesKeyBlob;
pMyKeyBlob-> bType = PLAINTEXTKEYBLOB;
pMyKeyBlob-> ; bVersion = CUR_BLOB_VERSION;
pMyKeyBlob-> reserved = 0;
pMyKeyBlob-> aiKeyAlg = CALG_AES_128;
memcpy(szDesKeyBlob + sizeof(PUBLICKEYSTRUC),szBinDataSize,4); // size
memcpy(szDesKeyBlob + sizeof(PUBLICKEYSTRUC)+ 4,szBinData,16); // key
dwDataLen = sizeof(PUBLICKEYSTRUC)+ 16 + 4;
dwError = GetLastError();
dwTrash = CryptImportKey(hCryptProv,szDesKeyBlob,dwDataLen,0,CRYPT_EXPORTABLE | CRYPT_NO_SALT,& hAESKey);
//模式
cMode = CRYPT_MODE_CBC;
dwTrash = CryptSetKeyParam (hAESKey,KP_MODE,(BYTE *)& cMode,0);
// IV
dwTrash = CryptSetKeyParam(hAESKey,KP_IV,szIV,0);
memset(szBinDataOut,0x00,sizeof(szBinDataOut));
memcpy(szBinDataOut,szSeed,iSizeOfszSeed);
dwDataLen = 144;
memset(szKey,0x00,iSizeOfszKey);
memcpy(szKey,szSeed,dwDataLen);
dwTrash = CryptDecrypt(hAESKey,0,FALSE,0,szKey, & dwDataLen);
dwError = GetLastError(); // NTE_BAD_ALGID  80090008
I am importing a user known key using the PUBLICKEYSTRUC and CryptImportKey. Sometimes this works and sometimes it does not. It seems to be related to the memory location that the key gets loaded. This is a plain text key that is not encoded (This is not a public key that is encoded in a certificate). My code is as follows
//sample key 16 bytes with a null on the end
char szKey[17] = {0x0C, 0x42, 0x11, 0x87, 0x46, 0x19, 0x83, 0x77, 0x57, 0x98, 0x12, 0x68, 0x21, 0x64, 0x34, 0x71, 0x00};
//sample IV omitted as it is the same format.
PUBLICKEYSTRUC * pMyKeyBlob;
CONST BYTE szDesKeyBlob[1024];
DWORD dwAlgoId;
BYTE szBinDataSize[] = {0x10, 0x00, 0x00, 0x00 };
memset( szDesKeyBlob, 0x00, sizeof(szDesKeyBlob));
if (!CryptAcquireContext(&hCryptProv, "MyData", MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0 ))
{
dwError = GetLastError();
if ( dwError = NTE_BAD_KEYSET )
if (!CryptAcquireContext(&hCryptProv, "MyData", MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_NEWKEYSET ))
{
sprintf( szErrorMessage, "Error %08X", dwError );
MessagePopup ("Error", szErrorMessage);
}
}
pMyKeyBlob = (PUBLICKEYSTRUC *) szDesKeyBlob;
pMyKeyBlob->bType = PLAINTEXTKEYBLOB;
pMyKeyBlob->bVersion = CUR_BLOB_VERSION;
pMyKeyBlob->reserved = 0;
pMyKeyBlob->aiKeyAlg = CALG_AES_128;
memcpy( szDesKeyBlob+sizeof(PUBLICKEYSTRUC), szBinDataSize, 4 ); //size
memcpy( szDesKeyBlob+sizeof(PUBLICKEYSTRUC)+4, szBinData, 16 ); //key
dwDataLen = sizeof(PUBLICKEYSTRUC)+16+4;
dwError = GetLastError();
dwTrash = CryptImportKey(hCryptProv, szDesKeyBlob, dwDataLen, 0, CRYPT_EXPORTABLE|CRYPT_NO_SALT, &hAESKey );
//mode
cMode = CRYPT_MODE_CBC;
dwTrash = CryptSetKeyParam(hAESKey, KP_MODE, (BYTE *)&cMode, 0);
//IV
dwTrash = CryptSetKeyParam(hAESKey, KP_IV, szIV, 0);
memset( szBinDataOut, 0x00, sizeof(szBinDataOut));
memcpy( szBinDataOut, szSeed, iSizeOfszSeed );
dwDataLen = 144;
memset( szKey, 0x00, iSizeOfszKey);
memcpy( szKey, szSeed, dwDataLen );
dwTrash = CryptDecrypt( hAESKey, 0, FALSE, 0, szKey, &dwDataLen );
dwError = GetLastError(); //NTE_BAD_ALGID 80090008
这篇关于调用CryptDecrypt时NTE_BAD_ALGID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!