Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用bcrypt库进行加密/解密_C++_Encryption_Cng - Fatal编程技术网

C++ 使用bcrypt库进行加密/解密

C++ 使用bcrypt库进行加密/解密,c++,encryption,cng,C++,Encryption,Cng,我正在调用BCryptDecrypt函数,该函数返回一个错误。我使用getlasterror获得错误120,这意味着msdn中的系统不支持此函数 status = BCryptDecrypt( hKey, pbInput, cbInput, NULL, NULL, sizeof(DWORD), NULL, 0, &pcbResult, BCRYPT_BLOCK_PADDING); pbInput是指向包含要解密的数据的地址的指针,cbInput是文件的长度,pcbResult将获得输出

我正在调用BCryptDecrypt函数,该函数返回一个错误。我使用getlasterror获得错误120,这意味着msdn中的系统不支持此函数

status = BCryptDecrypt( hKey, pbInput, cbInput, NULL, NULL, sizeof(DWORD), NULL, 0, &pcbResult, BCRYPT_BLOCK_PADDING);

pbInput是指向包含要解密的数据的地址的指针,cbInput是文件的长度,pcbResult将获得输出文件解密数据的大小。BCryptEncrypt工作正常,但BCryptDecrypt不工作

谁能帮帮我吗? 与解密相关的几行代码:

status=bcryptoppenalgorithmprovider&hAlgorithm,BCRYPT_AES_算法,NULL,0; 如果NT_成功状态 { 回来 }


请显示一些代码安装了错误的.net版本?您能简要解释一下您想说什么吗。我正在vs-8中用mfc编写代码。BCryptEncrypt工作正常,但BCryptDecrypt给出运行时错误。
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);