当我尝试导入公钥时,BCryptImportKeyPair返回STATUS_INVALID_PARAMETER [英] BCryptImportKeyPair returns STATUS_INVALID_PARAMETER when i try to import public key

查看:334
本文介绍了当我尝试导入公钥时,BCryptImportKeyPair返回STATUS_INVALID_PARAMETER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了此示例.我试图将我从服务器获得的公钥添加到密钥对中,并且得到STATUS_INVALID_PARAMETER.

I followed this example. I am trying to add the public key which i got from the server into the key Pair and I am getting STATUS_INVALID_PARAMETER.

    BCRYPT_DH_KEY_BLOB header;
    header.dwMagic = BCRYPT_DH_PUBLIC_MAGIC;
    header.cbKey = (ULONG)(pub_key.size());
    cout << "header contents " << header.dwMagic << " : " << header.cbKey << endl;
    memcpy(&pubKeyBlobFromServer[0], &header, sizeof(BCRYPT_DH_KEY_BLOB));
    // copy Public key
    cout << "size of pub_key " << pub_key.size() << endl;
    cout << "size of pubKeyBlobFromServer before :" << pubKeyBlobFromServer.size() << endl;
    cout << "size of BCRYPT_DH_KEY_BLOB " << sizeof(BCRYPT_DH_KEY_BLOB) << endl;
    pubKeyBlobFromServer.insert(pubKeyBlobFromServer.end(), pub_key.begin(), pub_key.end());
    cout << "size of pubKeyBlobFromServer after :" << pubKeyBlobFromServer.size() << endl;
    Status = BCryptImportKeyPair(
                                        ExchAlgHandleB,             // Alg handle
                                        nullptr,                       // Parameter not used
                                        BCRYPT_DH_PUBLIC_BLOB,      // Blob type (Null terminated unicode string)
                                        &PubKeyHandleB,             // Key handle that will be recieved
                                        const_cast<PUCHAR>(pubKeyBlobFromServer.data()),            // Buffer than points to the key blob
                                        (ULONG)pubKeyBlobFromServer.size(),     // Buffer length in bytes
                                        0);                         // Flags

我得到以下输出.

header contents 1112557636 : 128
size of pub_key 128
size of pubKeyBlobFromServer before :8
size of BCRYPT_DH_KEY_BLOB 8
size of pubKeyBlobFromServer after :136

我尝试打印pubKeyBlobFromServer的字节.公钥从第8个字节开始.前8个为BCRYPT_DH_KEY_BLOB保留.我不确定是怎么了.请提出我犯错的地方.如果不是,请提出一个示例,该示例从字符串中导入公钥.预先感谢.

I tried printing the bytes of pubKeyBlobFromServer. the public key starts from 8th byte. first 8 is reserved for BCRYPT_DH_KEY_BLOB . I am not sure what is wrong. Please suggest the place where i am making mistake. If not please suggest a sample which imports public key from string. Thanks in Advance.

推荐答案

Microsoft的示例代码可以轻松解决;因为相同的API导出了密钥,所以它已经采用了正确的格式.

Microsoft's sample code takes the easy way out; because the same API exported the key, it is already in the right format.

为了自己构建有效的密钥blob,您需要查找

In order to construct a valid key blob yourself, you need to look up the documentation for the BCRYPT_DH_KEY_BLOB structure:

Diffie-Hellman公钥BLOB(BCRYPT_DH_PUBLIC_BLOB)在连续内存中具有以下格式.模数,生成器和公用号采用大端格式.

A Diffie-Hellman public key BLOB (BCRYPT_DH_PUBLIC_BLOB) has the following format in contiguous memory. The Modulus, Generator, and Public numbers are in big-endian format.

BCRYPT_DH_KEY_BLOB
Modulus[cbKey] // Big-endian.
Generator[cbKey] // Big-endian.
Public[cbKey] // Big-endian.

看起来您的代码仅包含三个组件之一.

Looks like your code was only including one of the three components.

这篇关于当我尝试导入公钥时,BCryptImportKeyPair返回STATUS_INVALID_PARAMETER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