从MSDN使用BCryptDeriveKeyPBKDF2的示例



我没有找到一些使用BCryptDeriveKeyPBKDF2函数的简单示例。我需要用PBKDF2算法获得密钥。我认为这是简单的行动,但我发现非常困难的方法。我使用的是CryptoApi,我需要get key。

请告诉我如何使用此函数获得密钥BCryptDeriveKeyPBKDF2 ?

https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptderivekeypbkdf2

我也很难找到一个简单的例子。我最终创建了这个:

#pragma comment(lib, "bcrypt.lib")
#include <Windows.h>
#include <string>
#include <iostream>
int main()
{
std::string password = "This is the password that will be encrypted";
std::string salt = "EncryptionSalt";
NTSTATUS Status;
BYTE DerivedKey[64];
BCRYPT_ALG_HANDLE handle;
Status = BCryptOpenAlgorithmProvider(&handle, BCRYPT_SHA512_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG);
if (Status != 0)
{
std::cout << "BCryptOpenAlgorithmProvider exited with error message " << Status;
goto END;
}
Status = BCryptDeriveKeyPBKDF2(handle, (BYTE*)password.data(), password.length(), (BYTE*)salt.data(), 8, 2048, DerivedKey, 64, 0);
if (Status != 0)
{
std::cout << "BCryptDeriveKeyPBKDF2 exited with error message " << Status;
goto END;
}
else
std::cout << "Operation completed successfully. Your encrypted key is in variable DerivedKey.";
BCryptCloseAlgorithmProvider(handle, 0);
END:;
}

相关内容

  • 没有找到相关文章

最新更新