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

📄 maindialog.cpp

📁 C++写的加密程序,可对文件进行加密.
💻 CPP
字号:
/*
	Application Main Dialog Implementation.
*/
#include "stdafx.h"
#include "MainDialog.h"
#include "Files/GenFiles.h"
#include "Security/FileWipe.h"
#include <stdio.h>
#include "Processing.h"
#include "ConfigDialog.h"
#include "Files/SelfExtract.h"
#include <Shlwapi.h>

MainDialog::MainDialog()
{
}

MainDialog::~MainDialog()
{
	char garbage[100];
	FillMemory(garbage, 100, 'A');
	garbage[110] = 0;
	
	SetDlgItemText(ID_PASSWORD, garbage);
	SetDlgItemText(ID_PASSVERIFY, garbage);

	DestroyWindow();
	CoUninitialize();
}

LRESULT MainDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	GenLib::SelfExtract selfCheck;

	if (!selfCheck.checkSignature()) 
	{
		MessageBox("Application was NOT processed by the builder utility!", "Error", MB_ICONERROR);
		PostQuitMessage(0);
	}

	// adding auto-complete feature when typing filename
	// (_ATL_APARTMENT_THREADED must be declared in stdafx - CoInitializeEx auto. called).
	SHAutoComplete(GetDlgItem(ID_SOURCEFILE), SHACF_FILESYSTEM);
	SHAutoComplete(GetDlgItem(ID_DESTFILE), SHACF_FILESYSTEM);

	return 1;  // Let the system set the focus
}

LRESULT MainDialog::OnCloseDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PostQuitMessage(0);
	return 0;
}

LRESULT MainDialog::OnBnClickedSourcebrowseButton(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	char resultFile[MAX_PATH];

	GenLib::GenFiles::FILE_DIALOG_TYPE dialogType;

	GetDlgItemText(ID_SOURCEFILE, resultFile, MAX_PATH);

	if (IsDlgButtonChecked(ID_ENCRYPT_RADIO) == BST_CHECKED)
	{
		dialogType = GenLib::GenFiles::OPEN_FILE_DIALOG;
	}
	else
	{
		dialogType = GenLib::GenFiles::SAVE_FILE_DIALOG;
		if (strlen(resultFile) == 0)
		{
            GetDlgItemText(ID_DESTFILE, resultFile, MAX_PATH);
			if (strlen(resultFile) > 4) resultFile[strlen(resultFile) - 4] = 0;
		}
	}

	if (GenLib::GenFiles::fileDialog(dialogType, m_hWnd, "Chooose Source File", "All Files (*.*)\0*.*\0\0", resultFile, MAX_PATH))
		setDlgEncrypt(resultFile);

	return 0;
}

LRESULT MainDialog::OnBnClickedDestbrowseButton(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	char resultFile[MAX_PATH];

	GenLib::GenFiles::FILE_DIALOG_TYPE dialogType;
	
	GetDlgItemText(ID_DESTFILE, resultFile, MAX_PATH);
	if (IsDlgButtonChecked(ID_DECRYPT_RADIO) == BST_CHECKED)
	{
		dialogType = GenLib::GenFiles::OPEN_FILE_DIALOG;
	}
	else
	{
		dialogType = GenLib::GenFiles::SAVE_FILE_DIALOG;
		if (strlen(resultFile) == 0)
		{
            GetDlgItemText(ID_DESTFILE, resultFile, MAX_PATH);

			if (strlen(resultFile) == 0) strcpy(resultFile, "*");

			strcat(resultFile, ".enc");
		}
	}

	if (GenLib::GenFiles::fileDialog(dialogType, m_hWnd, "Chooose Destination File", "Encrypted Files (*.enc)\0*.enc\0\0", resultFile, MAX_PATH))
		setDlgDecrypt(resultFile);
	return 0;
}

bool MainDialog::updateUI(const char *cmdLine)
{
	if (strlen(cmdLine) == 0)
	{
		CheckDlgButton(ID_ENCRYPT_RADIO, BST_CHECKED);
		return true;
	}

	char processFile[MAX_PATH];

	bool stripSides = false;
	if (cmdLine[0] == '"') stripSides = true;
    
	// Checking if parameter filename is surrended by ".
	if (stripSides)
	{
		size_t size = strlen(cmdLine)-2;
		strncpy(processFile, cmdLine+1, size);
		processFile[size] = 0;
	}
	else
	{
		strcpy(processFile, cmdLine);
	}

	// checking if parameter file exists.
	if (!PathFileExists(processFile))
	{
		char errorMsg[100];
		_snprintf(errorMsg, 100, "Command Line Error - %s does not exists!\0", processFile);
		MessageBox(errorMsg, "Error", MB_ICONERROR);
		return false;
	}

	// Updating UI according to parameter kind (source or destination).
	if (strstr(_strlwr(processFile), ".enc") == NULL)
	{
		CheckDlgButton(ID_ENCRYPT_RADIO, BST_CHECKED);
		CheckDlgButton(ID_DECRYPT_RADIO, BST_UNCHECKED);
		setDlgEncrypt(processFile);
	}
	else
	{
		CheckDlgButton(ID_DECRYPT_RADIO, BST_CHECKED);
		CheckDlgButton(ID_ENCRYPT_RADIO, BST_UNCHECKED);
        setDlgDecrypt(processFile);
	}

	return true;
}

LRESULT MainDialog::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	PostQuitMessage(0);
	return 0;
}

LRESULT MainDialog::OnFileSettings(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	ConfigDialog config;
	config.DoModal();
	return 0;
}

LRESULT MainDialog::OnHelpAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	MessageBox("File Encryption Utility v1.0 (c) Nir Dremer\n", "About", MB_ICONINFORMATION);
	return 0;
}

void MainDialog::setDlgEncrypt(const char *file)
{
	if (strlen(file) == 0) return;

	char tempName[MAX_PATH];

	strcpy(tempName, file);
	
	SetDlgItemText(ID_SOURCEFILE, tempName);
	if (IsDlgButtonChecked(ID_ENCRYPT_RADIO) == BST_CHECKED)
	{
		setAutoDest();
		::SetFocus(::GetDlgItem(m_hWnd, ID_PASSWORD));
	}
	else
	{
		::SetFocus(::GetDlgItem(m_hWnd, ID_SOURCEFILE));
	}

	::EnableWindow(::GetDlgItem(m_hWnd, ID_PASSVERIFY), TRUE);
	::EnableWindow(::GetDlgItem(m_hWnd, ID_ENCEXE), TRUE);
	::EnableWindow(::GetDlgItem(m_hWnd, ID_DELAFTER), TRUE);

	CheckDlgButton(ID_DELAFTER, BST_CHECKED);
}

void MainDialog::setDlgDecrypt(const char *file)
{
	if (strlen(file) == 0) return;
	char tempName[MAX_PATH];

	strcpy(tempName, file);
	
	SetDlgItemText(ID_DESTFILE, tempName);

	if (IsDlgButtonChecked(ID_DECRYPT_RADIO) == BST_CHECKED)
	{
		char tempSource[10];
		GetDlgItemText(ID_SOURCEFILE, tempSource, 10);
		if (strlen(tempSource) == 0)
		{
			tempName[strlen(tempName)-4] = 0;
			SetDlgItemText(ID_SOURCEFILE, tempName);
		}
		::SetFocus(::GetDlgItem(m_hWnd, ID_PASSWORD));
	}
	else
	{
		::SetFocus(::GetDlgItem(m_hWnd, ID_DESTFILE));
	}

	CheckDlgButton(ID_DELAFTER, BST_UNCHECKED);

	::EnableWindow(::GetDlgItem(m_hWnd, ID_DELAFTER), FALSE);
	::EnableWindow(::GetDlgItem(m_hWnd, ID_PASSVERIFY), FALSE);
	::EnableWindow(::GetDlgItem(m_hWnd, ID_ENCEXE), FALSE);
}

bool MainDialog::setAutoDest()
{
	char newDest[MAX_PATH];
	
	GetDlgItemText(ID_SOURCEFILE, newDest, MAX_PATH);

	if (strlen(newDest) == 0) return false;

	if (IsDlgButtonChecked(ID_ENCEXE) == BST_CHECKED)
	{
        strcat(newDest, " (Encrypted).exe");		
	}
	else
	{
		strcat(newDest, ".enc");
	}

	SetDlgItemText(ID_DESTFILE, newDest);
	return true;
}

LRESULT MainDialog::OnBnClickedEncryptButton(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	char source[MAX_PATH];
	GetDlgItemText(ID_SOURCEFILE, source, MAX_PATH);
	char dest[MAX_PATH];
	GetDlgItemText(ID_DESTFILE, dest, MAX_PATH);

	if ((IsDlgButtonChecked(ID_ENCRYPT_RADIO) == BST_CHECKED) && (PathFileExists(dest)))
	{
		if (MessageBox("Are you sure you want to overwrite encrypted file?", "File Overwrite", MB_YESNO | MB_ICONQUESTION) == IDNO)
		{
			::SetFocus(::GetDlgItem(m_hWnd, ID_DESTFILE)); 
			return 0;
		}
	}

	if ((IsDlgButtonChecked(ID_DECRYPT_RADIO) == BST_CHECKED) && (PathFileExists(source)))
	{
		if (MessageBox("Are you sure you want to overwrite source file?", "File Overwrite", MB_YESNO | MB_ICONQUESTION) == IDNO)
		{
			::SetFocus(::GetDlgItem(m_hWnd, ID_SOURCEFILE)); 
			return 0;
		}
	}

	char password[57];
	GetDlgItemText(ID_PASSWORD, password, 56);
	password[56]=0;

	if (strlen(password) < 5)
	{
		MessageBox("Password length should be at least 5 characters!", "Error");
		wipePwds(password, 0);
		return 0;
	}

	char passverify[57];
	GetDlgItemText(ID_PASSVERIFY, passverify, 56);
	passverify[56] = 0;

	if ((IsDlgButtonChecked(ID_ENCRYPT_RADIO) == BST_CHECKED) && (strcmp(password, passverify) != 0))
	{
        MessageBox("Two Passwords do NOT match!", "Error");
		wipePwds(password, passverify);
		return 0;
	}

	if (IsDlgButtonChecked(ID_ENCRYPT_RADIO) == BST_CHECKED)
	{
		if (!PathFileExists(source))
		{
			MessageBox("Requested file for encryption does NOT exist.", "Error");
			wipePwds(password, passverify);
			return 0;
		}
		enum CIPHER cipher = ENCRYPT_METHOD;
		if (IsDlgButtonChecked(ID_ENCEXE) == BST_CHECKED)
			cipher = ENCRYPT_EXE;
		
		Processing processing(cipher, source, dest, password);
		processing.DoModal();
		
		if (IsDlgButtonChecked(ID_DELAFTER) == BST_CHECKED)
		{
			if (!GenLib::FileWipe::wipeFile(source))
				MessageBox("CAUTION: File Wipe failed - source file still exists on disk!");
		}

		wipePwds(password, passverify);
		return 0;
	}
	else if (IsDlgButtonChecked(ID_DECRYPT_RADIO) == BST_CHECKED)
	{
		if (!PathFileExists(dest))
		{
			MessageBox("Requested file for decryption does NOT exist.", "Error");
			wipePwds(password, passverify);
			return 0;
		}
		Processing processing(DECRYPT_METHOD, source, dest, password);
		processing.DoModal();
		wipePwds(password, passverify);
		return 0;
	}
	else
	{
		MessageBox("Please select Method - Encrypt/Decrypt", "Error");
	}

	wipePwds(password, passverify);
	return 0;
}

LRESULT MainDialog::OnBnClickedEncryptRadio(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	::EnableWindow(::GetDlgItem(m_hWnd, ID_PASSVERIFY), TRUE);
	::EnableWindow(::GetDlgItem(m_hWnd, ID_ENCEXE), TRUE);

	::EnableWindow(::GetDlgItem(m_hWnd, ID_DELAFTER), TRUE);
	CheckDlgButton(ID_DELAFTER, BST_CHECKED);
	//::SetFocus(::GetDlgItem(m_hWnd, ID_PASSWORD));
	return 0;
}

LRESULT MainDialog::OnBnClickedDecryptRadio(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	::EnableWindow(::GetDlgItem(m_hWnd, ID_PASSVERIFY), FALSE);
	::EnableWindow(::GetDlgItem(m_hWnd, ID_ENCEXE), FALSE);

	::EnableWindow(::GetDlgItem(m_hWnd, ID_DELAFTER), FALSE);
	CheckDlgButton(ID_DELAFTER, BST_UNCHECKED);
	//::SetFocus(::GetDlgItem(m_hWnd, ID_PASSWORD));
	return 0;
}

void MainDialog::wipePwds(char *pwd, char *verify)
{
    if (pwd != 0)		ZeroMemory(pwd, 57);
	if (verify != 0)	ZeroMemory(verify, 57);
}

LRESULT MainDialog::OnBnClickedWipeButton(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	char filename[MAX_PATH];
	GetDlgItemText(ID_SOURCEFILE, filename, MAX_PATH);

	if (!PathFileExists(filename))
	{
		MessageBox("Source File does NOT exist!", "Error", MB_ICONERROR);
		return 0;
	}

	char wipeFile[MAX_PATH+100];
	_snprintf(wipeFile, 100, "Are you sure you want to wipe %s?", filename);
	if (MessageBox(wipeFile, "File Wipe", MB_YESNO | MB_ICONQUESTION) == IDYES)
	{
		if (GenLib::FileWipe::wipeFile(filename))
			MessageBox("File Wiped Successfully");	
		else
			MessageBox("File Wipe failed!");
	}
	
    return 0;
}

LRESULT MainDialog::OnSelfExtractClick(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	setAutoDest();
	return 0;
}

⌨️ 快捷键说明

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