⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processing.cpp

📁 文件加密解密源代码
💻 CPP
字号:
#include "stdafx.h"
#include "Processing.h"
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include "../Enc_Blowfish/Blowfish.h"
#include "Security/FileWipe.h"
#include "Files/SelfExtract.h"

Processing::Processing(enum CIPHER cipher, const char *source, const char *dest, const char *pwd)
	:	_cipher(cipher)
{
	if ((!pwd) || (!dest) || (!source)) throw "Invalid processing constructor params.";

	strncpy(_password, pwd, 100);
	if (cipher == DECRYPT_METHOD)
	{
		strcpy(_readFile, dest);
		strcpy(_writeFile, source);
	}
	else // both encrypt & self-extracted
	{
		strcpy(_readFile, source);
		strcpy(_writeFile, dest);
	}
}

Processing::~Processing()
{
}

LRESULT Processing::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	SendDlgItemMessage(ID_CLOSE_BUTTON, WM_ENABLE, FALSE, 0);

	start("FileProcessingThread");
	
	return 1;  // Let the system set the focus
}

LRESULT Processing::OnCloseDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// if backbone thread exists - closing it.
	if (getHandle()) 
		terminateAndWait();

	EndDialog(0);

	return 0;
}

LRESULT Processing::OnIMError(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if (wParam)
	{
		MessageBox((LPCTSTR)wParam, "Error", MB_ICONERROR);
		return 0;
	}
	return 1;
}

LRESULT Processing::OnBnClickedCloseButton(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// if backbone thread exists - closing it.
	if (getHandle()) 
		terminateAndWait();

	EndDialog(0);

	return 0;
}

char *Processing::generateTempFile()
{
	char *tempFile = new char[MAX_PATH];
	
	GetEnvironmentVariable("TEMP", tempFile, MAX_PATH);
	
	if (tempFile[strlen(tempFile)-1] != '\\') strcat(tempFile, "\\");
	strcat(tempFile, "fileenc.tmp");
	
	return tempFile;
}

int Processing::run()
{
	if (_cipher == DECRYPT_METHOD)
	{
		SetWindowText("Decrypting File, Please Wait...");
	}
	else
	{
		SetWindowText("Encrypting File, Please Wait...");
	}

	FILE *readFile = fopen(_readFile, "rb");
	if (readFile == 0) return threadError("Unable to open source file (fopen).");

	const size_t bufferSize = 1024;
	int barSize = 0;

	// reaching the end of the file and getting position = getting file size (bytes).
	fseek(readFile, 0, SEEK_END);
	barSize = ftell(readFile);
	fseek(readFile, 0, SEEK_SET);
	if (barSize == -1) return threadError("Unable to get file size (_filelength).");
	barSize = barSize / bufferSize;

	// initialize ProgressBar with the size of source file (k) and step size to 1.
	SendDlgItemMessage(ID_PROGRESSBAR, PBM_SETRANGE, 0, MAKELPARAM(0, barSize+1));
	SendDlgItemMessage(ID_PROGRESSBAR, PBM_SETSTEP, 1, 0);

	char outfile[MAX_PATH];
	if (_cipher == ENCRYPT_EXE)
	{
		char *temp = generateTempFile();
		strcpy(outfile, temp);
		delete temp;
	}
	else
	{
		strcpy(outfile, _writeFile);
	}

	FILE *writeFile = fopen(outfile, "wb");
	if (writeFile == 0) return threadError("Unable to open destination file.");

	char readBuffer[bufferSize];
	char outBuffer[bufferSize];
	size_t readRet = 0;

	BlowFishEnc encryption(_password);

	bool abort = false;
	int encRet;
	while (!feof(readFile))
	{
		readRet = fread(readBuffer, sizeof(char), bufferSize, readFile);

		if ((_cipher == DECRYPT_METHOD) && (!abort))
		{
			encRet = encryption.decryptStream(readBuffer, (DWORD)readRet, outBuffer);
			if feof(readFile)
			{
				int pos = 0;
				// removing trailing zeros - encrypted file must be x8 bytes.
				while ((pos < 8) && ((outBuffer + encRet - pos)[0] == 0)) pos++;
				// if found trailing zeros - decreasing the writing buffer marker (not writing them).
				if (pos) encRet -= (pos - 1);
			}
		}
		else if (!abort)
		{
			encRet = encryption.encryptStream(readBuffer, (DWORD)readRet, outBuffer);
		}

		fwrite(outBuffer, sizeof(char), encRet, writeFile);

		stepProgressbar();

		// check if user aborted!
		if ((shouldTerminate()) || (abort))
		{
			fclose(readFile); fclose(writeFile);
			GenLib::FileWipe::wipeFile(outfile);
			return 0;
		}
	}

	fflush(writeFile);

	fclose(writeFile);
	fclose(readFile);

	ZeroMemory(outBuffer, bufferSize);
	ZeroMemory(readBuffer, bufferSize);

	// if encrypted should be created as self extracted.
	if (_cipher == ENCRYPT_EXE)
	{
		GenLib::SelfExtract attacher;
		attacher.detachFile(_writeFile);
	
		GenLib::SelfExtract builder(_writeFile);
		builder.attachFile(outfile, _readFile);
	}

	SendDlgItemMessage(ID_PROGRESSBAR, PBM_SETPOS, barSize+1, 0);

	::ShowWindow(::GetDlgItem(m_hWnd, ID_FINISHNOTE), SW_SHOWNORMAL);
	::ShowWindow(::GetDlgItem(m_hWnd, ID_PROGRESSBAR), SW_HIDE);
	if (_cipher == DECRYPT_METHOD)
	{
		::EnableWindow(::GetDlgItem(m_hWnd, ID_OPENFILE_BUTTON), TRUE);
		SetDlgItemText(ID_FINISHNOTE, "Decryption Completed. Click 'Open File' to view file content.");
	}
	else
	{
		SetDlgItemText(ID_FINISHNOTE, "Encryption Completed.");
	}

	SetDlgItemText(ID_CLOSE_BUTTON, "&Close");

	return 0;
}

int Processing::threadError(const char *error)
{
	SendMessage(IM_ERROR, (WPARAM)"Error reading source file!", 0);
	EndDialog(0);
	return 0;
}
LRESULT Processing::OnBnClickedOpenfileButton(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	char openfileText[100];
	GetDlgItemText(ID_OPENFILE_BUTTON, openfileText, 100);
	if (strcmp(openfileText, "&Open File") == 0)
	{
		ShellExecute(m_hWnd, "open", _writeFile, NULL, NULL, SW_SHOWNORMAL);
		SetDlgItemText(ID_OPENFILE_BUTTON, "&Wipe File");
	}
	else
	{
		if (MessageBox("Are you sure you want to open the decrypted file?", "File Wipe", MB_YESNO | MB_ICONQUESTION) == IDYES)
		{
			if (GenLib::FileWipe::wipeFile(_writeFile))
			{
				MessageBox("File Wiped Successfully");	
				::EnableWindow(::GetDlgItem(m_hWnd, ID_OPENFILE_BUTTON), FALSE);
			}
			else
				MessageBox("File Wipe failed!");
		}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -