使用bcrypt加密(VC ++) [英] Encryption using bcrypt (VC++)
本文介绍了使用bcrypt加密(VC ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用SHA256算法获取哈希值。
我的应用程序生成的哈希值与在线工具生成的哈希值相同。
我尝试过:
I used SHA256 algorithm for getting hash.
THe hash produced by my application is not same as the one generated by online tools for the same string.
What I have tried:
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_HASH_HANDLE hHash = NULL;
NTSTATUS status = STATUS_UNSUCCESSFUL;
DWORD cbData = 0,
cbHash = 0,
cbHashObject = 0;
PBYTE pbHashObject = NULL;
PBYTE pbHash = NULL;
//open an algorithm handle
if(!NT_SUCCESS(status = BCryptOpenAlgorithmProvider(
&hAlg,
BCRYPT_SHA256_ALGORITHM,
NULL,
0)))
{
goto Cleanup;
}
//calculate the size of the buffer to hold the hash object
if(!NT_SUCCESS(status = BCryptGetProperty(
hAlg,
BCRYPT_OBJECT_LENGTH,
(PBYTE)&cbHashObject,
sizeof(DWORD),
&cbData,
0)))
{
goto Cleanup;
}
//allocate the hash object on the heap
pbHashObject = (PBYTE)HeapAlloc (GetProcessHeap (), 0, cbHashObject);
if(NULL == pbHashObject)
{
goto Cleanup;
}
//calculate the length of the hash
if(!NT_SUCCESS(status = BCryptGetProperty(
hAlg,
BCRYPT_HASH_LENGTH,
(PBYTE)&cbHash,
sizeof(DWORD),
&cbData,
0)))
{
goto Cleanup;
}
//allocate the hash buffer on the heap
pbHash = (PBYTE)HeapAlloc (GetProcessHeap (), 0, cbHash);
if(NULL == pbHash)
{
goto Cleanup;
}
//create a hash
if(!NT_SUCCESS(status = BCryptCreateHash(
hAlg,
&hHash,
pbHashObject,
cbHashObject,
NULL,
0,
0)))
{
goto Cleanup;
}
PCWSTR pwTst = (PCWSTR)csText_i;
//hash some data
if(!NT_SUCCESS(status = BCryptHashData(
hHash,
(PBYTE)pwTst,
sizeof(rgbMsg),
0)))
{
goto Cleanup;
}
//close the hash
if(!NT_SUCCESS(status = BCryptFinishHash(
hHash,
pbHash,
cbHash,
0)))
{
goto Cleanup;
}
PCWSTR pTst = (PCWSTR)pbHash;
csEnryptedText_o = pTst;
是因为我投射的方式??
Is it because of the way i cast??
推荐答案
ccInput
参数BCryptHashData
错误:
ThecbInput
parameter ofBCryptHashData
is wrong:
if(!NT_SUCCESS(status = BCryptHashData(
hHash,
(PBYTE)pwTst,
sizeof(rgbMsg),
0)))
{
goto Cleanup;
}
cbInput [in]
pbInput缓冲区中的字节数。
cbInput [in]
The number of bytes in the pbInput buffer.
应该可能是
It should be probably
if(!NT_SUCCESS(status = BCryptHashData(
hHash,
(PBYTE)pwTst,
wcslen(pwTest) * sizeof(wchar_t),
0)))
{
goto Cleanup;
}
这要求 csText_i
是一个空终止的宽字符串。该行
This requires that csText_i
is a null terminated wide string. The line
PCWSTR pwTst = (PCWSTR)csText_i;
让我假设情况并非如此,否则应该没有必要强制转换为 PCWSTR
。
let me assume that this is not the case because otherwise there should be no need to cast to PCWSTR
.
这篇关于使用bcrypt加密(VC ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文