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

📄 encryptdlg.cpp

📁 加减密
💻 CPP
字号:
// encryptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "encrypt.h"
#include "encryptDlg.h"
#include "PlainDlg.h"
#include "stdio.h"
#include "aes.h"

//extern int aes_encrypt(char* file_path, char* cipher_path, char* key, int key_len);
//extern int aes_decrypt(char* cipher_path, char* file_path, char* key, int key_len);

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg dialog

CEncryptDlg::CEncryptDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEncryptDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEncryptDlg)
	m_edit_bits = _T("");
	m_edit_plain = _T("");
	m_edit_cipher = _T("");
	m_edit_outext = _T("");
	m_radio_128 = -1;
	m_radio_192 = -1;
	m_radio_256 = -1;
	m_static_bits = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEncryptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEncryptDlg)
	DDX_Control(pDX, IDC_KEY_EDIT, m_key_edit);
	DDX_Text(pDX, IDC_EDIT2, m_edit_bits);
	DDX_Text(pDX, IDC_EDIT3, m_edit_plain);
	DDX_Text(pDX, IDC_EDIT4, m_edit_cipher);
	DDX_Text(pDX, IDC_EDIT5, m_edit_outext);
	DDX_Radio(pDX, IDC_RADIO1, m_radio_128);
	DDX_Radio(pDX, IDC_RADIO2, m_radio_192);
	DDX_Radio(pDX, IDC_RADIO3, m_radio_256);
	DDX_Text(pDX, IDC_STATIC1, m_static_bits);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEncryptDlg, CDialog)
	//{{AFX_MSG_MAP(CEncryptDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio_128_Cliecked)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio_192_Cliecked)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio_256_Cliecked)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton_PlainFile_Clicked)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton_CipherFile_Clicked)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton_EncryptFile_Clicked)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton_DecryptFile_Clicked)
	ON_BN_CLICKED(IDC_BUT_ENCRYPT, OnButEncrypt)
	ON_BN_CLICKED(IDC_BUT_DECRYPT, OnButDecrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg message handlers

BOOL CEncryptDlg::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);
		}
	}

	// 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
	
	// TODO: Add extra initialization here

	//SetTextColor(m_hWnd, RGB(0, 0, 0xff));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEncryptDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEncryptDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEncryptDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEncryptDlg::OnRadio_128_Cliecked() 
{
	// TODO: Add your control notification handler code here
	::CheckDlgButton(m_hWnd,IDC_RADIO2,0);
	::CheckDlgButton(m_hWnd,IDC_RADIO3,0);
	::SetWindowText(::GetDlgItem(m_hWnd,IDC_STATIC1),"128");
	m_key_edit.SetLimitText(16);
}

void CEncryptDlg::OnRadio_192_Cliecked() 
{
	// TODO: Add your control notification handler code here
	::CheckDlgButton(m_hWnd,IDC_RADIO1,0);
	::CheckDlgButton(m_hWnd,IDC_RADIO3,0);
	::SetWindowText(::GetDlgItem(m_hWnd,IDC_STATIC1),"192");
	m_key_edit.SetLimitText(24);
}

void CEncryptDlg::OnRadio_256_Cliecked() 
{
	// TODO: Add your control notification handler code here
	::CheckDlgButton(m_hWnd,IDC_RADIO2,0);
	::CheckDlgButton(m_hWnd,IDC_RADIO1,0);
	::SetWindowText(::GetDlgItem(m_hWnd,IDC_STATIC1),"256");
	m_key_edit.SetLimitText(32);
}

void CEncryptDlg::OnButton_PlainFile_Clicked() 
{
	char szFileName[MAX_PATH] = "";

	OPENFILENAME file = { 0 };
	file.lStructSize = sizeof(file);

	file.lpstrFile = szFileName;
	file.nMaxFile = MAX_PATH;

	file.lpstrFilter = "*.xml\0*.xml\0Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	file.nFilterIndex = 1;
	
	//pop-up the dialog of open file
	if(::GetOpenFileName(&file))
	{
		::SetWindowText(::GetDlgItem(m_hWnd, IDC_PLAINT_EDIT),szFileName);
	}
}

void CEncryptDlg::OnButton_CipherFile_Clicked() 
{
	char szFileName[MAX_PATH] = "";
	OPENFILENAME file = { 0 };
	file.lStructSize = sizeof(file);

	file.lpstrFile = szFileName;
	file.nMaxFile = MAX_PATH;

	file.lpstrFilter = "*.xml\0*.xml\0Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	file.nFilterIndex = 1;

	//pop-up the dialog of open file
	if(::GetOpenFileName(&file))
	{
		::SetWindowText(::GetDlgItem(m_hWnd, IDC_CIPHER_EDIT),szFileName);
	}
}

