📄 encryptdlg.cpp
字号:
// encryptDlg.cpp : implementation file
//
#include "stdafx.h"
#include <fstream.h>
#include "encrypt.h"
#include "encryptDlg.h"
#define AFX_RC4
#include "rc4.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()
/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg dialog
CEncryptDlg::CEncryptDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEncryptDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEncryptDlg)
m_strOpenFileName = _T("");
m_strSvaePath = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEncryptDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEncryptDlg)
DDX_Text(pDX, IDE_FILE_NAME, m_strOpenFileName);
DDX_Text(pDX, IDE_SAVE_PATH, m_strSvaePath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEncryptDlg, CDialog)
//{{AFX_MSG_MAP(CEncryptDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_ENCRYPT_BROWSE, OnEncryptBrowse)
ON_BN_CLICKED(IDC_SAVE_BROWSE, OnSaveBrowse)
ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
ON_BN_CLICKED(IDC_CANCEL, OnCancel)
ON_EN_CHANGE(IDE_SAVE_PATH, OnChangeSavePath)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg message handlers
BOOL CEncryptDlg::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 CEncryptDlg::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 CEncryptDlg::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 CEncryptDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEncryptDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
// CDialog::OnClose();
CDialog::OnCancel();
}
void CEncryptDlg::OnCancel()
{
// TODO: Add your control notification handler code here
// CDialog::OnClose();
CDialog::OnCancel();
}
void CEncryptDlg::OnEncryptBrowse()
{
// TODO: Add your control notification handler code here
// CFileDialog OpenFile(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"TEXT Files (*.txt)|*.txt|All Files (*.*)|*.*||",this);
CFileDialog OpenFile(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"text Files (*.txt)|*.txt||",this);
if(OpenFile.DoModal() == IDOK)
{
m_strFileTitle = OpenFile.GetFileTitle();
m_strOpenFileName = OpenFile.GetPathName();
UpdateData(false);
}
}
void CEncryptDlg::OnSaveBrowse()
{
// TODO: Add your control notification handler code here
// CFileDialog SaveFile(FALSE,".en",m_strFileTitle,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"TEXT Files (*.txt)|*.txt|encrypt Files (*.en)|*.en|All Files (*.*)|*.*||",this);
CFileDialog SaveFile(FALSE,".txt",m_strFileTitle,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"text Files (*.txt)|*.txt||",this);
// CFileDialog SaveFile(FALSE,NULL,m_strFileTitle,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,"TEXT Files (*.txt)|*.txt|encrypt Files (*.en)|*.en|All Files (*.*)|*.*||",this);
if(SaveFile.DoModal() == IDOK)
{
m_strSvaePath = SaveFile.GetPathName();
UpdateData(false);
}
}
void CEncryptDlg::OnEncrypt()
{
// TODO: Add your control notification handler code here
//判断源文件路径名与目的文件路径名是否相同
UpdateData(TRUE);
m_strOpenFileName.TrimLeft(' ');
m_strOpenFileName.TrimRight(' ');
m_strSvaePath.TrimLeft(' ');
m_strSvaePath.TrimRight(' ');
BOOL bSourceDestSame = FALSE;
if( m_strOpenFileName == m_strSvaePath )
{
bSourceDestSame = TRUE;
m_strSvaePath += ".temp!!";
}
ifstream infile(m_strOpenFileName);
ofstream outfile(m_strSvaePath);
if(!infile||!outfile)
{
AfxMessageBox("请输入合法的保存文件");
return;
}
struct rc4_state s;
UCHAR keys[11]={0x01,0x23,0x36,0x47,0x65,0x71,0x89,0xab,0xb1,0xcd,0xef};
char buff[256];
memset(buff,0,256);
while(infile >>buff)
{
int len = lstrlen(buff);
rc4_setup( &s, keys,11);
rc4_crypt( &s,(UCHAR *) buff, len);
outfile<<buff<<endl;
memset(buff,0,256);
}
infile.close();
outfile.close();
if( TRUE == bSourceDestSame )
{
CFile::Remove( m_strOpenFileName);
CFile::Rename( m_strSvaePath, m_strOpenFileName);
}
return;
}
void CEncryptDlg::OnChangeSavePath()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -