📄 hillcodordlg.cpp
字号:
// HillCodorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "HillCodor.h"
#include "HillCodorDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CHillCodorDlg dialog
CHillCodorDlg::CHillCodorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHillCodorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHillCodorDlg)
m_strEnMFilePath = _T("");
m_strEnEFilePath = _T("");
m_strEnKeyFilePath = _T("");
m_strEnKey = _T("");
m_strDisMFilePath = _T("");
m_strDisKeyFilePath = _T("");
m_strDisEFilePath = _T("");
m_strUnMFilePath = _T("");
m_strUnEFilePath = _T("");
m_strUnKey = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHillCodorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHillCodorDlg)
DDX_Text(pDX, IDC_EN_M_FILE_PATH, m_strEnMFilePath);
DDX_Text(pDX, IDC_EN_E_FILE_PATH, m_strEnEFilePath);
DDX_Text(pDX, IDC_EN_KEY_FILE_PATH, m_strEnKeyFilePath);
DDX_Text(pDX, IDC_EN_KEY, m_strEnKey);
DDX_Text(pDX, IDC_DIS_M_FILE_PATH, m_strDisMFilePath);
DDX_Text(pDX, IDC_DIS_KEY_FILE_PATH, m_strDisKeyFilePath);
DDX_Text(pDX, IDC_DIS_E_FILE_PATH, m_strDisEFilePath);
DDX_Text(pDX, IDC_UN_M_FILE_PATH, m_strUnMFilePath);
DDX_Text(pDX, IDC_UN_E_FILE_PATH, m_strUnEFilePath);
DDX_Text(pDX, IDC_UNCODE_KEY, m_strUnKey);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHillCodorDlg, CDialog)
//{{AFX_MSG_MAP(CHillCodorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_OPEN_M_FILE, OnButtonOpenMFile)
ON_BN_CLICKED(IDC_BUTTON_SAVE_E_FILE, OnButtonSaveEFile)
ON_BN_CLICKED(IDC_BUTTON_SAVE_KEY_FILE, OnButtonSaveKeyFile)
ON_BN_CLICKED(IDC_BUTTON_ENCRYPTION, OnButtonEncryption)
ON_BN_CLICKED(IDC_BUTTON_SAVE_M_FILE, OnButtonSaveMFile)
ON_BN_CLICKED(IDC_BUTTON_OPEN_E_FILE, OnButtonOpenEFile)
ON_BN_CLICKED(IDC_BUTTON_OPEN_KEY_FILE_DIS, OnButtonOpenKeyFileDis)
ON_BN_CLICKED(IDC_BUTTON_DISCRYPTION, OnButtonDiscryption)
ON_BN_CLICKED(IDC_BUTTON_GET_KEY, OnButtonGetKey)
ON_BN_CLICKED(IDC_BUTTON_OPEN_M_UN, OnButtonOpenMUn)
ON_BN_CLICKED(IDC_BUTTON_OPEN_E_UN, OnButtonOpenEUn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHillCodorDlg message handlers
BOOL CHillCodorDlg::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 CHillCodorDlg::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 CHillCodorDlg::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 CHillCodorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHillCodorDlg::OnButtonOpenMFile()
{
// TODO: Add your control notification handler code here
// 提示选择打开文件路径
CFileDialog dlg(TRUE, "txt", m_strEnMFilePath, OFN_HIDEREADONLY,
"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
// 提示用户选择保存的路径
if (dlg.DoModal() != IDOK)
{
// 返回
return;
}
// 获取用户指定的文件路径
m_strEnMFilePath = dlg.GetPathName();
// 更新
UpdateData(FALSE);
}
void CHillCodorDlg::OnButtonSaveEFile()
{
// TODO: Add your control notification handler code here
// 提示选择保存文件路径
CFileDialog dlg(FALSE, "txt", m_strEnEFilePath, OFN_HIDEREADONLY,
"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
// 提示用户选择保存的路径
if (dlg.DoModal() != IDOK)
{
// 返回
return;
}
// 获取用户指定的文件路径
m_strEnEFilePath = dlg.GetPathName();
// 更新
UpdateData(FALSE);
}
void CHillCodorDlg::OnButtonSaveKeyFile()
{
// TODO: Add your control notification handler code here
// 提示选择保存文件路径
CFileDialog dlg(FALSE, "txt", m_strEnKeyFilePath, OFN_HIDEREADONLY,
"txt文本文件 (*.txt) | *.txt|所有文件 (*.*) | *.*||", NULL);
// 提示用户选择保存的路径
if (dlg.DoModal() != IDOK)
{
// 返回
return;
}
// 获取用户指定的文件路径
m_strEnKeyFilePath = dlg.GetPathName();
// 更新
UpdateData(FALSE);
}
void CHillCodorDlg::OnButtonEncryption()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFile fileM; // 明文
CFileException fe;
if(!fileM.Open(m_strEnMFilePath.GetBuffer(0), CFile::modeRead, &fe))
{
CString strError = "";
strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
m_strEnMFilePath.GetBuffer(0), fe.m_cause);
AfxMessageBox(strError);
return;
}
CFile fileE; // 密文
if(!fileE.Open(m_strEnEFilePath.GetBuffer(0), CFile::modeWrite|CFile::modeCreate, &fe))
{
CString strError = "";
strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
m_strEnMFilePath.GetBuffer(0), fe.m_cause);
AfxMessageBox(strError);
return;
}
CFile fileKey; // 解密密钥文件
if(!fileKey.Open(m_strEnKeyFilePath.GetBuffer(0), CFile::modeWrite|CFile::modeCreate, &fe))
{
CString strError = "";
strError.Format("Error!\r\nCan't open the file:%s\r\nCode: %d",
m_strEnMFilePath.GetBuffer(0), fe.m_cause);
AfxMessageBox(strError);
return;
}
CHillCode codeEncryptor;
long *pEncryKey = NULL;
char * pM = NULL;
char * pE = NULL;
int nLen = m_strEnKey.GetLength(); // 块大小
int i = 0;
nLen = (int)sqrt((nLen));
pM = new char[nLen];
if(NULL == pM)
{
exit(1);
}
pE = new char[nLen];
if(NULL == pM)
{
exit(1);
}
int nKeyLen = nLen * nLen;
pEncryKey = new long[nKeyLen];
if(NULL == pEncryKey)
{
exit(1);
}
for(i=0; i<nKeyLen; i++)
{
pEncryKey[i] = m_strEnKey.GetAt(i);
}
BeginWaitCursor();
// 设置密钥
if(!codeEncryptor.SetEncryKey(pEncryKey, nKeyLen))
{
MessageBox("密钥不合适,请重新输入!");
if(NULL != pEncryKey)
{
delete pEncryKey;
pEncryKey = NULL;
}
if(NULL != pM)
{
delete pM;
pM = NULL;
}
if(NULL != pE)
{
delete pE;
pE = NULL;
}
fileM.Close();
fileE.Close();
fileKey.Close();
return;
}
m_strEnKey = m_strEnKey.Left(nKeyLen);
UpdateData(FALSE);
// 写入解密密钥(一个密钥元素占4个字节)
long * pDiscryKey = NULL;
pDiscryKey = new long[nKeyLen];
if(NULL == pDiscryKey)
{
exit(1);
}
memcpy(pDiscryKey, codeEncryptor.GetDiscryptedKey(), sizeof(long) * nKeyLen);
fileKey.Write((char *)pDiscryKey, sizeof(long) * nKeyLen);
// 写入明文长度
long nMFileLen = fileM.GetLength();
fileKey.Write(&nMFileLen, sizeof(long));
// 分块循环加密
int nBlockNum = nMFileLen / nLen;
for(i=0; i<nBlockNum; i++)
{
fileM.Read(pM, nLen);
codeEncryptor.SetM((const unsigned char *)pM, nLen);
memcpy(pE, codeEncryptor.Encrypting(), nLen);
fileE.Write(pE, nLen);
}
// 加密最后一块
NULL;
EndWaitCursor();
// 结束处理
if(NULL != pDiscryKey)
{
delete pDiscryKey;
pDiscryKey = NULL;
}
if(NULL != pEncryKey)
{
delete pEncryKey;
pEncryKey = NULL;
}
if(NULL != pM)
{
delete pM;
pM = NULL;
}
if(NULL != pE)
{
delete pE;
pE = NULL;
}
fileM.Close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -