Bad key error with importing private key
From: prs(tm) (PaulRSchmidt_at_gmail.com)
Date: 09/09/05
- Next message: Sylvain: "Re: SCardControl()"
- Previous message: lewix: "Image generated from scanner/fingerprint device"
- Next in thread: lelteto: "RE: Bad key error with importing private key"
- Reply: lelteto: "RE: Bad key error with importing private key"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: 9 Sep 2005 11:11:55 -0700
Maybe it's just an endian issue, but I would have thought that would
just cause problems with encryption/decryption: I'm trying to import an
RSA private key generated on a *nix box, and failing. Following is my
test jig; any insights would be most appreciated:
#define SHORTLEN (1024/16)
#define LONGLEN (1024/8)
#pragma pack(1)
struct SBlob
{
BLOBHEADER blobheader;
RSAPUBKEY rsapubkey;
BYTE modulus[LONGLEN];
BYTE prime1[SHORTLEN];
BYTE prime2[SHORTLEN];
BYTE exponent1[SHORTLEN];
BYTE exponent2[SHORTLEN];
BYTE coefficient[SHORTLEN];
BYTE privateExponent[LONGLEN];
};
void CPKBlobDlg::OnBnClickedButton1()
{
SBlob blob;
blob.blobheader.bType = PRIVATEKEYBLOB;
blob.blobheader.bVersion = CUR_BLOB_VERSION;
blob.blobheader.reserved = 0;
blob.blobheader.aiKeyAlg = CALG_RSA_KEYX;
blob.rsapubkey.magic = '2ASR'; // 'RSA2'
blob.rsapubkey.bitlen = 1024;
blob.rsapubkey.pubexp = 65537;
HexToBin(sz_N, blob.modulus, LONGLEN);
HexToBin(sz_p, blob.prime1, SHORTLEN);
HexToBin(sz_q, blob.prime2, SHORTLEN);
HexToBin(sz_exponent1, blob.exponent1, SHORTLEN);
HexToBin(sz_exponent2, blob.exponent2, SHORTLEN);
HexToBin(sz_coefficient, blob.coefficient, SHORTLEN);
HexToBin(sz_d, blob.privateExponent, LONGLEN);
BOOL bOk;
DWORD dw;
HCRYPTPROV hProv;
bOk = ::CryptAcquireContext(&hProv,
"saKeyContiner",
NULL,
PROV_RSA_FULL,
CRYPT_SILENT);
if (!bOk)
{
bOk = ::CryptAcquireContext(&hProv,
"saKeyContiner",
NULL,
PROV_RSA_FULL,
CRYPT_SILENT|CRYPT_NEWKEYSET);
}
dw = ::GetLastError();
int iSize = (int)sizeof(blob);
HCRYPTKEY hKey;
// Following fails with 0x80090003, Bad Key
bOk = ::CryptImportKey(hProv,
(BYTE*)&blob,
sizeof(blob),
0,
CRYPT_EXPORTABLE,
&hKey);
dw = ::GetLastError();
::CryptDestroyKey(hKey);
}
- Next message: Sylvain: "Re: SCardControl()"
- Previous message: lewix: "Image generated from scanner/fingerprint device"
- Next in thread: lelteto: "RE: Bad key error with importing private key"
- Reply: lelteto: "RE: Bad key error with importing private key"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]