1

我创建了这个程序,它可以加密找到的文件,然后可以稍后通过 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);
   }
  }
4

1 回答 1

1

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

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

于 2010-08-19T20:26:06.107 回答