📄 desdlg.cpp
字号:
// DesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "030300816.h"
#include "DesDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDesDlg dialog
CDesDlg::CDesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDesDlg)
m_cipherFile = _T("");
m_cipherString = _T("");
m_plainFile = _T("");
m_plainString = _T("");
m_radio = 0;
m_key = _T("");
//}}AFX_DATA_INIT
}
void CDesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDesDlg)
DDX_Text(pDX, IDC_CIPHER_FILE, m_cipherFile);
DDX_Text(pDX, IDC_CIPHER_STRING, m_cipherString);
DDX_Text(pDX, IDC_PLAIN_FILE, m_plainFile);
DDX_Text(pDX, IDC_PLAIN_STRING, m_plainString);
DDX_Radio(pDX, IDC_RADIO_STRING, m_radio);
DDX_Text(pDX, IDC_KEY, m_key);
DDV_MaxChars(pDX, m_key, 8);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDesDlg, CDialog)
//{{AFX_MSG_MAP(CDesDlg)
ON_BN_CLICKED(IDC_SETKEY, OnSetkey)
ON_BN_CLICKED(IDC_ENCIPHER, OnEncipher)
ON_BN_CLICKED(IDC_DECIPHER, OnDecipher)
ON_BN_CLICKED(IDC_RADIO_STRING, OnRadioString)
ON_BN_CLICKED(IDC_RADIO_FILE, OnRadioFile)
ON_BN_CLICKED(IDC_OPEN_PLAIN, OnOpenPlain)
ON_BN_CLICKED(IDC_OPEN_CIPHER, OnOpenCipher)
ON_WM_SETCURSOR()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CDesDlg::OnSetkey()
{
// TODO: Add your control notification handler code here
UpdateData();
UpdateData(false);
}
void CDesDlg::OnEncipher()
{
bool checked = CheckKey();
if ( checked )
{
// TODO: Add your control notification handler code here
if ( 0 == m_iMethod )
{
EncipherString();
}
else // File
{
EncipherFile();
}
}
}
BOOL CDesDlg::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 CDesDlg::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);
m_iMethod = 0;
ShowStringGroup(true);
ShowFileGroup(false);
GetDlgItem(IDC_ERROR)->ShowWindow(SW_HIDE);
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.
}
void CDesDlg::OnDecipher()
{
bool checked = CheckKey();
if ( checked )
{
// TODO: Add your control notification handler code here
if ( 0 == m_iMethod )
{
DecipherString();
}
else // File
{
DecipherFile();
}
}
}
void CDesDlg::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 CDesDlg::OnRadioString()
{
// TODO: Add your control notification handler code here
ShowStringGroup(true);
ShowFileGroup(false);
m_iMethod = 0;
}
void CDesDlg::OnRadioFile()
{
// TODO: Add your control notification handler code here
ShowStringGroup(false);
ShowFileGroup(true);
m_iMethod = 1;
}
void CDesDlg::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 CDesDlg::EncipherString()
{
UpdateData();
CDes Des;
Des.m_key = m_key;
Des.m_plainString = m_plainString;
// Des.m_decFbkKey = m_decFbkKey;
// Des.m_decInitKey = m_decInitKey;
Des.Encipher();
m_plainString = "";
m_cipherString = Des.m_cipherString;
UpdateData(false);
}
void CDesDlg::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();
CDes Des;
Des.m_plainString = pBuf;
Des.m_key = m_key;
// Des.m_decFbkKey = m_decFbkKey;
// Des.m_decInitKey = m_decInitKey;
Des.Encipher();
int length = Des.m_cipherString.GetLength() +1;
unsigned char * cipherBlock = (unsigned char*)malloc(length);
memset(cipherBlock, 0, length);
memcpy(cipherBlock, Des.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 CDesDlg::DecipherString()
{
UpdateData();
CDes Des;
Des.m_key = m_key;
Des.m_cipherString = m_cipherString;
// Des.m_decFbkKey = m_decFbkKey;
// Des.m_decInitKey = m_decInitKey;
Des.Decipher();
m_cipherString = "";
m_plainString = Des.m_plainString;
UpdateData(false);
}
void CDesDlg::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();
CDes Des;
Des.m_key = m_key;
Des.m_cipherString = pBuf;
// Des.m_decFbkKey = m_decFbkKey;
// Des.m_decInitKey = m_decInitKey;
Des.Decipher();
int length = Des.m_cipherString.GetLength() +1;
unsigned char * cipherBlock = (unsigned char*)malloc(length);
memset(cipherBlock, 0, length);
memcpy(cipherBlock, Des.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);
}
void CDesDlg::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 CDesDlg::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
);
}
}
BOOL CDesDlg::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_PLAIN_FILE)
|| pWnd == GetDlgItem(IDC_CIPHER_FILE)
|| pWnd == GetDlgItem(IDC_KEY)
)
::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;
}
bool CDesDlg::CheckKey()
{
UpdateData();
CWnd * checkKey = GetDlgItem( IDC_ERROR );
checkKey->ShowWindow(SW_HIDE);
int length = m_key.GetLength();
if ( length < 8 )
{
checkKey->ShowWindow(SW_SHOW);
return 0;
}
return 1;
}
HBRUSH CDesDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if (pWnd->GetDlgCtrlID() == IDC_ERROR)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0));
// Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT);
// Return handle to our CBrush object
hbr = m_brush;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -