压缩天然气.AES.错误解密(可能加密)的文件

CNG. AES. Incorrectly decrypted(possibly encrypted) file

本文关键字:加密 文件 天然气 AES 错误 解密 压缩      更新时间:2023-10-16

我研究CNG API,我尝试加密文件,然后解密它。为了获取密钥,我使用 SHA512 进行 AES 加密,使用以下代码:

#include <QCoreApplication>
#include <windows.h>
#include <wincrypt.h>
#include <bcrypt.h>
#include <fstream>
#include <iostream>
#include <QDebug>
#include <tchar.h>
using namespace std;
int main(int argc, char *argv[])
{
srand(time(NULL));
const int blockSize = 64;
const int buffSize = 512;
DWORD cbHashObject = 0, cbData = 0, cbKeyObject = 0, cbKeyData = 0, cbBlockLen = 0, cbCipherData = 0, cbPlainData = 0, cbBlob = 0;
PBYTE pbHashObject = NULL;
PBYTE pbKeyObject = NULL;
BCRYPT_ALG_HANDLE hAlg;
BCRYPT_HASH_HANDLE hHashB;
BCRYPT_KEY_HANDLE hKey;
NTSTATUS status;
PBYTE pbHash, pbIV, pbBlob = NULL;
PBYTE pbPlainData = NULL, pbCipherData = NULL;
BYTE* password = (BYTE*)"pass";
QCoreApplication a(argc, argv);
char *buff = new char[buffSize + 1];

if (!BCRYPT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA512_ALGORITHM, NULL, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR1 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbHashObject, sizeof(DWORD), &cbData, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR2 " << hex << status << " E: " << hex << dwErr;
}
pbHashObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbHashObject);
if (!BCRYPT_SUCCESS(status = BCryptCreateHash(hAlg, &hHashB, pbHashObject, cbHashObject, NULL, 0, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR3 " << hex << status << " E: " << hex << dwErr;
}
ZeroMemory(buff, buffSize + 1);
if (!BCRYPT_SUCCESS(status = BCryptHashData(hHashB, (PBYTE)password, strlen((char*)password)*sizeof(BYTE), 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR5 " << hex << status << " E: " << hex << dwErr;
}
cbHashObject = 0, cbData = 0;
if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, (PBYTE)&cbHashObject, sizeof(DWORD), &cbData, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR6 " << hex << status << " E: " << hex << dwErr;
}
pbHash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbHashObject);
if (!BCRYPT_SUCCESS(status = BCryptFinishHash(hHashB, pbHash, cbHashObject, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR7 " << hex << status << " E: " << hex << dwErr;
}
char *str_hash = new char[cbHashObject*2+1];
for(int i = 0; i < cbHashObject;i++)
sprintf(str_hash+2*i,"%02X",pbHash[i]);
cout << "PASSWORD SHA512 = " <<str_hash<< endl << endl;
ZeroMemory(str_hash, cbHashObject);
if (!BCRYPT_SUCCESS(status = BCryptDestroyHash(hHashB)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR8 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptCloseAlgorithmProvider(hAlg, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR9 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, NULL, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR10 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbKeyData, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR11 " << hex << status << " E: " << hex << dwErr;
}
if(!BCRYPT_SUCCESS(status = BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR12 " << hex << status << " E: " << hex << dwErr;
}
pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject);
if(!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_BLOCK_LENGTH, (PBYTE)&cbBlockLen, sizeof(DWORD), &cbData, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR13 " << hex << status << " E: " << hex << dwErr;
}
pbIV= (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbBlockLen+1);
for(int i(0); i < cbBlockLen; i++)
{
//pbIV[i]=rand();
pbIV[i]=0xAA;
}
for(int i = 0; i < cbBlockLen ;i++)
sprintf(str_hash+2*i,"%02X",pbIV[i]);
cout << "nnIV = " << str_hash;
if(!BCRYPT_SUCCESS(status = BCryptGenerateSymmetricKey(hAlg, &hKey, pbKeyObject, cbKeyObject, pbHash, cbHashObject, 0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR14 " << hex << status << " E: " << hex << dwErr;
}
if(!BCRYPT_SUCCESS(status = BCryptExportKey(hKey,NULL,BCRYPT_OPAQUE_KEY_BLOB,NULL,0,&cbBlob,0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR15 " << hex << status << " E: " << hex << dwErr;
}
pbBlob = (PBYTE)HeapAlloc(GetProcessHeap (), 0, cbBlob);
if(!BCRYPT_SUCCESS(status = BCryptExportKey(hKey,NULL,BCRYPT_OPAQUE_KEY_BLOB,pbBlob,cbBlob,&cbBlob,0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR16 " << hex << status << " E: " << hex << dwErr;
}
ifstream f1("..\1.txt");
ofstream f2("..\2.txt", ios_base::trunc);
pbPlainData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, blockSize + 1);
ZeroMemory(pbPlainData, blockSize + 1);
f1.read((char*)pbPlainData, blockSize);
if(!BCRYPT_SUCCESS(status = BCryptEncrypt(hKey, (BYTE*)pbPlainData, blockSize, NULL, 0, NULL, NULL, 0, &cbCipherData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR15 " << hex << status << " E: " << hex << dwErr;
}
pbCipherData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbCipherData + 1);
ZeroMemory(pbCipherData, cbCipherData + 1);
do {
if (!BCRYPT_SUCCESS(status = BCryptEncrypt(hKey, (BYTE*)pbPlainData, blockSize, NULL, 0, NULL, (BYTE*)pbCipherData, cbCipherData, &cbData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR17 " << hex << status << " E: " << dwErr;
}
f2.write((char*)pbCipherData, cbData);
f2.flush();
f1.read((char*)pbPlainData, blockSize);
} while(!f1.eof());
f1.close();
f2.close();
BCryptDestroyKey(hKey);
HeapFree(GetProcessHeap(), 0, pbPlainData);
HeapFree(GetProcessHeap(), 0, pbCipherData);
pbPlainData = NULL;
pbCipherData = NULL;
memset(pbIV, 0xAA, cbBlockLen);
memset(pbKeyObject, 0 , cbKeyObject);

if(!BCRYPT_SUCCESS(status = BCryptImportKey(hAlg,NULL,BCRYPT_OPAQUE_KEY_BLOB,&hKey,pbKeyObject,cbKeyObject,pbBlob,cbBlob,0)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR18 " << hex << status << " E: " << hex << dwErr;
}
ifstream f3("..\2.txt");
ofstream f4("..\3.txt", ios_base::trunc);
pbCipherData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, blockSize + 1);
ZeroMemory(pbCipherData, blockSize + 1);
f3.read((char*)pbCipherData, blockSize);
if(!BCRYPT_SUCCESS(status = BCryptEncrypt(hKey, (BYTE*)pbCipherData, blockSize, NULL, NULL, 0, NULL, 0, &cbPlainData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR19 " << hex << status << " E: " << hex << dwErr;
}
pbPlainData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbPlainData);
ZeroMemory(pbPlainData, cbPlainData);
do{
if (!BCRYPT_SUCCESS(status = BCryptEncrypt(hKey, (BYTE*)pbCipherData, blockSize, NULL, NULL, 0, (BYTE*)pbPlainData, cbPlainData, &cbData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
qDebug()<<"ERROR20 " << hex << status << " E: " << dwErr;
}
f4.write((char*)pbPlainData, cbData);
f4.flush();
f3.read((char*)pbCipherData, blockSize);
}while(!f3.eof());
f3.close();
f4.close();
return a.exec();
}

起初以为问题出在IV中,但是我将其删除,没有任何变化。以下是示例:

加密前(1.txt(:

1234567890

加密后(2.txt(:

O“сh4W;H;7Т†Gж
qї
–JmdВу•оё:s±°|kД'y|‰зXgK4ПбЖl©bЪmЗШоЏMfU¶ysr‰Ї[Іґћ·У:RљС

解密后(3.txt(:

ї
–JmdВу•оё:s_T–їя5rJ™U,ЁМЈ‘µМ­ґВ0уБЉO ‰CifБZDЧЈ)Тў«э"ќкгИу}ШЩєьъ©фba[

我已经解决了你的问题,在最后两行(206,215(将BCryptEncrypt更改为BCryptDecrypt

#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <wincrypt.h>
#include <bcrypt.h>
#include <fstream>
#include <iostream>
#include <tchar.h>
#pragma comment(lib, "bcrypt.lib")
using namespace std;
int main(int argc, char* argv[])
{
const int blockSize = 64;
const int buffSize = 512;
DWORD cbHashObject = 0, cbData = 0, cbKeyObject = 0, cbKeyData = 0, cbBlockLen = 0, cbCipherData = 0, cbPlainData = 0, cbBlob = 0;
PBYTE pbHashObject = NULL;
PBYTE pbKeyObject = NULL;
BCRYPT_ALG_HANDLE hAlg;
BCRYPT_HASH_HANDLE hHashB;
BCRYPT_KEY_HANDLE hKey;
NTSTATUS status;
PBYTE pbHash, pbIV, pbBlob = NULL;
PBYTE pbPlainData = NULL, pbCipherData = NULL;
BYTE* password = (BYTE*)"pass";
char* buff = new char[buffSize + 1];

if (!BCRYPT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA512_ALGORITHM, NULL, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR1 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbHashObject, sizeof(DWORD), &cbData, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR2 " << hex << status << " E: " << hex << dwErr;
}
pbHashObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbHashObject);
if (!BCRYPT_SUCCESS(status = BCryptCreateHash(hAlg, &hHashB, pbHashObject, cbHashObject, NULL, 0, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR3 " << hex << status << " E: " << hex << dwErr;
}
ZeroMemory(buff, buffSize + 1);
if (!BCRYPT_SUCCESS(status = BCryptHashData(hHashB, (PBYTE)password, strlen((char*)password) * sizeof(BYTE), 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR5 " << hex << status << " E: " << hex << dwErr;
}
cbHashObject = 0, cbData = 0;
if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, (PBYTE)&cbHashObject, sizeof(DWORD), &cbData, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR6 " << hex << status << " E: " << hex << dwErr;
}
pbHash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbHashObject);
if (!BCRYPT_SUCCESS(status = BCryptFinishHash(hHashB, pbHash, cbHashObject, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR7 " << hex << status << " E: " << hex << dwErr;
}
char* str_hash = new char[cbHashObject * 2 + 1];
for (int i = 0; i < cbHashObject; i++)
sprintf(str_hash + 2 * i, "%02X", pbHash[i]);
cout << "PASSWORD SHA512 = " << str_hash << endl << endl;
ZeroMemory(str_hash, cbHashObject);
if (!BCRYPT_SUCCESS(status = BCryptDestroyHash(hHashB)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR8 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptCloseAlgorithmProvider(hAlg, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR9 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, NULL, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR10 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbKeyData, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR11 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR12 " << hex << status << " E: " << hex << dwErr;
}
pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject);
if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_BLOCK_LENGTH, (PBYTE)&cbBlockLen, sizeof(DWORD), &cbData, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR13 " << hex << status << " E: " << hex << dwErr;
}
pbIV = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbBlockLen + 1);
for (int i(0); i < cbBlockLen; i++)
{
//pbIV[i]=rand();
pbIV[i] = 0xAA;
}
for (int i = 0; i < cbBlockLen; i++)
sprintf(str_hash + 2 * i, "%02X", pbIV[i]);
std::cout << "nnIV = " << str_hash;
if (!BCRYPT_SUCCESS(status = BCryptGenerateSymmetricKey(hAlg, &hKey, pbKeyObject, cbKeyObject, pbHash, cbHashObject, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR14 " << hex << status << " E: " << hex << dwErr;
}
if (!BCRYPT_SUCCESS(status = BCryptExportKey(hKey, NULL, BCRYPT_OPAQUE_KEY_BLOB, NULL, 0, &cbBlob, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR15 " << hex << status << " E: " << hex << dwErr;
}
pbBlob = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbBlob);
if (!BCRYPT_SUCCESS(status = BCryptExportKey(hKey, NULL, BCRYPT_OPAQUE_KEY_BLOB, pbBlob, cbBlob, &cbBlob, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR16 " << hex << status << " E: " << hex << dwErr;
}
ifstream f1("C:\LOL\1.txt");
ofstream f2("C:\LOL\2.txt", ios_base::trunc);
pbPlainData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, blockSize + 1);
ZeroMemory(pbPlainData, blockSize + 1);
f1.read((char*)pbPlainData, blockSize);
if (!BCRYPT_SUCCESS(status = BCryptEncrypt(hKey, (BYTE*)pbPlainData, blockSize, NULL, 0, NULL, NULL, 0, &cbCipherData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR15 " << hex << status << " E: " << hex << dwErr;
}
pbCipherData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbCipherData + 1);
ZeroMemory(pbCipherData, cbCipherData + 1);
do {
if (!BCRYPT_SUCCESS(status = BCryptEncrypt(hKey, (BYTE*)pbPlainData, blockSize, NULL, 0, NULL, (BYTE*)pbCipherData, cbCipherData, &cbData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR17 " << hex << status << " E: " << dwErr;
}
f2.write((char*)pbCipherData, cbData);
f2.flush();
f1.read((char*)pbPlainData, blockSize);
} while (!f1.eof());
f1.close();
f2.close();
BCryptDestroyKey(hKey);
HeapFree(GetProcessHeap(), 0, pbPlainData);
HeapFree(GetProcessHeap(), 0, pbCipherData);
pbPlainData = NULL;
pbCipherData = NULL;
memset(pbIV, 0xAA, cbBlockLen);
memset(pbKeyObject, 0, cbKeyObject);

if (!BCRYPT_SUCCESS(status = BCryptImportKey(hAlg, NULL, BCRYPT_OPAQUE_KEY_BLOB, &hKey, pbKeyObject, cbKeyObject, pbBlob, cbBlob, 0)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR18 " << hex << status << " E: " << hex << dwErr;
}
ifstream f3("C:\LOL\2.txt");
ofstream f4("C:\LOL\3.txt", ios_base::trunc);
pbCipherData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, blockSize + 1);
ZeroMemory(pbCipherData, blockSize + 1);
f3.read((char*)pbCipherData, blockSize);
if (!BCRYPT_SUCCESS(status = BCryptDecrypt(hKey, (BYTE*)pbCipherData, blockSize, NULL, NULL, 0, NULL, 0, &cbPlainData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR19 " << hex << status << " E: " << hex << dwErr;
}
pbPlainData = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbPlainData);
ZeroMemory(pbPlainData, cbPlainData);
do {
if (!BCRYPT_SUCCESS(status = BCryptDecrypt(hKey, (BYTE*)pbCipherData, blockSize, NULL, NULL, 0, (BYTE*)pbPlainData, cbPlainData, &cbData, BCRYPT_BLOCK_PADDING)))
{
DWORD dwErr = GetLastError();
std::cout << "ERROR20 " << hex << status << " E: " << dwErr;
}
f4.write((char*)pbPlainData, cbData);
f4.flush();
f3.read((char*)pbCipherData, blockSize);
} while (!f3.eof());
f3.close();
f4.close();
return 0;
}

我现在也在研究它。我可以加密文件,但无法解密它!

这是代码:

#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <bcrypt.h>
#pragma comment(lib, "bcrypt.lib")
#define NT_SUCCESS(Status)          (((NTSTATUS)(Status)) >= 0)
#define STATUS_UNSUCCESSFUL         ((NTSTATUS)0xC0000001L)
#define DATA_TO_ENCRYPT  "Test Data"
static const BYTE rgbAES128Key[] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
int main() {
BCRYPT_ALG_HANDLE       hAesAlg = NULL;
BCRYPT_KEY_HANDLE       hKey = NULL;
NTSTATUS                status = STATUS_UNSUCCESSFUL;
DWORD                   cbCipherText = 0,
cbPlainText = 0,
cbData = 0,
cbKeyObject = 0,
cbBlockLen = 0,
cbBlob = 0;
PBYTE                   pbCipherText = NULL,
pbPlainText = NULL,
pbKeyObject = NULL,
pbIV = NULL,
pbBlob = NULL;
BOOL                    bResult = FALSE;
wchar_t default_key[] = L"3igcZhRdWq96m3GUmTAiv9";
wchar_t* key_str = default_key;
// Open an algorithm handle.
if (!NT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAesAlg, BCRYPT_AES_ALGORITHM, NULL, 0)))
{
wprintf(L"**** Error 0x%x returned by BCryptOpenAlgorithmProvidern", status);
return 0;
}
// Calculate the size of the buffer to hold the KeyObject.
if (!NT_SUCCESS(status = BCryptGetProperty(hAesAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbData, 0)))
{
wprintf(L"**** Error 0x%x returned by BCryptGetPropertyn", status);
return 0;
}
// Allocate the key object on the heap.
pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject);
if (NULL == pbKeyObject)
{
wprintf(L"**** memory allocation failedn");
return 0;
}
// Generate the key from supplied input key bytes.
if (!NT_SUCCESS(status = BCryptGenerateSymmetricKey(hAesAlg, &hKey, pbKeyObject, cbKeyObject, (PBYTE)rgbAES128Key, sizeof(rgbAES128Key), 0)))
{
wprintf(L"**** Error 0x%x returned by BCryptGenerateSymmetricKeyn", status);
return 0;
}
// Save another copy of the key for later.
if (!NT_SUCCESS(status = BCryptExportKey(hKey, NULL, BCRYPT_OPAQUE_KEY_BLOB, NULL, 0, &cbBlob, 0)))
{
wprintf(L"**** Error 0x%x returned by BCryptExportKeyn", status);
return 0;
}
// Allocate the buffer to hold the BLOB.
pbBlob = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbBlob);
if (NULL == pbBlob)
{
wprintf(L"**** memory allocation failedn");
return 0;
}
if (!NT_SUCCESS(status = BCryptExportKey(
hKey,
NULL,
BCRYPT_OPAQUE_KEY_BLOB,
pbBlob,
cbBlob,
&cbBlob,
0)))
{
wprintf(L"**** Error 0x%x returned by BCryptExportKeyn", status);
return 0;
}


HANDLE hInpFile = CreateFileA("C:\LOL\file_1.encrypted", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hInpFile == INVALID_HANDLE_VALUE) {
printf("Cannot open input file!n");
system("pause");
return 0;
}
HANDLE hOutFile = CreateFileA("C:\LOL\file_1_decrypted.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hOutFile == INVALID_HANDLE_VALUE) {
printf("Cannot open output file!n");
system("pause");
return 0;
}
const size_t chunk_size = 48;
BYTE chunk[chunk_size] = { 0 };
DWORD out_len = 0;
BOOL isFinal = FALSE;
DWORD readTotalSize = 0;
DWORD inputSize = GetFileSize(hInpFile, NULL);
while (bResult = ReadFile(hInpFile, chunk, chunk_size, &out_len, NULL)) {
if (0 == out_len) {
break;
}
readTotalSize += out_len;
if (readTotalSize == inputSize) {
isFinal = TRUE;
printf("Final chunk set.n");
}
//
// Get the output buffer size.
//
if (!NT_SUCCESS(status = BCryptDecrypt(hKey, chunk, chunk_size, NULL, NULL, NULL, NULL, 0, &cbCipherText, BCRYPT_BLOCK_PADDING)))
{
wprintf(L"**** 1 Error 0x%x returned by BCryptEncryptn", status);
return 0;
}
pbCipherText = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbCipherText);
if (NULL == pbCipherText)
{
wprintf(L"**** memory allocation failedn");
return 0;
}
// Use the key to encrypt the plaintext buffer.
// For block sized messages, block padding will add an extra block.
if (!NT_SUCCESS(status = BCryptDecrypt(hKey, chunk, chunk_size, NULL, NULL, NULL, pbCipherText, cbCipherText, &cbCipherText, BCRYPT_BLOCK_PADDING)))
{
wprintf(L"**** 2 Error 0x%x returned by BCryptEncryptn", status);
return 0;
}
if (!WriteFile(hOutFile, pbCipherText, cbCipherText, &cbData, NULL)) {
printf("writing failed!n");
break;
}
}
}

主要变化是函数名称,从BCryptEncryptBCryptDerypt。 此外,文件名。

当我更改为解密时,我收到">

**

** 2 错误"错误...

这很奇怪,因为我使用相同的方法,相同的密钥......