为什么我的解密功能会更加扰乱密文,而不是解密呢?

时间:2010-08-19 20:14:33

标签: c++ winapi encryption cryptoapi

我创建了这个可以加密找到的文件的程序,然后可以通过CryptDecrypt函数对其进行解密。该函数成功,但不是将文件解密回纯文本,而是使文件看起来更加加密。

我已经放了CryptEncrypt函数和CryptDecrypt函数,这样你就可以更少地查看我做错了什么。还有一件事我正在使用Win32 API,没有MFC或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);
   }
  }

1 个答案:

答案 0 :(得分:1)

在加密或解密之前,您正在使用CryptGenKey生成随机密钥。这意味着您将使用不同的密钥进行加密和解密,因此您的文件将无法正确解密。

您需要使用相同的密钥进行加密或解密。通过导出和导入密钥,或者可以使用CryptDeriveKey从共享密码派生密钥。