CryptHashData returns ERROR_INVALID_PARAMETER (CAPI)
CryptHashData returns ERROR_INVALID_PARAMETER (CAPI)
CryptHashData (https://msdn.microsoft.com/en-us/library/windows/desktop/aa380202(v=vs.85(.aspx( 始终返回 FALSE。当我调用 GetLastError 时,我返回的值为 87,这是常量ERROR_INVALID_PARAMETER。我已经验证了我的参数(MSDN 注释说要检查传入的指针(,一切看起来都很好。我已将其尽可能靠近 MSDN 代码示例。
我的目标只是散列密码并从中派生密钥。
我正在 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()
。
if (!CryptHashData(
hHashKeyProvider,
应该是
if (!CryptHashData(
hHash,
相关文章:
- 修复"-Wunused-parameter"取决于预处理器条件的警告
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- 为什么"types may not be defined in parameter types"
- 如何理解"abstract-declarator containing an ellipsis shall only be used in a parameter-declaration"
- 为什么这段代码抱怨"a non-type template parameter cannot have type"?
- 如何抛出"template parameter"类型的异常?
- 如何在开关语句中使用 cin "as a parameter"
- 派生类中函数参数变化的虚函数按常量类型在"function parameter"会破坏虚拟机制吗?
- 在 lambda(parameter) 中为 std::transform with std::sregex_token
- 如何修复此"argument of type incompatible with parameter"错误?
- C 从XML导入CAPI/CNG中的RSA私钥
- 谷歌模拟 - 怎么说"function must be called ONCE with a certain parameter but ok to be called many times with
- CUDA 内核"Only a single pack parameter is allowed"解决方法?
- "Cannot convert parameter 1 from double to float"虽然没有更改参数
- CryptHashData returns ERROR_INVALID_PARAMETER (CAPI)
- rvalue for a std::string parameter
- 在 MSVC 上,为什么没有出现 std::string 的警告"unreferenced formal parameter"?
- result_of, make_tuple, parameter pack
- "useinbandfec is a unidirectional receive-only parameter"作品中是什么意思?
- 重新定义类<template-parameter-1-2>的默认参数