📄 vigeneradlg.cpp
字号:
// VigeneraDlg.cpp : implementation file
//
#include "stdafx.h"
#include "030300816.h"
#include "VigeneraDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVigeneraDlg dialog
CVigeneraDlg::CVigeneraDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVigeneraDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVigeneraDlg)
m_cipherFile = _T("");
m_cipherString = _T("");
m_key = _T("fast");
m_plainFile = _T("");
m_plainString = _T("");
m_radio = 0;
m_iMethod = 0;
//}}AFX_DATA_INIT
}
void CVigeneraDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVigeneraDlg)
DDX_Text(pDX, IDC_CIPHER_FILE, m_cipherFile);
DDX_Text(pDX, IDC_CIPHER_STRING, m_cipherString);
DDX_Text(pDX, IDC_KEY, m_key);
DDX_Text(pDX, IDC_PLAIN_FILE, m_plainFile);
DDX_Text(pDX, IDC_PLAIN_STRING, m_plainString);
DDX_Radio(pDX, IDC_RADIO_STRING, m_radio);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVigeneraDlg, CDialog)
//{{AFX_MSG_MAP(CVigeneraDlg)
ON_BN_CLICKED(IDC_RADIO_STRING, OnRadioString)
ON_BN_CLICKED(IDC_RADIO_FILE, OnRadioFile)
ON_BN_CLICKED(IDC_ENCIPHER, OnEncipher)
ON_BN_CLICKED(IDC_DECIPHER, OnDecipher)
ON_BN_CLICKED(IDC_OPEN_PLAIN, OnOpenPlain)
ON_BN_CLICKED(IDC_OPEN_CIPHER, OnOpenCipher)
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVigeneraDlg message handlers
void CVigeneraDlg::OnRadioString()
{
// TODO: Add your control notification handler code here
ShowStringGroup(true);
ShowFileGroup(false);
m_iMethod = 0;
}
void CVigeneraDlg::OnRadioFile()
{
// TODO: Add your control notification handler code here
ShowStringGroup(false);
ShowFileGroup(true);
m_iMethod = 1;
}
void CVigeneraDlg::OnEncipher()
{
// TODO: Add your control notification handler code here
if ( 0 == m_iMethod )
{
EncipherString();
}
else // File
{
EncipherFile();
}
}
void CVigeneraDlg::OnDecipher()
{
// TODO: Add your control notification handler code here
if ( 0 == m_iMethod )
{
DecipherString();
}
else // File
{
DecipherFile();
}
}
void CVigeneraDlg::OnOpenPlain()
{
// TODO: Add your control notification handler code here
CFileDialog oFileOpen(TRUE);
oFileOpen.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
oFileOpen.m_ofn.lpstrDefExt="txt";
CString oStrDir;
if(IDOK == oFileOpen.DoModal())
{
CString oStrFilePath = oFileOpen.GetPathName();
TCHAR szDir[MAX_PATH+1];
_tcscpy(szDir, LPCTSTR(oStrFilePath));
oStrFilePath = CString(szDir);
SetDlgItemText(
IDC_PLAIN_FILE,
oStrFilePath
);
}
}
void CVigeneraDlg::OnOpenCipher()
{
// TODO: Add your control notification handler code here
CFileDialog oFileOpen(TRUE);
oFileOpen.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
oFileOpen.m_ofn.lpstrDefExt="txt";
CString oStrDir;
if(IDOK == oFileOpen.DoModal())
{
CString oStrFilePath = oFileOpen.GetPathName();
TCHAR szDir[MAX_PATH+1];
_tcscpy(szDir, LPCTSTR(oStrFilePath));
oStrFilePath = CString(szDir);
SetDlgItemText(
IDC_CIPHER_FILE,
oStrFilePath
);
}
}
void CVigeneraDlg::EncipherString()
{
UpdateData();
CVigenera vigenera;
vigenera.m_plainString = m_plainString;
vigenera.m_key = m_key;
vigenera.Encipher();
m_plainString = "";
m_cipherString = vigenera.m_cipherString;
UpdateData(FALSE);
}
void CVigeneraDlg::EncipherFile()
{
UpdateData();
if (m_cipherFile=="" || m_plainFile=="")
{
MessageBox("Please set file path correctly!","Error",MB_ICONSTOP);
return;
}
CFile rfile(
m_plainFile,
CFile::modeRead
);
char *pBuf;
DWORD dwFileLen;
dwFileLen=rfile.GetLength();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
rfile.Read(pBuf,dwFileLen);
rfile.Close();
CVigenera vigenera;
vigenera.m_plainString = pBuf;
vigenera.m_key = m_key;
vigenera.Encipher();
int length = vigenera.m_cipherString.GetLength() +1;
unsigned char * cipherBlock = (unsigned char*)malloc(length);
memset(cipherBlock, 0, length);
memcpy(cipherBlock, vigenera.m_cipherString, length -1);
CFile wfile(m_cipherFile,CFile::modeCreate | CFile::modeWrite);
wfile.Write(cipherBlock,length);
wfile.Close();
CString succeed = "File encryption succeed!\nThe enciphed file is saved at\n" +
m_cipherFile + "\n\npreview:\n" + cipherBlock;
MessageBox(succeed,"Encrption finish ^o^",MB_ICONINFORMATION);
UpdateData(FALSE);
}
void CVigeneraDlg::DecipherString()
{
UpdateData();
CVigenera vigenera;
vigenera.m_cipherString = m_cipherString;
vigenera.m_key = m_key;
vigenera.Decipher();
m_plainString = vigenera.m_plainString;
m_cipherString = "";
UpdateData(FALSE);
}
void CVigeneraDlg::ShowFileGroup(bool bShow)
{
CWnd* poWnd;
poWnd = GetDlgItem(IDC_STATIC_FILE1);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_STATIC_FILE2);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_STATIC_FILE3);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_PLAIN_FILE);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_CIPHER_FILE);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_OPEN_PLAIN);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_OPEN_CIPHER);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
}
void CVigeneraDlg::ShowStringGroup(bool bShow)
{
CWnd* poWnd;
poWnd = GetDlgItem(IDC_STATIC_STRING1);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_STATIC_STRING2);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_STATIC_STRING3);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_PLAIN_STRING);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
poWnd = GetDlgItem(IDC_CIPHER_STRING);
poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
}
void CVigeneraDlg::DecipherFile()
{
UpdateData();
if (m_cipherFile=="" || m_plainFile=="")
{
MessageBox("Please set file path correctly!","Error",MB_ICONSTOP);
return;
}
CFile rfile(
m_plainFile,
CFile::modeRead
);
char *pBuf;
DWORD dwFileLen;
dwFileLen=rfile.GetLength();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
rfile.Read(pBuf,dwFileLen);
rfile.Close();
CVigenera vigenera;
vigenera.m_cipherString = pBuf;
vigenera.m_key = m_key;
vigenera.Decipher();
int length = vigenera.m_cipherString.GetLength() +1;
unsigned char * cipherBlock = (unsigned char*)malloc(length);
memset(cipherBlock, 0, length);
memcpy(cipherBlock, vigenera.m_plainString, length -1);
CFile wfile(m_cipherFile,CFile::modeCreate | CFile::modeWrite);
wfile.Write(cipherBlock,length);
wfile.Close();
CString succeed = "File Decryption succeed!\nThe deciphed file is saved at\n" +
m_cipherFile + "\n\npreview:\n" + cipherBlock;
MessageBox(succeed,"Decrption finish ^o^",MB_ICONINFORMATION);
UpdateData(FALSE);
}
BOOL CVigeneraDlg::PreTranslateMessage(MSG* pMsg)
{
// CG: The following block was added by the ToolTips component. { // Let the ToolTip process this message. m_tooltip.RelayEvent(pMsg); } return CDialog::PreTranslateMessage(pMsg); // CG: This was added by the ToolTips component.
}
BOOL CVigeneraDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // CG: This was added by the ToolTips component. // CG: The following block was added by the ToolTips component. { // Create the ToolTip control. m_tooltip.Create(this); m_tooltip.Activate(TRUE); ShowStringGroup(true);
ShowFileGroup(false);
m_hArrow = AfxGetApp()->LoadCursor(IDC_MYARROW);
m_hHand = AfxGetApp()->LoadCursor(IDC_MYHAND);
m_hBeam = AfxGetApp()->LoadCursor(IDC_MYBEAM);
m_hPen = AfxGetApp()->LoadCursor(IDC_MYPEN);
m_hMove = AfxGetApp()->LoadCursor(IDC_MYMOVE); // TODO: Use one of the following forms to add controls: // m_tooltip.AddTool(GetDlgItem(IDC_<name>), <string-table-id>); // m_tooltip.AddTool(GetDlgItem(IDC_<name>), "<text>"); }
// pSkin2->ApplySkin((long)m_hWnd); return TRUE; // CG: This was added by the ToolTips component.
}
BOOL CVigeneraDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if ( pWnd == GetDlgItem(IDC_ENCIPHER)
|| pWnd == GetDlgItem(IDC_DECIPHER)
|| pWnd == GetDlgItem(IDC_OPEN_PLAIN)
|| pWnd == GetDlgItem(IDC_OPEN_CIPHER)
|| pWnd == GetDlgItem(IDC_SETKEY)
)
::SetCursor(m_hHand);
else if ( pWnd == GetDlgItem(IDC_PLAIN_STRING)
|| pWnd == GetDlgItem(IDC_CIPHER_STRING)
|| pWnd == GetDlgItem(IDC_KEY)
|| pWnd == GetDlgItem(IDC_PLAIN_FILE)
|| pWnd == GetDlgItem(IDC_CIPHER_FILE)
)
::SetCursor(m_hBeam);
else if ( pWnd == GetDlgItem(IDC_RADIO_STRING)
|| pWnd == GetDlgItem(IDC_RADIO_FILE)
)
::SetCursor(m_hPen);
else if ( nHitTest == HTCAPTION )
::SetCursor(m_hMove);
else
::SetCursor(m_hArrow);
return true;
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -