我按照这个例子。我正在尝试将从服务器获得的公钥添加到密钥对中,并且我得到了 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 保留。我不确定出了什么问题。请建议我犯错的地方。如果不是,请建议一个从字符串导入公钥的示例。提前致谢。