- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
CryptHashData ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa380202(v=vs.85).aspx) 一直返回 FALSE。当我调用 GetLastError 时,我返回了一个 87 的值,它是常量 ERROR_INVALID_PARAMETER。我已经验证了我的参数(MSDN 注释说检查传入的指针)并且一切看起来都正常。我已将其尽可能靠近 MSDN 代码示例。
我的目标只是散列密码并从中派生 key 。
我在 Windows 10 上运行这段代码。
我的代码:
std::string sPassword = "123P@ssword"; //password to hash
HCRYPTHASH hHash = NULL; //password hash handle
HCRYPTPROV hHashKeyProvider = NULL; //provider to make the key derived from the hash
HCRYPTKEY hHashDerivedKey = NULL; //key derived from password hash
//get provider to the hash based password
if (!CryptAcquireContext(
&hHashKeyProvider,
0,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT
))
{
throw std::runtime_error("Could not acquire context to make key from hash");
}
//create hash object from provider
if (!CryptCreateHash(
hHashKeyProvider,
CALG_SHA1,
0,
0,
&hHash
))
{
CryptReleaseContext(hHashKeyProvider, 0);
throw std::runtime_error("Could not create hash");
}
//get hash of password
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa380202(v=vs.85).aspx
BYTE* pbPasswordBuffer = (BYTE*)sPassword.c_str();
DWORD dwPasswordBufferLength = strlen((char*)pbPasswordBuffer);
if (!CryptHashData(
hHashKeyProvider,
pbPasswordBuffer,
dwPasswordBufferLength,
0
))
{
CryptReleaseContext(hHashKeyProvider, 0);
DWORD dwLast = GetLastError(); //THIS EQUALS 87 for ERROR_INVALID_PARAMETER WHY???
throw std::runtime_error("Could not hash password");
}
//create key from hash
if (!CryptDeriveKey(
hHashKeyProvider,
CALG_AES_256,
hHash,
0,
&hHashDerivedKey
))
{
CryptDestroyHash(hHash);
CryptReleaseContext(hHashKeyProvider, 0);
throw std::runtime_error("Could not create key from password hash");
}
//free the hash
CryptDestroyHash(hHash);
建议?
最佳答案
注意,您不小心将 hHashKeyProvider
而不是 hHash
传递给了 CryptHashData()
。
此外,您的 GetLastError()
调用应该在 CryptReleaseContext()
之前进行,否则后者中的错误可能看起来像是来自 CryptHashData()
代替。
关于c++ - CryptHashData 返回 ERROR_INVALID_PARAMETER (CAPI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48431937/
CryptHashData ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa380202(v=vs.85).aspx) 一直返
我正在尝试将一些现有的 C++ 代码转换为 C#。我有一些代码在同一个 HCRYPTHASH 上调用 CryptHashData 3 次。文档说“可以多次调用此函数和 CryptHashSession
我是一名优秀的程序员,十分优秀!