void CEncryptDlg::OnButton_EncryptFile_Clicked() 
{
	int key_len=0;
	char in_key[256]={0};
	char file_path[256]={0};
	char cipher_path[256]={0};

	//get the length of key
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_STATIC_BITS),in_key,10);	
	key_len = atoi(in_key);

	//get the seciet key
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_KEY_EDIT),in_key,256);	
	if(strlen(in_key) == 0)
	{
		::MessageBox(m_hWnd, "No key specified!", "Message", MB_OK);
		return;
	}

	//get the path of plain file and cipher file
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_PLAINT_EDIT),file_path,256);
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_CIPHER_EDIT),cipher_path,256);
	if(strlen(file_path) == 0 && strlen(cipher_path) == 0)
		return;

	//execute the encrypt 
	if(aes_encrypt(file_path,cipher_path,in_key,key_len))
		::MessageBox(m_hWnd, "Encrypt Success!", "Message", MB_OK);
	else
		::MessageBox(m_hWnd, "Encrypt Fail!", "Message", MB_OK);
}

void CEncryptDlg::OnButton_DecryptFile_Clicked() 
{
	int key_len=0,i=0,index=0;
	char in_key[256]={0};
	char file_path[256]={0};
	char cipher_path[256]={0};

	//get the length of key
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_STATIC_BITS),in_key,10);	
	key_len = atoi(in_key);

	//get the seciet key
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_KEY_EDIT),in_key,256);	
	if(strlen(in_key) == 0)
	{
		::MessageBox(m_hWnd, "No key specified!", "Message", MB_OK);
		return;
	}

	//get the path of plain file and cipher file
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_PLAINT_EDIT),file_path,256);
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_CIPHER_EDIT),cipher_path,256);
	if(strlen(file_path) == 0 && strlen(cipher_path) == 0)
		return;

	//execute the decrypt
	if(aes_decrypt(cipher_path,file_path,in_key,key_len))
		::MessageBox(m_hWnd, "Decrypt Success!", "Message", MB_OK);
	else
		::MessageBox(m_hWnd, "Decrypt Fail!", "Message", MB_OK);
}

void CEncryptDlg::OnButEncrypt() 
{
	// TODO: Add your control notification handler code here
	char* cipher=NULL;
	char* plaint=NULL;
	int plant_len=0, block_len=0;
	int rest_len=0;
	unsigned long in_block[4]={0}, out_block[4]={0};	
	char in_key[256]={0};

	//get the length of key
	//key_len = 128;

	//get the seciet key
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_KEY_EDIT),in_key,256);	
	if(strlen(in_key) == 0)
	{
		::MessageBox(m_hWnd, "No key specified!", "Message", MB_OK);
		return;
	}
	set_key((u4byte*)in_key, 128);
	
	plant_len = ::GetWindowTextLength(::GetDlgItem(m_hWnd,IDC_PLANT_TEXT_EDIT));
	if((cipher = (char* )malloc(plant_len+16)) == NULL)
		return;
	memset(cipher,0,plant_len + 16);
	::GetWindowText(::GetDlgItem(m_hWnd,IDC_PLANT_TEXT_EDIT),cipher,(plant_len+16));

	plaint = cipher;
	rest_len = plant_len + 16;
	while (rest_len)
	{
		if (rest_len > 16)
			block_len = 16;
		else
			block_len = rest_len;
		
		memcpy((char*)in_block, (char*)plaint, block_len);
		encrypt(in_block, out_block);
		memcpy((char*)plaint, (char*)out_block, block_len);

		plaint += block_len;
		rest_len -= block_len;
	}
	cipher_temp = cipher;
	::SetWindowText(::GetDlgItem(m_hWnd,IDC_CIPHER_TEXT_EDIT),cipher);
}

void CEncryptDlg::OnButDecrypt() 
{
	// TODO: Add your control notification handler code here
	char* cipher=NULL;
	char* plaint=NULL;
	int plant_len=0, block_len=0;
	int rest_len=0;
	unsigned long in_block[4], out_block[4];
	char in_key[256]={0};

	//get the length of key
	//key_len = 128;

	//get the seciet key
	::GetWindowText(::GetDlgItem(m_hWnd, IDC_KEY_EDIT),in_key,256);	
	if(strlen(in_key) == 0)
	{
		::MessageBox(m_hWnd, "No key specified!", "Message", MB_OK);
		return;
	}
	set_key((u4byte*)in_key, 128);

	plant_len = ::GetWindowTextLength(::GetDlgItem(m_hWnd,IDC_PLANT_TEXT_EDIT));
	if((cipher = (char*)malloc(plant_len)) == NULL)
		return;
	memset(cipher,0,plant_len + 16);
	::GetWindowText(::GetDlgItem(m_hWnd,IDC_CIPHER_TEXT_EDIT),cipher,plant_len);	

	plaint = cipher_temp;
	rest_len = plant_len+16;	
	while (rest_len)
	{
		if (rest_len > 16)
			block_len = 16;
		else
			block_len = rest_len;
		
		memcpy((char*)in_block, plaint, block_len);
		decrypt(in_block, out_block);
		memcpy(plaint, (char*)out_block, block_len);
		
		plaint += block_len;
		rest_len -= block_len;
	}	
	::SetWindowText(::GetDlgItem(m_hWnd,IDC_OUT_TEXT_EDIT),cipher_temp);	
}


⌨️ 快捷键说明

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