I am calling the BCryptDecrypt
function which is returning an error. I get the error 120 using getlasterror
which means this function is not supported in this system (in msdn).
status = BCryptDecrypt( hKey, pbInput, cbInput, NULL, NULL, sizeof(DWORD), NULL, 0, &pcbResult, BCRYPT_BLOCK_PADDING);
pbInput
is the pointer to address which contains the data to be decrypted, cbInput
is the length of the file and pcbResult
will get the size of the output file (decrypted data). BCryptEncrypt
is working fine but BCryptDecryptis
not working.
Can anyone please help me out? few lines of code related to decrypt:
status = BCryptOpenAlgorithmProvider(&hAlgorithm , BCRYPT_AES_ALGORITHM , NULL , 0); if (!NT_SUCCESS(status)) { return; }
DWORD cbKey = 0;
DWORD cbData =0;
status = BCryptSetProperty(hAlgorithm , BCRYPT_CHAINING_MODE , (PBYTE)BCRYPT_CHAIN_MODE_ECB , sizeof(BCRYPT_CHAIN_MODE_ECB) , 0);
if (!NT_SUCCESS(status))
{
return;
}
status = BCryptGetProperty(hAlgorithm,
BCRYPT_OBJECT_LENGTH,
(LPBYTE)&cbData,
sizeof(DWORD),
&cbKey,
0);
LPBYTE pbKey = (BYTE*)HeapAlloc(GetProcessHeap() , 0 , cbData);
LPCSTR szpwd = (LPCSTR)Getpwd();
BCRYPT_KEY_HANDLE hKey = NULL;
status = BCryptGenerateSymmetricKey(hAlgorithm,
&hKey,
pbKey,
cbData,
(PUCHAR)szpwd,
(ULONG)strlen(szpwd),
0);
if (!NT_SUCCESS(status))
{
if(hAlgorithm)
{
BCryptCloseAlgorithmProvider(hAlgorithm,0);
}
if(pbKey)
{
HeapFree(GetProcessHeap(), 0, pbKey);
}
return;
}
DWORD pcbResult = 0;
status = BCryptDecrypt( hKey,
pbInput,
cbInput,
NULL,
NULL,
sizeof(DWORD),
NULL,
0,
&pcbResult,
BCRYPT_BLOCK_PADDING);
DWORD cbError = GetLastError();
if(cbError != 0)
{
LPCSTR messageBuffer = NULL;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, cbError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);