J'ai créé ce programme qui peut crypter un fichier trouvé, puis il peut être décrypté plus tard via la fonction CryptDecrypt. La fonction réussit mais au lieu de décrypter le fichier en texte brut, le fichier semble encore plus crypté.Pourquoi ma fonction de décryptage brouille-t-elle encore plus le texte chiffré au lieu de le décrypter?
J'ai mis à la fois la fonction CryptEncrypt et la fonction CryptDecrypt afin que vous puissiez avoir une vision plus faible de ce que je fais de mal. Encore une chose que j'utilise l'API Win32, pas de MFC ou ATL.
if (LOWORD(wParam) == WORD(decrypt_id))
{
wchar_t filepath[256];
GetWindowTextW(hWnd, filepath, (int)256);
_wstat(filepath, &info4);
const long bytesize = info4.st_size;
unsigned char *buffer = new unsigned char[bytesize];
file = _wfopen(filepath, L"r");
size_t readsize = fread(buffer, sizeof(char), info4.st_size , file);
BOOL returnn = CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0);
BOOL rvalue1 = CryptGenKey(hCryptProv, CALG_RC4, KEYLENGTH | CRYPT_EXPORTABLE, &hkey);
DWORD val = GetLastError();
DWORD datalength = info4.st_size;
BOOL rvalue3 = CryptDecrypt(hkey, NULL, FALSE, NULL, buffer, &datalength);
file2 = _wfopen(filepath, L"w");
size_t writesize = fwrite(buffer, sizeof(char), sizeof(buffer), file2);
free(buffer);
CryptReleaseContext(hCryptProv, 0);
CryptDestroyKey(hkey);
if (rvalue3 == 0)
{
DWORD result = GetLastError();
wchar_t dest[256] = L"Decryptor Failed To Decrypt File!";
wcscat_s(dest, L"\n");
wcscat_s(dest, L"Error Code: ");
wchar_t code[256];
swprintf_s(code, L"%d", result);
wcscat_s(dest, code);
wcscat_s(dest, L"\n");
wcscat_s(dest, L"Error Code Information at: http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx");
MessageBoxW(hWnd, dest, L"Error", MB_ICONERROR | MB_OK);
ShowWindow(encrypt_button, SW_HIDE);
}
else
{
MessageBox(hWnd, L"Successfully Decrypted The File!", L"", MB_OK | MB_ICONINFORMATION);
ShowWindow(encrypt_button, SW_HIDE);
}
}
if (LOWORD(wParam) == WORD(encrypt_id))
{
wchar_t filepath[256];
GetWindowTextW(hWnd, filepath, (int)256);
_wstat(filepath, &info4);
const long bytesize = info4.st_size;
unsigned char *buffer = new unsigned char[bytesize];
file = _wfopen(filepath, L"r");
size_t readsize = fread(buffer, sizeof(char), info4.st_size , file);
BOOL returnn = CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0);
BOOL rvalue1 = CryptGenKey(hCryptProv, CALG_RC4, KEYLENGTH | CRYPT_EXPORTABLE, &hkey);
DWORD val = GetLastError();
DWORD datalength = info4.st_size;
BOOL rvalue3 = CryptEncrypt(hkey, NULL, FALSE, NULL, buffer, &datalength, datalength);
file2 = _wfopen(filepath, L"w");
size_t writesize = fwrite(buffer, sizeof(char), sizeof(buffer), file2);
free(buffer);
CryptDestroyKey(hkey);
CryptReleaseContext(hCryptProv, 0);
if (rvalue3 == 0)
{
DWORD result = GetLastError();
wchar_t dest[256] = L"Encryptor Failed To Encrypt File!";
wcscat_s(dest, L"\n");
wcscat_s(dest, L"Error Code: ");
wchar_t code[256];
swprintf_s(code, L"%d", result);
wcscat_s(dest, code);
wcscat_s(dest, L"\n");
wcscat_s(dest, L"Error Code Information at: http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx");
MessageBoxW(hWnd, dest, L"Error", MB_ICONERROR | MB_OK);
ShowWindow(encrypt_button, SW_HIDE);
}
else
{
MessageBox(hWnd, L"Successfully Encrypted The File!", L"", MB_OK | MB_ICONINFORMATION);
ShowWindow(encrypt_button, SW_HIDE);
}
}