本文介绍了错误使用CryptoAPI的CryptImportKey导入公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想公开密钥blob导入到CSP。但错误发生。
字节pbData [] ={0xEB,0x2A,0x38,0x56,0x86,0x61,0x88,0x7F,0xA1,0x80,0xBD,0xDB,0x5C,0xAB,0xD5,0xF2,0x1C,0x7B,0xFD,0x59,0xC0,0x90,0xCB,0x2D,0x24,0x5A,0x87,0xAC,0x25,0x30,0x62,0x88,0x27,0x29,0x29,0x3E,0x55,0x06,0x35,0x05,0x08,0xE7,0xF9,0xAA,0x3B,0xB7,0x7F,0x43,0x33,0x23,0x14,0x90,0xF9,0x15,0xF6,0xD6,0x3C,0x55,0xFE,0x2F,0x08,0xA4,0x9B,0x35,0x3F,0x44,0x4A,0xD3,0x99,0x3C,0xAC,0xC0,0x2D,0xB7,0x84,0xAB,0xBB,0x8E,0x42,0xA9,0xB1,0xBB,0xFF,0xFB,0x38,0xBE,0x18,0xD7,0x8E,0x87,0xA0,0xE4,0x1B,0x9B,0x8F,0x73,0xA9,0x28,0xEE,0x0C,0xCE,0xE1,0xF6,0x73,0x98,0x84,0xB9,0x77,0x7E,0x4F,0xE9,0xE8,0x8A,0x1B,0xBE,0x49,0x59,0x27,0xAC,0x4A,0x79,0x9B,0x31,0x81,0xD6,0x44,0x24,0x43};
USHORT modulusLengthInBytes = sizeof的(pbData);
HCRYPTKEY hPublicKey;
DWORD keyBlobLength = sizeof的(BLOBHEADER)+的sizeof(RSAPUBKEY)+ modulusLengthInBytes;
BYTE *密钥块=(BYTE *)malloc的(keyBlobLength);
BLOBHEADER * blobheader =(BLOBHEADER *)密钥块;
blobheader-> B型= PUBLICKEYBLOB;
blobheader-> bVersion = CUR_BLOB_VERSION;
blobheader->保留= 0;
blobheader-> aiKeyAlg = CALG_RSA_KEYX;
RSAPUBKEY * rsapubkey =(RSAPUBKEY *)密钥块+的sizeof(BLOBHEADER);
rsapubkey->魔术= 0x31415352;
rsapubkey-> bitlen = modulusLengthInBytes * 8;
rsapubkey-> pubexp = 65537; //或任何你的公用指数为。
BYTE *模量=密钥块+的sizeof(BLOBHEADER)+的sizeof(RSAPUBKEY);
的memcpy(模数,pbData,modulusLengthInBytes);
BRET = CryptImportKey(hProv,密钥块,keyBlobLength,0,0,&放大器; hPublicKey);
如果(!BRET)
{
dwRet = GetLastError函数();
}
CryptImportKey
返回0和 dwRet
是0x80090004(NTE_BAD_LEN)。我非常肯定的长度是否正确。但还是发生了同样的错误。
解决方案
RSAPUBKEY * rsapubkey =(RSAPUBKEY *)密钥块+的sizeof(BLOBHEADER);
而应该读
RSAPUBKEY * rsapubkey =(RSAPUBKEY *)(密钥块+的sizeof(BLOBHEADER));
I want to import public key blob to a CSP. but error occurred.
BYTE pbData[] ={0xEB,0x2A,0x38,0x56,0x86,0x61,0x88,0x7F,0xA1,0x80,0xBD,0xDB,0x5C,0xAB,0xD5,0xF2,0x1C,0x7B,0xFD,0x59,0xC0,0x90,0xCB,0x2D,0x24,0x5A,0x87,0xAC,0x25,0x30,0x62,0x88,0x27,0x29,0x29,0x3E,0x55,0x06,0x35,0x05,0x08,0xE7,0xF9,0xAA,0x3B,0xB7,0x7F,0x43,0x33,0x23,0x14,0x90,0xF9,0x15,0xF6,0xD6,0x3C,0x55,0xFE,0x2F,0x08,0xA4,0x9B,0x35,0x3F,0x44,0x4A,0xD3,0x99,0x3C,0xAC,0xC0,0x2D,0xB7,0x84,0xAB,0xBB,0x8E,0x42,0xA9,0xB1,0xBB,0xFF,0xFB,0x38,0xBE,0x18,0xD7,0x8E,0x87,0xA0,0xE4,0x1B,0x9B,0x8F,0x73,0xA9,0x28,0xEE,0x0C,0xCE,0xE1,0xF6,0x73,0x98,0x84,0xB9,0x77,0x7E,0x4F,0xE9,0xE8,0x8A,0x1B,0xBE,0x49,0x59,0x27,0xAC,0x4A,0x79,0x9B,0x31,0x81,0xD6,0x44,0x24,0x43};
USHORT modulusLengthInBytes = sizeof(pbData);
HCRYPTKEY hPublicKey;
DWORD keyBlobLength = sizeof(BLOBHEADER)+sizeof(RSAPUBKEY)+modulusLengthInBytes;
BYTE* keyBlob = (BYTE*)malloc(keyBlobLength);
BLOBHEADER* blobheader = (BLOBHEADER*) keyBlob;
blobheader->bType = PUBLICKEYBLOB;
blobheader->bVersion = CUR_BLOB_VERSION;
blobheader->reserved = 0;
blobheader->aiKeyAlg = CALG_RSA_KEYX;
RSAPUBKEY* rsapubkey = (RSAPUBKEY*)keyBlob + sizeof(BLOBHEADER);
rsapubkey->magic = 0x31415352;
rsapubkey->bitlen = modulusLengthInBytes*8;
rsapubkey->pubexp = 65537; // Or whatever your public exponent is.
BYTE* modulus = keyBlob + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY);
memcpy(modulus, pbData, modulusLengthInBytes);
bRet = CryptImportKey(hProv, keyBlob, keyBlobLength, 0, 0, &hPublicKey);
if (!bRet)
{
dwRet = GetLastError();
}
CryptImportKey
return 0 and dwRet
is 0x80090004(NTE_BAD_LEN). I am very sure the length is correct. but still same error occurred.
解决方案
RSAPUBKEY* rsapubkey = (RSAPUBKEY*)keyBlob + sizeof(BLOBHEADER);
should instead read
RSAPUBKEY* rsapubkey = (RSAPUBKEY*) (keyBlob + sizeof(BLOBHEADER));
这篇关于错误使用CryptoAPI的CryptImportKey导入公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!