如何使用BCryptVerifyPassword

问题描述 投票:0回答:0

如何在 C++ MFC 项目中使用 bcrypt 来验证数据库中的密码哈希值?当前有一部分数据库带有 bcrypt 加密的哈希值。我在C++ MFC中已经成功连接到数据库,可以获取到数据了。我写了一段代码,但我似乎无法使用BCryptVerifyPassword,按照说明,我已经包含了bcrypt.h并导入了bcrypt.lib,我的bcrypt是通过在目录C:\Program Files中安装windows SDK获得的( x86)\ Windows Kits \lib or include,使用x64下的文件

#include "pch.h"
#include "MYCLASS.h"

#include <bcrypt.h>
#pragma comment(lib, "bcrypt.lib")

bool MYCLASS::VerifyPassword(CString inputPassword, char* sqlHash) {
    // Convert input password to UTF-16
    LPCWSTR utf16Password = inputPassword.AllocSysString();

    // Get the length of the hash in bytes
    int hashLength = MultiByteToWideChar(CP_ACP, 0, sqlHash, -1, NULL, 0);

    // Convert hash to UTF-16
    LPWSTR utf16Hash = new WCHAR[hashLength];
    MultiByteToWideChar(CP_ACP, 0, sqlHash, -1, utf16Hash, hashLength);

    // Verify the password
    BOOL result = FALSE;
    if (BCryptVerifyPassword(utf16Password, utf16Hash, (PUCHAR)NULL, 0, NULL, 0, &result) != 0) {
        // Error occurred
        delete[] utf16Hash;
        return false;
    }

    delete[] utf16Hash;
    return (result == TRUE);
}
c++ mfc bcrypt
© www.soinside.com 2019 - 2024. All rights reserved.