在 CryptoAPI 中导出密钥时出现 BAD_UID 错误
我正在为 Microsoft CryptoAPI 编写一个测试应用程序。我想使用第二方的公钥导出一方的密钥,然后将该密钥作为第二方的密钥导入(这会设置用于通信的共享密钥)。这是我的代码:
if(!CryptExportKey(encryptT->hSymKey, decryptT->hPubKey, SIMPLEBLOB, 0, keyExBuf, &bufLen)) {
FormattedDebugPrint(NULL, GetLastError(), "could not export secret key", TRUE);
return -1;
}
if(!CryptImportKey(decryptT->hCryptProv, keyExBuf, bufLen, decryptT->hPubKey, 0, &(decryptT->hSymKey))) {
FormattedDebugPrint(NULL, GetLastError(), "could not import secret key", TRUE);
return -1;
}
这给出了错误:
80090001: Bad UID.
正在通过调用为 encryptT 和解密T(发送者,接收者)生成公钥对:
CryptGenKey(encryptT->hCryptProv, CALG_RSA_KEYX, CRYPT_EXPORTABLE, &(encryptT->hPubKey))
知道可能导致错误的原因吗?
谢谢,
I am writing a test application for Microsoft CryptoAPI. I want to export the secret key of one party using the public key of the second party, and then import that secret key as the second party's secret key (this sets up a shared secret key for communication). Here is my code:
if(!CryptExportKey(encryptT->hSymKey, decryptT->hPubKey, SIMPLEBLOB, 0, keyExBuf, &bufLen)) {
FormattedDebugPrint(NULL, GetLastError(), "could not export secret key", TRUE);
return -1;
}
if(!CryptImportKey(decryptT->hCryptProv, keyExBuf, bufLen, decryptT->hPubKey, 0, &(decryptT->hSymKey))) {
FormattedDebugPrint(NULL, GetLastError(), "could not import secret key", TRUE);
return -1;
}
And this gives the error:
80090001: Bad UID.
The public keypair is being generated for both encryptT and decryptT (sender, receiver) by calling:
CryptGenKey(encryptT->hCryptProv, CALG_RSA_KEYX, CRYPT_EXPORTABLE, &(encryptT->hPubKey))
Any idea what could be causing the error?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我已经弄清楚了。基本上,即使它以相同的方式初始化,您也不能直接使用另一个公钥 - 我需要首先导出该公钥,然后使用另一方的加密提供程序的句柄将其导入。
Never mind, I figured it out. Basically, you can't just use another public key directly even if it's initialized the same way -- I needed to first export that public key, and then import it using the handle to the cryptographic provider of the other party.