📄 passwordtestdlg.cpp
字号:
// PassWordTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PassWordTest.h"
#include "PassWordTestDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CPassWordTestDlg dialog
CPassWordTestDlg::CPassWordTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPassWordTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPassWordTestDlg)
m_FilePath = _T("");
m_FileName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPassWordTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPassWordTestDlg)
DDX_Text(pDX, IDC_FILEPATH, m_FilePath);
DDX_Text(pDX, IDC_FILENAME, m_FileName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPassWordTestDlg, CDialog)
//{{AFX_MSG_MAP(CPassWordTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_PWD, OnPwd)
ON_BN_CLICKED(IDC_REPWD, OnRepwd)
ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPassWordTestDlg message handlers
BOOL CPassWordTestDlg::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
AfxMessageBox("请注意:加密或解密生成的文件与原始文件在同一个目录下!");
return TRUE; // return TRUE unless you set the focus to a control
}
void CPassWordTestDlg::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 CPassWordTestDlg::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 CPassWordTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPassWordTestDlg::OnPwd()
{
// TODO: Add your control notification handler code here
CString strFileName,strFilePass,strOutFileName;
GetDlgItemText(IDC_FILENAME,strFileName);
GetDlgItemText(IDC_PASS,strFilePass);
GetDlgItemText(IDC_OUTFILE,strOutFileName);
if( (strFilePass.IsEmpty()) || (strFilePass.IsEmpty()) || (strOutFileName.IsEmpty()) )
{
AfxMessageBox("你输入的信息不完整,请确定填写完整!");
GetDlgItem(IDC_PASS)->SetWindowText("");
return;
}
char* pwd=strdup(strFilePass);//使用函数strdup使CString转换成为字符型数组
FILE *fp1,*fp2;
int flag=1;
register char ch;
int j=0;
int j0=0;
fp1=fopen(strFileName,"rb");
if(fp1==NULL){
AfxMessageBox("文件打开失败,请确定文件与工具在同一个目录下");
exit(1);/*如果不能打开要加密的文件,便退出程序*/
}
ch=fgetc(fp1);
if(ch=='-')flag=0;
if(!flag)
{
AfxMessageBox("文件已加密过,请不要尝试再次加密!!");
return;
}
fp2=fopen(strOutFileName,"wb");
if(fp2==NULL){
AfxMessageBox("加密文件写入失败,请重新尝试一次!");
exit(1);/*如果不能建立加密后的文件,便退出*/
}
fseek(fp2,1,1);
rewind(fp1);//重置FP1的位置
while(pwd[++j0]);//利用j0判断密码的长度,如果异或的长度超过密码则重置
ch=fgetc(fp1);
/*加密算法开始*/
while(!feof(fp1)){
fputc(ch^pwd[j>=j0?j=0:j++],fp2);//异或后写入fp2文件
ch=fgetc(fp1);
}
rewind(fp2);
fputc('-',fp2);
fseek(fp2,0,2);
fclose(fp1);/*关闭源文件*/
fclose(fp2);/*关闭目标文件*/
if(AfxMessageBox("是否删除原始文件,只留下加密文件??",1)==IDOK)
{
if( remove(strFileName) != 0 )
{
AfxMessageBox("原始文件 "+strFileName+" 删除失败!");
}
AfxMessageBox("操作成功!");
}
exit(1);
}
void CPassWordTestDlg::OnRepwd()
{
// TODO: Add your control notification handler code here
CString strFileName,strFilePass,strOutFileName;
GetDlgItemText(IDC_FILENAME,strFileName);
GetDlgItemText(IDC_PASS,strFilePass);
GetDlgItemText(IDC_OUTFILE,strOutFileName);
if( (strFilePass.IsEmpty()) || (strFilePass.IsEmpty()) || (strOutFileName.IsEmpty()) )
{
AfxMessageBox("你输入的信息不完整,请确定填写完整!");
GetDlgItem(IDC_PASS)->SetWindowText("");
return;
}
char* pwd=strdup(strFilePass);
FILE *fp1,*fp2;
int flag=1;
register char ch;
int j=0;
int j0=0;
fp1=fopen(strFileName,"rb");
if(fp1==NULL){
AfxMessageBox("文件打开失败,请确定文件与工具在同一个目录下");
exit(1);/*如果不能打开要加密的文件,便退出程序*/
}
ch=fgetc(fp1);
if(ch!='-')flag=0;
if(!flag)
{
AfxMessageBox("文件还没有加密过,请不要尝试再次解密!!");
return;
}
fp2=fopen(strOutFileName,"wb");
if(fp2==NULL){
AfxMessageBox("解密文件写入失败,请重新尝试一次!");
exit(1);/*如果不能建立加密后的文件,便退出*/
}
while(pwd[++j0]);
ch=fgetc(fp1);
/*加密算法开始*/
while(!feof(fp1)){
fputc(ch^pwd[j>=j0?j=0:j++],fp2);/*异或后写入fp2文件*/
ch=fgetc(fp1);
}
fclose(fp1);/*关闭源文件*/
fclose(fp2);/*关闭目标文件*/
if(AfxMessageBox("是否删除原始文件,只留下解密文件??",1)==IDOK)
{
if( remove(strFileName) != 0 )
{
AfxMessageBox("原始文件 "+strFileName+" 删除失败!");
}
AfxMessageBox("操作成功!");
}
exit(1);
}
void CPassWordTestDlg::OnOpenfile()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(true);
dlg.DoModal();
m_FileName=dlg.GetFileName();
m_FilePath=dlg.GetPathName();
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -