📄 decryptdlg.cpp
字号:
// decryptdlg.cpp : implementation file
//
#include "stdafx.h"
#include "encrypt.h"
#include "decryptdlg.h"
#include "des.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Cdecryptdlg dialog
Cdecryptdlg::Cdecryptdlg(CWnd* pParent /*=NULL*/)
: CDialog(Cdecryptdlg::IDD, pParent)
{
//{{AFX_DATA_INIT(Cdecryptdlg)
m_password = _T("");
//}}AFX_DATA_INIT
}
void Cdecryptdlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Cdecryptdlg)
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
DDX_Text(pDX, IDC_EDIT_PASSWORD, m_password);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Cdecryptdlg, CDialog)
//{{AFX_MSG_MAP(Cdecryptdlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Cdecryptdlg message handlers
void Cdecryptdlg::OnOK()
{
GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
UpdateData(TRUE);
if(m_password=="")
{ MessageBox("请输入您的密码");
GetDlgItem(IDCANCEL)->EnableWindow(true);
GetDlgItem(IDOK)->EnableWindow(true);
return;
}
if(m_password.GetLength()>8||m_password.GetLength()<8)
{
MessageBox("密码字符必须为8位之间,请重新输入");
m_password="";
UpdateData(FALSE);
UpdateData(TRUE);
GetDlgItem(IDCANCEL)->EnableWindow(true);
GetDlgItem(IDOK)->EnableWindow(true);
return;
}
CFile fin;//建立文件指针
CString filename;
filename="C:\\mylockedfile\\"+decryptfilename;
if(!fin.Open(filename,CFile::modeRead))//判断打开文件是否正常
{
MessageBox("文件打开失败,请重新检查你的文件是否存在或文件名是否正确");
m_password="";
UpdateData(FALSE);
UpdateData(TRUE);
GetDlgItem(IDCANCEL)->EnableWindow(true);
GetDlgItem(IDOK)->EnableWindow(true);
return;
}
//如果正常
CString savename;
savename=filename+".jib";
CFile fout(savename,CFile::modeCreate|CFile::modeWrite);//新建一个文件
long lFileLen=fin.GetLength();//求出文件的字节数
long c=lFileLen/8-2;//计算文件可以分多少块进行解密
int d;
fin.SeekToBegin();
fout.SeekToBegin();
char inbuf[8],key[8];//定义一个字符数组,用来存储每次读入的信息
memcpy(key,(LPCTSTR)m_password,8);
float m;
int t=0;
m=c/20;
m_progress.SetRange(0,20);
m_progress.SetStep(1);
m_progress.SetPos(0);
//解密文件的前8位,并与当前的密码进行比对
fin.Read(inbuf,8);
Des_SetKey(key);
Des_Run(inbuf, inbuf, DECRYPT);
bool flag=false;
for(int j=0;j<8;j++)
{
if(inbuf[j]!=key[j])
flag=true;
}
if(flag)
{
MessageBox("密码错误,请重新输入密码");
m_password="";
UpdateData(FALSE);
UpdateData(TRUE);
fin.Close();
fout.Close();
return;
}
//提取密文中的标志位
fin.Read(inbuf,8);
Des_SetKey(key);
Des_Run(inbuf, inbuf, DECRYPT);
if(inbuf[0]=='0')
d=8;
else d=inbuf[0]-48;
//开始解密数据
for(long i=0;i<c-1;i++)//解密成块数据
{
m_progress.SetPos((int)((i+1)/m));
if(t!=(int)((i+1)/m))
{t=(int)((i+1)/m);
m_progress.StepIt();
}
fin.Read(inbuf,8);
Des_SetKey(key);
Des_Run(inbuf, inbuf, DECRYPT);
fout.Write(inbuf,8);
}
//解密尾数,对其进行特殊处理
fin.Read(inbuf,8);
Des_SetKey(key);
Des_Run(inbuf, inbuf, DECRYPT);
fout.Write(inbuf,d);
//关闭已打开的文件
fin.Close();
fout.Close();
CString savetofilename("C:\\mydecryptfile\\");
savetofilename=savetofilename+decryptfilename;
MoveFile(savename,savetofilename);
DeleteFile(filename);
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -