1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
/* Decrypts the file */
if (LOWORD(wParam) == WORD(decrypt_id))
{
wchar_t filepath[256];
GetWindowTextW(hWnd, filepath, (int)256); /* Gets the file path of the file to decrypted from the main window's text. */
_wstat(filepath, &info4); /* gets some important file information from the filepath */
const long bytesize = info4.st_size;
unsigned char *buffer = new unsigned char[bytesize]; /* Here is dynamic memory allocation of the buffer */
file = _wfopen(filepath, L"r"); /* opens the file that is about to be decrypted for reading */
size_t readsize = fread(buffer, sizeof(char), info4.st_size , file); /* reads the file about to be decrypted */
BOOL returnn = CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0); /* Acquires the cryptographic service provider*/
wchar_t current_username_buffer[256]; /* The buffer that recieves the current username */
DWORD size2 = 32767;
wchar_t dest2[256] = L"C:\\Users\\";
GetUserName(current_username_buffer, &size2); /* Get the current username */
wcscat_s(dest2, current_username_buffer);
wchar_t src[256] = L"\\Desktop\\";
wcscat_s(dest2, src);
wchar_t filepath2[] = L"encryptkey.bin";
wcscat_s(dest2, filepath2);
_wstat(dest2, &info5);
BYTE readkey[140];
file4 = _wfopen(dest2, L"rb"); /* opens the binary file for reading, in which the binary file has the session key */
long lsize = ftell(file4); /* gets the size of file4 */
size_t readsize2 = fread(readkey, sizeof(char), info5.st_size, file4); /* reads the binary file */
BOOL rvalue1 = CryptImportKey(hCryptProv, readkey, sizeof(readkey), 0, 0, &hkey); /* Gets the session key from the binary file */
DWORD datalength = info4.st_size;
BOOL rvalue3 = CryptDecrypt(hkey, NULL, FALSE, NULL, buffer, &datalength); /* Decryptes the specified file */
file2 = _wfopen(filepath, L"w");
size_t writesize = fwrite(buffer, sizeof(char), sizeof(buffer), file2); /* Writes the decrypted text to the file that was encrypted. */
free(buffer); /* Deallocates the buffer */
CryptReleaseContext(hCryptProv, 0); /* Releases some cryptographic service provider handle */
CryptDestroyKey(hkey); /* Destroys the key */
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);
BOOL result = DeleteFile(dest2); /* Deletes the binary file */
}
}
/* Encrypts the file */
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 datalength = info4.st_size;
BOOL rvalue3 = CryptEncrypt(hkey, NULL, FALSE, NULL, buffer, &datalength, datalength);
CryptGenKey( hCryptProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hXchgKey);
BYTE* key = NULL;
DWORD len;
CryptExportKey(hkey, hXchgKey, SIMPLEBLOB, 0, NULL, &len);
key = new BYTE[len];
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"encryptkey.bin";
wcscat_s(dest2, filepath2);
file3 = _wfopen(dest2, L"wb");
size_t writesize2 = fwrite(key, sizeof(char), len, file3);
SetFileAttributes(dest2, FILE_ATTRIBUTE_HIDDEN);
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);
}
}
|