views:

30

answers:

0

I've created this program that can encrypt and decrypt a found file. I've got the encryption part working fine, and its seems to me that the encryption key is exported as a tmp file. The problem is when I'm importing the session key for decryption that is where the CryptImportKey fails and so does CryptDecrypt fails too. Can someone please help me, I'll post the code below.

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);
   BYTE readkey[256];
   wchar_t current_username_buffer[256];
   DWORD size2 = 32767;
   wchar_t dest2[256] = L"C:\\Users\\";
   GetUserName(current_username_buffer, &size2);
   wcscat_s(dest2, current_username_buffer);
   wchar_t src[256] = L"\\Desktop\\";
   wcscat_s(dest2, src);
   wchar_t filepath2[] = L"enckeytemp.tmp";
   wcscat_s(dest2, filepath2);
   file4 = _wfopen(dest2, L"r");
   size_t writesize2 = fread(readkey, sizeof(char), sizeof(readkey), file4);
   BOOL rvalue1 = CryptImportKey(hCryptProv, readkey, sizeof(readkey), 0, 0, &hkey);
   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);
   BYTE key[256];
   DWORD len = 256;
   BOOL returnval = CryptExportKey(hkey, hXchgKey, SIMPLEBLOB, NULL, key, &len);
   wchar_t current_username_buffer[256];
   DWORD size2 = 32767;
   wchar_t dest2[256] = L"C:\\Users\\";
   GetUserName(current_username_buffer, &size2);
   wcscat_s(dest2, current_username_buffer);
   wchar_t src[256] = L"\\Desktop\\";
   wcscat_s(dest2, src);
   wchar_t filepath2[] = L"enckeytemp.tmp";
   wcscat_s(dest2, filepath2);
   file3 = _wfopen(dest2, L"w");
   size_t writesize2 = fwrite(key, sizeof(char), sizeof(key), file3);
   DWORD datalength = info4.st_size;
   BOOL rvalue3 = CryptEncrypt(hkey, NULL, FALSE, NULL, buffer, &datalength, datalength);
   CryptGenKey( hCryptProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hXchgKey);
   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);
   }
  }