jiamidlg.cpp

来自「使用vc编写的加密解密程序」· C++ 代码 · 共 347 行

CPP
347
字号
// JiaMiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "JiaMi.h"
#include "JiaMiDlg.h"

#include "aes.h"
#include "des.h"
#include "encfile.h"

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

/////////////////////////////////////////////////////////////////////////////
// CJiaMiDlg dialog

CJiaMiDlg::CJiaMiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CJiaMiDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CJiaMiDlg)
	m_strPlain = _T("");
	m_strKey = _T("");
	m_strEncrypt = _T("");
	m_nCryptType = 0;
	m_strEditFin = _T("");
	m_strEditFout = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	//waring warning warning!!!!!: must first call aes_inittab before call AES_jiami or AES_jiemi
	AES_inittab();
}
void DES_jiami(unsigned char in[8], unsigned char out[8], unsigned char key[8]);
void DES_jiemi(unsigned char in[8], unsigned char out[8], unsigned char key[8]);
void CJiaMiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJiaMiDlg)
	DDX_Text(pDX, IDC_EDITPAIN, m_strPlain);
	DDX_Text(pDX, IDC_EDITKEY, m_strKey);
	DDX_Text(pDX, IDC_EDITCRY, m_strEncrypt);
	DDX_Radio(pDX, IDC_RADIOAES, m_nCryptType);
	DDX_Text(pDX, IDC_EDIT_FIN, m_strEditFin);
	DDX_Text(pDX, IDC_EDIT_FOUT, m_strEditFout);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CJiaMiDlg, CDialog)
	//{{AFX_MSG_MAP(CJiaMiDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_DECRYPT, OnButtonDecrypt)
	ON_BN_CLICKED(IDC_BUTTON_ENCRYPT, OnButtonEncrypt)
	ON_BN_CLICKED(IDC_BUTTON_FIN, OnButtonFin)
	ON_BN_CLICKED(IDC_BUTTON_FOUT, OnButtonFout)
	ON_BN_CLICKED(IDC_BUTTON_FENC, OnButtonFenc)
	ON_BN_CLICKED(IDC_BUTTON_FDEC, OnButtonFdec)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJiaMiDlg message handlers

BOOL CJiaMiDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CJiaMiDlg::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 CJiaMiDlg::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 CJiaMiDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CJiaMiDlg::OnButtonDecrypt() 
{
	UpdateData(TRUE);
	unsigned char plain[16], key[16], crypt[16];
	GetHexFromString(m_strEncrypt, crypt, sizeof(crypt));
	GetHexFromString(m_strKey, key, sizeof(key));
	if (m_nCryptType == 0) {
		AES_jiemi(crypt, plain, key);
		m_strPlain = GetStringFromHex(plain, sizeof(plain));
		m_strKey = GetStringFromHex(key, sizeof(key));
		m_strEncrypt = GetStringFromHex(crypt, sizeof(crypt));
	}
	else {
		DES_jiemi(crypt, plain, key);
		m_strPlain = GetStringFromHex(plain, sizeof(plain)/2);
		m_strKey = GetStringFromHex(key, sizeof(key)/2);
		m_strEncrypt = GetStringFromHex(crypt, sizeof(crypt)/2);
	}

	UpdateData(FALSE);
}

void CJiaMiDlg::OnButtonEncrypt() 
{
	UpdateData(TRUE);
	unsigned char plain[16], key[16], crypt[16];
	GetHexFromString(m_strPlain, plain, sizeof(plain));
	GetHexFromString(m_strKey, key, sizeof(key));

	if (m_nCryptType == 0) {
		AES_jiami(plain, crypt, key);
		m_strPlain = GetStringFromHex(plain, sizeof(plain));
		m_strKey = GetStringFromHex(key, sizeof(key));
		m_strEncrypt = GetStringFromHex(crypt, sizeof(crypt));
	}
	else {
		DES_jiami(plain, crypt, key);
		m_strPlain = GetStringFromHex(plain, sizeof(plain)/2);
		m_strKey = GetStringFromHex(key, sizeof(key)/2);
		m_strEncrypt = GetStringFromHex(crypt, sizeof(crypt)/2);
	}

	UpdateData(FALSE);
}

void CJiaMiDlg::GetHexFromString(const CString& str, unsigned char state[], int size)
{
	int i = str.GetLength();
	if (str.GetLength() > size * 2)
		return;
	memset(state, 0, size);
	for (i = 0; i < str.GetLength(); i++)
		if (i%2) state[i/2] += ConvertChar2Hex(str[i]);
		else state[i/2] = ConvertChar2Hex(str[i]) * 16;
}


CString CJiaMiDlg::GetStringFromHex(unsigned char state[], int size)
{
	CString temp, hexbyte;
	int i;
	for (i = 0; i < size; i++) {
		hexbyte.Format("%02x", state[i]);
		temp += hexbyte;
	}
	return temp;
}

int CJiaMiDlg::ConvertChar2Hex(unsigned char ch)
{
	if (ch >= '0' && ch <= '9')
		return (ch-'0');
	else if (ch >= 'a' && ch<= 'f')
		return (ch-'a'+10);
	else
		return (ch-'A'+10);
}

void CJiaMiDlg::OnButtonFin() 
{
	UpdateData(TRUE);

	CFileDialog dlg(TRUE);
	dlg.DoModal();
	m_strEditFin = dlg.GetPathName();
	UpdateData(FALSE);
}

void CJiaMiDlg::OnButtonFout() 
{
	UpdateData(TRUE);

	CFileDialog dlg(FALSE);
	dlg.DoModal();
	m_strEditFout = dlg.GetPathName();
	UpdateData(FALSE);
}

void CJiaMiDlg::OnButtonFenc() 
{
	UpdateData(TRUE);
	unsigned char key[16];	
	GetHexFromString(m_strKey, key, sizeof(key));

	if (m_strEditFin == "") {
		MessageBox("无输入文件");
		return;
	}
	if (m_strEditFout == "") {
		MessageBox("无输出文件");
		return;
	}
	if (m_strKey == "")
		if (MessageBox("密钥应为32位?", 0, MB_YESNO) == IDNO)
			return;

	m_strKey = GetStringFromHex(key, sizeof(key));
	UpdateData(FALSE);

	if (!AES_file_encrypt(m_strEditFin, m_strEditFout, key))
		MessageBox("加密成功!");
	else
		MessageBox("加密失败!");
}

void CJiaMiDlg::OnButtonFdec() 
{
	UpdateData(TRUE);
	unsigned char key[16];	
	GetHexFromString(m_strKey, key, sizeof(key));

	if (m_strEditFin == "") {
		MessageBox("无输入文件");
		return;
	}
	if (m_strEditFout == "") {
		MessageBox("无输出文件");
		return;
	}
	if (m_strKey == "")
		if (MessageBox("密钥应为32位?", 0, MB_YESNO) == IDNO)
			return;

	m_strKey = GetStringFromHex(key, sizeof(key));
	UpdateData(FALSE);

	if (!AES_file_decrypt(m_strEditFout, m_strEditFin, key))
		MessageBox("解密成功!");
	else
		MessageBox("解密失败!");	
}

⌨️ 快捷键说明

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