【发布时间】:2020-11-17 04:21:40
【问题描述】:
我很难解密使用 OpenSSL 和 RSA_PKCS1_OAEP_PADDING 填充选项加密的数据。
我正在做的是:
BCRYPT_ALG_HANDLE hCryptAlg = NULL;
BCRYPT_OAEP_PADDING_INFO paddingInfo = { 0 };
DWORD cbDecryptedMessage;
BYTE* pbDecryptedMessage = NULL;
paddingInfo.pszAlgId = BCRYPT_SHA1_ALGORITHM;
// Open an algorithm handle.
BCryptOpenAlgorithmProvider(&hCryptAlg, BCRYPT_RSA_ALGORITHM, NULL, 0);
// Calculate the required buffer
NCryptDecrypt(m_hKeyContextFull, (LPBYTE)pEncrypted, encryptedLenInBytes, &paddingInfo, NULL, cbDecryptedMessage, &outputDataLen, NCRYPT_PAD_OAEP_FLAG | NCRYPT_SILENT_FLAG);
// After required buffer is allocated...
NCryptDecrypt(m_hKeyContextFull, (LPBYTE)pEncrypted, encryptedLenInBytes, &paddingInfo, pbDecryptedMessage, cbDecryptedMessage, &outputDataLen, NCRYPT_PAD_OAEP_FLAG | NCRYPT_SILENT_FLAG);
NTE_INVALID_PARAMETER (0x80090027) 失败。我尝试了不同的标志,但它们都不起作用。
注意:m_hKeyContextFull 已经使用 CryptAcquireCertificatePrivateKey 函数调用检索到:
m_hSystemStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE, m_storeName.c_str());
m_pCertWithKeys = CertFindCertificateInStore(m_hSystemStore, SupportedEncodings, 0, CERT_FIND_SUBJECT_STR, m_certName.c_str(), NULL);
// Obtain the private key from the certificate.
DWORD m_KeyContextSpec = 0;
HCRYPTPROV_OR_NCRYPT_KEY_HANDLE m_hKeyContextFull;
CryptAcquireCertificatePrivateKey(m_pCertWithKeys, CRYPT_ACQUIRE_SILENT_FLAG | CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG, NULL, &m_hKeyContextFull, &m_KeyContextSpec, &m_KeyContextMustBeReleased);
注意:为了便于阅读,所有错误检查都已从代码中删除。
你知道我做错了什么吗?
谢谢。
【问题讨论】:
-
m_hKeyContextFull来自哪里? RSA 密钥从哪里加载等?非常不完整的代码。发布一个完整的示例,而不仅仅是一个sn-p。 -
paddingInfo无效。 -
嗨 Henno,感谢您的评论,我实际上不想通过发布完整示例来增加复杂性,密钥是使用 CertOpenStore 从 Windows 密钥存储加载的,然后调用 CertFindCertificateInStore。所有用于加密和解密的单元测试都通过了。但问题是当我尝试解密使用 OpenSSL 加密的数据时,并指定了 RSA_PKCS1_OAEP_PADDING 标志。我可以通过 SoftHSM 解密它,但在 MSCRYPTO 中我只是失败了。我会更新问题。