Error importing public key using cryptoapi CryptImportKey

吃可爱长大的小学妹 提交于 2019-12-07 15:02:40

问题


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.


回答1:


RSAPUBKEY* rsapubkey = (RSAPUBKEY*)keyBlob + sizeof(BLOBHEADER);

should instead read

RSAPUBKEY* rsapubkey = (RSAPUBKEY*) (keyBlob + sizeof(BLOBHEADER));


来源:https://stackoverflow.com/questions/14838123/error-importing-public-key-using-cryptoapi-cryptimportkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!