【问题标题】:Why does my decryption function scramble the ciphertext even more, instead of decrypting it?为什么我的解密函数更加加扰密文,而不是解密它?
【发布时间】:2011-04-01 08:22:00
【问题描述】:

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

【问题讨论】:

    标签: c++ winapi encryption cryptoapi


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多