如何在Windows上的C中导入加密公钥

时间:2010-09-23 21:27:45

标签: windows public-key-encryption

我有一个公钥,我想用它来加密一段数据。我正在尝试导入公钥以便使用它,但CryptImportKey给了我一个'invalic参数'错误。

有什么问题?

这是我的代码:

if( !CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) )
{
    /*
     * Print error and return
     */
}
pblob->header->bType = PUBLICKEYBLOB;
pblob->header->aiKeyAlg = CALG_AES_128;
pblob->header->bVersion = CUR_BLOB_VERSION;
pblob->header->reserved = 0;
pblob->key_len = key_len;

memcpy(pblob->key, key , key_len);

if( !CryptImportKey( &hProv,
    (LPCBYTE)pblob,
    sizeof(*pblob),
    0,
    CRYPT_EXPORTABLE,
    &hKey ) )
{
    // Print error and return
}

2 个答案:

答案 0 :(得分:0)

Windows CryptoAPI无法直接使用纯文本密钥;你必须跳过一点箍才能做到这一点。 Here is the knowledge base article describing how to do this

答案 1 :(得分:0)

感谢这里所说的一切,以及大量的搜索,我找到了答案: http://www.ms-news.net/f2748/problem-importing-public-key-4052577.html

用它创建我自己的程序,我在另一个帖子上发布: Load an X509 PEM file into Windows CryptoApi