📄 desddlg.cpp
字号:
// DesDDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DesD.h"
#include "DesDDlg.h"
#include "Des.h"
#include "Global.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()
/////////////////////////////////////////////////////////////////////////////
// CDesDDlg dialog
CDesDDlg::CDesDDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDesDDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDesDDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDesDDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDesDDlg)
DDX_Control(pDX, IDC_CHECK2, m_CheckHint);
DDX_Control(pDX, IDC_CHECK1, m_Check);
DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
DDX_Control(pDX, IDC_CONFIRMPW, m_ConKey);
DDX_Control(pDX, IDC_DESPASSWD, m_NewKey);
DDX_Control(pDX, IDC_RUN, m_btRun);
DDX_Control(pDX, IDC_EDITOUTPUT, m_editOutFile);
DDX_Control(pDX, IDC_EDITINPUT, m_editInputFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDesDDlg, CDialog)
//{{AFX_MSG_MAP(CDesDDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_INPUTFILE, OnInputfile)
ON_BN_CLICKED(IDC_SELECTDIR, OnSelectdir)
ON_BN_CLICKED(IDC_RUN, OnRun)
ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDesDDlg message handlers
BOOL CDesDDlg::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
m_Progress.SetPos(0);
m_Progress.SetStep(1);
m_CheckHint.SetCheck(1);
// TODO: Add extra initialization here
//m_editOutFile.SetWindowText("C:\\");
return TRUE; // return TRUE unless you set the focus to a control
}
void CDesDDlg::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 CDesDDlg::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 CDesDDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDesDDlg::OnInputfile()
{
// TODO: Add your control notification handler code here
CString m_strFileName;
CString right4;
CFileDialog mFileDlg(TRUE, NULL,NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"All Files (*.*)|*.*| |", AfxGetMainWnd());
if(mFileDlg.DoModal()==IDOK)
{
m_editInputFile.SetWindowText(mFileDlg.GetPathName());
m_strFileName=mFileDlg.GetPathName();
right4 = m_strFileName.Right(4);
right4.MakeUpper();
if(!right4.Compare(".DES"))
{
m_btRun.SetWindowText("解密");
m_editOutFile.SetWindowText(mFileDlg.GetPathName().Left(m_strFileName.GetLength()-4));
}
else
{
m_btRun.SetWindowText("加密");
m_editOutFile.SetWindowText(mFileDlg.GetPathName()+".DES");
}
}
}
void CDesDDlg::OnSelectdir()
{
// TODO: Add your control notification handler code here
CString m_strFolder;
CString m_strOldFileName;
int length;
m_editOutFile.GetWindowText(m_strOldFileName);
length = m_strOldFileName.ReverseFind('\\');
if(length>0)
m_strOldFileName = m_strOldFileName.Right(m_strOldFileName.GetLength()-length-1);
m_strFolder = "C:\\";
if(g_fSelectFolderDlg(&m_strFolder,m_strFolder,false))
{
UpdateData(false);
m_editOutFile.SetWindowText(m_strFolder+m_strOldFileName);
}
}
void CDesDDlg::OnRun()
{
// TODO: Add your control notification handler code here
// UpDate(true);
CString rstrFile,wstrFile;
CTime beginTime=CTime::GetCurrentTime();
CString newKey,conKey;
CString Caption;
bool Mode;
m_btRun.GetWindowText(Caption);
if(!Caption.Compare("运行")) return;
if(!Caption.Compare("加密"))
Mode = ENCRYPT;
else
Mode = DECRYPT;
m_editInputFile.GetWindowText(rstrFile);
m_editOutFile.GetWindowText(wstrFile);
m_NewKey.GetWindowText(newKey);
m_ConKey.GetWindowText(conKey);
if(rstrFile.IsEmpty())
{
MessageBox("请选择需要加/解密的文件!","输入错误",MB_OK|MB_ICONSTOP);
return;
}
if(newKey.IsEmpty()||newKey.IsEmpty()||newKey!=conKey)
{
MessageBox("密钥输入错误,请重新输入!","输入错误",MB_OK|MB_ICONSTOP);
m_NewKey.SetWindowText("");
m_ConKey.SetWindowText("");
return;
}
CFile rFile,wFile;
if(rFile.Open(rstrFile,CFile::modeRead|CFile::typeBinary)==0)
return;
wFile.Open(wstrFile,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::typeBinary);
int nCount;
int nMod;
CString ss;
char Buffer[8];
nMod = rFile.GetLength()%8;
nCount = rFile.GetLength()/8;
if(Mode == DECRYPT && nMod!=0)
{
nCount--;
}
m_Progress.SetRange(0,nCount);
//-------------------------加解密模块----------------------
Des_SetKey(newKey);
for(int i=0;i<nCount;i++)
{
m_Progress.StepIt();
rFile.Read(Buffer,8);
Des_Run(Buffer,Buffer,Mode);
wFile.Write(Buffer,8);
}
if(Mode == DECRYPT && nMod!=0)
{
char k[1];
int Num;
rFile.Read(Buffer,8);
Des_Run(Buffer,Buffer,Mode);
rFile.Read(k,1);
Num = k[0]-'0';
wFile.Write(Buffer,Num);
}
if(Mode == ENCRYPT && nMod)
{
strcpy(Buffer,"00000000");
rFile.Read(Buffer,nMod);
Des_Run(Buffer,Buffer,Mode);
wFile.Write(Buffer,8);
Buffer[0]='0'+nMod;
wFile.Write(Buffer,1);
}
rFile.Close();
wFile.Close();
//-------------------------加解密模块----------------------
CTimeSpan timeSpan=CTime::GetCurrentTime()-beginTime;
CString TimeSpan;
TimeSpan.Format("操作耗时%d秒!",timeSpan.GetSeconds());
if(m_Check.GetCheck())
{
if(m_CheckHint.GetCheck())
{
if(MessageBox("确定要删除原文件吗?","提示信息",MB_OKCANCEL|MB_ICONINFORMATION)==IDOK)
DeleteFile(rstrFile);
}
else
DeleteFile(rstrFile);
}
MessageBox(rstrFile+" "+Caption+"完成,"+TimeSpan+"谢谢您使用本加密工具!","提示信息",MB_OK|MB_ICONINFORMATION);
m_Progress.SetPos(0);
m_editInputFile.SetWindowText("");
m_editOutFile.SetWindowText("");
m_NewKey.SetWindowText("");
m_ConKey.SetWindowText("");
m_btRun.SetWindowText("运行");
//---------------------------------------------------------
}
void CDesDDlg::OnCheck1()
{
// TODO: Add your control notification handler code here
if(m_Check.GetCheck())
m_CheckHint.EnableWindow(true);
else
m_CheckHint.EnableWindow(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -