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

📄 filecryptdlg.cpp

📁 一些加密算法的介绍,可对文件或字符串加密
💻 CPP
📖 第 1 页 / 共 4 页
字号:

// FileCryptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileCrypt.h"
#include "FileCryptDlg.h"
#include <shlwapi.h>
#include <string>
#include <memory>
#include <exception>

using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CString g_oStrError = _T("ERROR");
CString g_oStrSuccess = _T("SUCCESS");

//Error Beep Signal
void ErrorBeep()
{
	//MessageBeep(MB_OK); //asynchronous
	MessageBeep(0xFFFFFFFF); //asynchronous
}

//Optimized Function to convert an unsigned char to a Hex string of length 2
void Char2Hex(unsigned char ch, char* szHex)
{
	static unsigned char saucHex[] = "0123456789ABCDEF";
	szHex[0] = saucHex[ch >> 4];
	szHex[1] = saucHex[ch&0xF];
	szHex[2] = 0;
}

//Function to convert a Hex string of length 2 to an unsigned char
bool Hex2Char(char const* szHex, unsigned char& rch)
{
	if(*szHex >= '0' && *szHex <= '9')
		rch = *szHex - '0';
	else if(*szHex >= 'A' && *szHex <= 'F')
		rch = *szHex - 55; //-'A' + 10
	else
		//Is not really a Hex string
		return false; 
	szHex++;
	if(*szHex >= '0' && *szHex <= '9')
		(rch <<= 4) += *szHex - '0';
	else if(*szHex >= 'A' && *szHex <= 'F')
		(rch <<= 4) += *szHex - 55; //-'A' + 10;
	else
		//Is not really a Hex string
		return false;
	return true;
}

//Function to convert binary string to hex string
void Binary2Hex(unsigned char const* pucBinStr, int iBinSize, char* pszHexStr)
{
	int i;
	char szHex[3];
	unsigned char const* pucBinStr1 = pucBinStr;
	*pszHexStr = 0;
	for(i=0; i<iBinSize; i++,pucBinStr1++)
	{
		Char2Hex(*pucBinStr1, szHex);
		strcat(pszHexStr, szHex);
	}
}

//Function to convert hex string to binary string
bool Hex2Binary(char const* pszHexStr, unsigned char* pucBinStr, int iBinSize)
{
	int i;
	unsigned char ch;
	for(i=0; i<iBinSize; i++,pszHexStr+=2,pucBinStr++)
	{
		if(false == Hex2Char(pszHexStr, ch))
			return false;
		*pucBinStr = ch;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileCryptDlg dialog

CString CFileCryptDlg::sm_oStrDir;

CFileCryptDlg::CFileCryptDlg(CWnd* pParent /*=NULL*/): CDialog(CFileCryptDlg::IDD, pParent),
	m_iAction(ENCRYPT), m_iMode(FILE), m_iMethod(AES),
	m_oKeyFrg(RGB(0,0,190)), m_oKeyBg(RGB(255,200,200)), m_oKeyBg1(RGB(200,150,150)),
	m_oPlainFrg(RGB(190,0,0)), m_oPlainBg(RGB(200,255,200)), m_oPlainBg1(RGB(150,200,150)),
	m_oEncFrg(RGB(0,130,0)), m_oEncBg(RGB(200,200,255)), m_oEncBg1(RGB(150,150,200)),
	//KEY
	m_oEditKeyData(m_oKeyFrg, m_oKeyBg), m_oEditKeyDataHex(m_oKeyFrg, m_oKeyBg1),
	//PLAIN
	m_oEdtStr(m_oPlainFrg, m_oPlainBg), m_oEdtStrHex(m_oPlainFrg, m_oPlainBg1),
	//ENCRYPTED
	m_oEdtEnc(m_oEncFrg, m_oEncBg1), m_oEdtEncHex(m_oEncFrg, m_oEncBg),
	//DIRS
	m_oEdtDir1(m_oPlainFrg, m_oPlainBg1), m_oEdtDir3(m_oEncFrg, m_oEncBg1),
	//EXT and FILE
	m_oEdtExt1(m_oPlainFrg, m_oPlainBg), m_oEdtFile1(m_oPlainFrg, m_oPlainBg),
	m_oEdtExt3(m_oEncFrg, m_oEncBg), m_oEdtFile3(m_oEncFrg, m_oEncBg)
{
	//{{AFX_DATA_INIT(CFileCryptDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileCryptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileCryptDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
	//DDX_Control(pDX, IDC_EDTENCHEX, m_oEdtHex); //???
}

BEGIN_MESSAGE_MAP(CFileCryptDlg, CDialog)
	//{{AFX_MSG_MAP(CFileCryptDlg)
	ON_WM_CLOSE()
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_MESSAGE_VOID(WM_KICKIDLE, OnKickIdle)
	ON_UPDATE_COMMAND_UI(IDC_BTNDIR1, OnUpdateBtnDir1)
	ON_UPDATE_COMMAND_UI(IDC_BTNDIR3, OnUpdateBtnDir3)
	ON_UPDATE_COMMAND_UI(IDC_BTNFILE1, OnUpdateBtnExtInput)
	ON_UPDATE_COMMAND_UI(IDC_BTNFILE3, OnUpdateBtnExtOutput)
	ON_UPDATE_COMMAND_UI(IDC_BTNACTION, OnUpdateBtnAction)
	ON_UPDATE_COMMAND_UI(IDC_COMBOKEYSIZE, OnUpdateComboKeysize)
	ON_UPDATE_COMMAND_UI(IDC_COMBOBLOCKSIZE, OnUpdateComboBlocksize)
	ON_UPDATE_COMMAND_UI(IDC_EDITBLOCKSIZE, OnUpdateEditBlockSize)
	ON_UPDATE_COMMAND_UI(IDC_COMBOOPMODE, OnUpdateComboOpMode)
	ON_UPDATE_COMMAND_UI(IDC_COMBOPADDING, OnUpdateComboPadding)
	ON_UPDATE_COMMAND_UI(IDC_EDITEXTROUNDS, OnUpdateEditExtRounds)
	ON_UPDATE_COMMAND_UI(IDC_EDITINTROUNDS, OnUpdateEditIntRounds)
	ON_BN_CLICKED(IDC_RADENCRYPT, OnRadencrypt)
	ON_BN_CLICKED(IDC_RADDECRYPT, OnRaddecrypt)
	ON_BN_CLICKED(IDC_BTNACTION, OnBtnaction)
	ON_BN_CLICKED(IDC_BTNFILE1, OnBtnfile1)
	ON_BN_CLICKED(IDC_BTNFILE3, OnBtnfile3)
	ON_CBN_SELCHANGE(IDC_COMBOMETHODS, OnSelchangeCombomethods)
	ON_BN_CLICKED(IDC_RADFILE, OnRadfile)
	ON_BN_CLICKED(IDC_RADSTRING, OnRadstring)
	ON_BN_CLICKED(IDC_BTNDIR1, OnBtndir1)
	ON_BN_CLICKED(IDC_BTNDIR3, OnBtndir3)
	ON_COMMAND(IDM_EXIT, OnExit)
	ON_COMMAND(IDM_HELP, OnHelp)
	ON_EN_CHANGE(IDC_EDTENCHEX, OnChangeEdthex)
	ON_BN_CLICKED(IDC_RADKEY, OnRadkey)
	ON_BN_CLICKED(IDC_RADPLAIN, OnRadplain)
	ON_BN_CLICKED(IDC_RADKEYHEX, OnRadkeyhex)
	ON_BN_CLICKED(IDC_RADPLAINHEX, OnRadplainhex)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileCryptDlg message handlers

void CFileCryptDlg::OnKickIdle()
{
    //Call this member function to update the state of dialog buttons and other controls
    //in a dialog box or window that uses the ON_UPDATE_COMMAND_UI callback mechanism
	UpdateDialogControls( this, FALSE );
}

//Enable the button only when the extension Edit Box is not empty
void CFileCryptDlg::OnUpdateBtnDir1(CCmdUI* pCmdUI)
{
	CButton* poButton = reinterpret_cast<CButton*>(GetDlgItem(IDC_RADFILE));
	pCmdUI->Enable(1==poButton->GetCheck());
}

//Enable the button only when the extension Edit Box is not empty
void CFileCryptDlg::OnUpdateBtnDir3(CCmdUI* pCmdUI)
{
	CButton* poButton = reinterpret_cast<CButton*>(GetDlgItem(IDC_RADFILE));
	pCmdUI->Enable(1==poButton->GetCheck());
}

//Enable the button only when the extension Edit Box is not empty
void CFileCryptDlg::OnUpdateBtnExtInput(CCmdUI* pCmdUI)
{
	CButton* poButton = reinterpret_cast<CButton*>(GetDlgItem(IDC_RADFILE));
	CEdit* poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT1));
	pCmdUI->Enable((poEdit->GetWindowTextLength()>0) && (1==poButton->GetCheck()));
}

//Enable the button only when the extension Edit Box is not empty
void CFileCryptDlg::OnUpdateBtnExtOutput(CCmdUI* pCmdUI)
{
	CButton* poButton = reinterpret_cast<CButton*>(GetDlgItem(IDC_RADFILE));
	CEdit* poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT3));
	pCmdUI->Enable((poEdit->GetWindowTextLength()>0) && (1==poButton->GetCheck()));
}

//Enable the Action button
void CFileCryptDlg::OnUpdateBtnAction(CCmdUI* pCmdUI)
{
	CButton* poBtnFile = reinterpret_cast<CButton*>(GetDlgItem(IDC_RADFILE));
	CButton* poBtnPlain = reinterpret_cast<CButton*>(GetDlgItem(IDC_RADPLAIN));
	CEdit* poEdit1;
	CEdit* poEdit2;
	CEdit* poEdit3;
	CEdit* poEdit5;
	if(ENCRYPT == m_iAction)
	{
		poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE1));
		poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE3));
		poEdit3 = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT3));
		if(1==poBtnPlain->GetCheck())
			poEdit5 = static_cast<CEdit*>(GetDlgItem(IDC_EDTSTR));
		else
			poEdit5 = static_cast<CEdit*>(GetDlgItem(IDC_EDTSTRHEX));
	}
	else //DECRYPT == m_iAction
	{
		poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE3));
		poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE1));
		poEdit3 = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT1));
		poEdit5 = static_cast<CEdit*>(GetDlgItem(IDC_EDTENCHEX));
	}
	CEdit* poEdit4;
	CButton* poBtnKey = reinterpret_cast<CButton*>(GetDlgItem(IDC_RADKEY));
	if(1==poBtnKey->GetCheck())
		poEdit4 = static_cast<CEdit*>(GetDlgItem(IDC_EDITKEYDATA));
	else
		poEdit4 = static_cast<CEdit*>(GetDlgItem(IDC_EDITKEYDATAHEX));
	BOOL bKeyData = poEdit4->GetWindowTextLength()>0;
	BOOL bCond = ((poEdit1->GetWindowTextLength()>0)&&((poEdit2->GetWindowTextLength()>0)||(poEdit3->GetWindowTextLength()>0)))
		&&(1==poBtnFile->GetCheck())||((poEdit5->GetWindowTextLength()>0)&&(0==poBtnFile->GetCheck()));
	pCmdUI->Enable(bKeyData&bCond);
}

void CFileCryptDlg::OnUpdateComboKeysize(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(AES == m_iMethod);
}

void CFileCryptDlg::OnUpdateComboBlocksize(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(AES == m_iMethod);
}

void CFileCryptDlg::OnUpdateComboOpMode(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(m_iMethod != XOR256_STREAM);
}

void CFileCryptDlg::OnUpdateComboPadding(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(m_iMethod != XOR256_STREAM);
}

void CFileCryptDlg::OnUpdateEditBlockSize(CCmdUI* pCmdUI)
{
	BOOL bEnable = (XOR256_BLOCK == m_iMethod);
	pCmdUI->Enable(bEnable);
	CSpinButtonCtrl* poSpin = static_cast<CSpinButtonCtrl*>(GetDlgItem(IDC_SPINBLOCKSIZE));
	poSpin->EnableWindow(bEnable);
}

void CFileCryptDlg::OnUpdateEditExtRounds(CCmdUI* pCmdUI)
{
	BOOL bEnable = (XOR256_BLOCK == m_iMethod);
	pCmdUI->Enable(bEnable);
	CSpinButtonCtrl* poSpin = static_cast<CSpinButtonCtrl*>(GetDlgItem(IDC_SPINEXTROUNDS));
	poSpin->EnableWindow(bEnable);
}

void CFileCryptDlg::OnUpdateEditIntRounds(CCmdUI* pCmdUI)
{
	BOOL bEnable = (XOR256_BLOCK == m_iMethod) || (XOR256_STREAM == m_iMethod);
	pCmdUI->Enable(bEnable);
	CSpinButtonCtrl* poSpin = static_cast<CSpinButtonCtrl*>(GetDlgItem(IDC_SPININTROUNDS));
	poSpin->EnableWindow(bEnable);
}

void CFileCryptDlg::OnOK() 
{
	//CDialog::OnOK(); // Avoid RETURN key action
	return;	
}

void CFileCryptDlg::OnCancel() 
{
	//CDialog::OnCancel(); // Avoid ESCAPE key action
	return;
}

void CFileCryptDlg::OnClose() 
{
	CDialog::EndDialog(IDOK);
}

BOOL CFileCryptDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// Add "About..." menu item to system menu.
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	//Setting Application's Menu
	m_oMenu.LoadMenu(IDR_MENU);
	SetMenu(&m_oMenu);
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	//Subclassing
	m_oComboMethods.SubclassDlgItem(IDC_COMBOMETHODS, this);
	m_oComboKeySize.SubclassDlgItem(IDC_COMBOKEYSIZE, this);
	m_oComboBlockSize.SubclassDlgItem(IDC_COMBOBLOCKSIZE, this);
	m_oEditBlockSize.SubclassDlgItem(IDC_EDITBLOCKSIZE, this);
	m_oEditKeyData.SubclassDlgItem(IDC_EDITKEYDATA, this);
	m_oEditKeyDataHex.SubclassDlgItem(IDC_EDITKEYDATAHEX, this);
	m_oComboOpMode.SubclassDlgItem(IDC_COMBOOPMODE, this);
	m_oComboPadding.SubclassDlgItem(IDC_COMBOPADDING, this);
	m_oEditExtRounds.SubclassDlgItem(IDC_EDITEXTROUNDS, this);
	m_oEditIntRounds.SubclassDlgItem(IDC_EDITINTROUNDS, this);
	m_oBtnAction.SubclassDlgItem(IDC_BTNACTION, this);
	m_oEdtStr.SubclassDlgItem(IDC_EDTSTR, this);
	m_oEdtStrHex.SubclassDlgItem(IDC_EDTSTRHEX, this);
	m_oEdtEnc.SubclassDlgItem(IDC_EDTENC, this);
	m_oEdtEncHex.SubclassDlgItem(IDC_EDTENCHEX, this);
	//
	m_oEdtDir1.SubclassDlgItem(IDC_EDTDIR1, this);
	m_oBtnDir1.SubclassDlgItem(IDC_BTNDIR1, this);
	m_oEdtExt1.SubclassDlgItem(IDC_EDTEXT1, this);
	m_oEdtFile1.SubclassDlgItem(IDC_EDTFILE1, this);
	m_oBtnFile1.SubclassDlgItem(IDC_BTNFILE1, this);
	//
	m_oEdtDir3.SubclassDlgItem(IDC_EDTDIR3, this);
	m_oBtnDir3.SubclassDlgItem(IDC_BTNDIR3, this);
	m_oEdtExt3.SubclassDlgItem(IDC_EDTEXT3, this);
	m_oEdtFile3.SubclassDlgItem(IDC_EDTFILE3, this);
	m_oBtnFile3.SubclassDlgItem(IDC_BTNFILE3, this);
	//
	//Create the ToolTip Control
	if(!m_oToolTipCtrl.Create(this))
	{
		TRACE("\nUnable to create ToolTip control");
	}
	else
	{
		m_oToolTipCtrl.AddTool(&m_oComboMethods, IDS_COMBOMETHODS);
		m_oToolTipCtrl.AddTool(&m_oComboKeySize, IDS_COMBOKEYSIZE);
		m_oToolTipCtrl.AddTool(&m_oComboBlockSize, IDS_COMBOBLOCKSIZE);
		m_oToolTipCtrl.AddTool(&m_oEditBlockSize, IDS_EDITBLOCKSIZE);
		m_oToolTipCtrl.AddTool(&m_oEditKeyData, IDS_EDITKEYDATA);

⌨️ 快捷键说明

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