📄 des_sysdlg.cpp
字号:
// DES_SYSDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DES_SYS.h"
#include "DES_SYSDlg.h"
#include "DES.h"
#include "stdlib.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()
/////////////////////////////////////////////////////////////////////////////
// CDES_SYSDlg dialog
CDES_SYSDlg::CDES_SYSDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDES_SYSDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDES_SYSDlg)
m_keyname = _T("");
m_filename = _T("");
m_savefile = _T("");
m_key = _T("");
m_filekey = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDES_SYSDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDES_SYSDlg)
DDX_Text(pDX, IDC_EDIT1, m_keyname);
DDX_Text(pDX, IDC_EDIT2, m_filename);
DDX_Text(pDX, IDC_EDIT3, m_savefile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDES_SYSDlg, CDialog)
//{{AFX_MSG_MAP(CDES_SYSDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON3, Onsefile)
ON_BN_CLICKED(IDC_BUTTON5, Onsavefile)
ON_BN_CLICKED(IDC_BUTTON4, OnOnfileenc)
ON_BN_CLICKED(IDC_BUTTON2, Onfiledec)
ON_BN_CLICKED(IDC_BUTTON6, OnCreateKey)
ON_BN_CLICKED(IDC_BUTTON1, OnLoadKey)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDES_SYSDlg message handlers
BOOL CDES_SYSDlg::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 CDES_SYSDlg::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 CDES_SYSDlg::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 CDES_SYSDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDES_SYSDlg::Onsefile()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
m_filename=dlg.GetPathName();
UpdateData(FALSE);
}
//DEL void CDES_SYSDlg::Onsavekey()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL UpdateData(TRUE);
//DEL CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
//DEL if(IDOK!=dlg.DoModal())return;
//DEL m_filename=dlg.GetPathName();
//DEL UpdateData(FALSE);
//DEL }
void CDES_SYSDlg::Onsavefile()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlg(0,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
m_savefile=dlg.GetPathName();
UpdateData(FALSE);
}
void CDES_SYSDlg::OnOnfileenc()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_filename=="" || m_keyname== "" || m_savefile == "")
{
MessageBox("请输入路径");
}
CFile ff(m_filename,CFile::modeRead);
long lFileLen=ff.GetLength();
long c=lFileLen/8;
long d=lFileLen%8;
CFile fp(m_savefile,CFile::modeCreate|CFile::modeWrite);
fp.SeekToBegin();
ff.SeekToBegin();
DES jm;
char inbuff[8],oubuff[8],skey[8];
/* char *cr=NULL;
for(int j=0;j<16;j++)
{
CString str=m_filekey.Mid(2*j,2);
skey[j]=unsigned char(strtol(str,&cr,16));
}*/
CFile fp1(m_keyname,CFile::modeRead);
char* buf = new char[4];
fp1.Read(buf, 4);
//if(buf == "KEY|")
/*if(!memcmp(buf, "KEY|", 4))
{
m_filekey = _T("");
fp1.Read(skey, 8);
}
else
{
//MessageBox("");
MessageBox("密钥损坏或无效 ",
"提示",MB_YESNO | MB_ICONQUESTION) ;
return;
}*/
int count = fp1.GetLength();
int j = 0;
while(j<count)
{
if ( memcmp(buf, "KEY|",4) == 0 )
break;
else
fp1.Seek(-3, CFile::current );
fp1.Read(buf, 4);
j++;
}
if ( j>=count )
{
MessageBox("读取密钥失败");
return;
}
memcpy(skey,(LPCTSTR)m_filekey,8);
for(long i=0;i<c;i++)
{
ff.Read(inbuff,8);
jm.Des_Go(oubuff, inbuff, sizeof(inbuff), skey,sizeof(skey), ENCRYPT);
fp.Write(oubuff,8);
}
if(d>0)
{
ff.Read(inbuff,d);
jm.Des_Go(oubuff, inbuff, sizeof(inbuff), skey,sizeof(skey), ENCRYPT);
fp.Write(oubuff,8);
}
fp1.Close();
ff.Close();
fp.Close();
AfxMessageBox("加密成功!");
UpdateData(FALSE);
}
void CDES_SYSDlg::Onfiledec()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_filename=="")return;
CFile ff(m_filename,CFile::modeRead);
long lFileLen=ff.GetLength();
long c=lFileLen/8;
long d=lFileLen%8;
if (m_savefile=="")
m_savefile=m_filename+".aen";
CFile fp(m_savefile,CFile::modeCreate|CFile::modeWrite);
fp.SeekToBegin();
ff.SeekToBegin();
//CString inbuff;
DES jm;
char inbuff[8],oubuff[8],skey[8];
memset(inbuff, 0, 8);
memset(oubuff, 0, 8);
memcpy(skey,(LPCTSTR)m_filekey,8);
CTime tm1,tm2;
tm1=CTime::GetCurrentTime();
for(long i=0;i<c;i++)
{
ff.Read(inbuff,8);
jm.Des_Go(oubuff, inbuff, sizeof(inbuff), skey,sizeof(skey), DECRYPT);
fp.Write(oubuff,8);
memset(inbuff, 0, 8);
memset(oubuff, 0, 8);
}
if(d>0)
{
AfxMessageBox("文件可能已损坏或非经aes加密过");
}
tm2=CTime::GetCurrentTime();
CTimeSpan tmSpan=tm2-tm1;
CString strspan=tmSpan.Format("Seconds:%S");
ff.Close();
fp.Close();
AfxMessageBox("解密成功!");
UpdateData(FALSE);
}
long suijishu()//产生随机key
{
long x,y;
srand( (unsigned)time( NULL ) );//与rand结合使用使每次产生key不同
x=rand();
y=rand();
return 1000*x+y;
}
void CDES_SYSDlg::OnCreateKey()
{
// TODO: Add your control notification handler code here
char* tmp = new char[8];
_itoa( suijishu(), tmp, 10 );
m_filekey = _T(tmp);
MessageBox("生成成功");
CFileDialog dlg(0,NULL,NULL,OFN_HIDEREADONLY ,"TXT Files(*.txt)|*.txt||");
dlg.m_ofn.lpstrDefExt="txt";
if(IDOK!=dlg.DoModal())return;
m_savefile=dlg.GetPathName();
CFile fp(m_savefile,CFile::modeCreate|CFile::modeWrite);
fp.SeekToBegin();
CString temp = _T("KEY|");
fp.Write("KEY|",temp.GetLength());
fp.Write(m_filekey,8);
}
//DEL void CDES_SYSDlg::OnSaveKey()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL UpdateData(TRUE);
//DEL CFileDialog dlg(0,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
//DEL if(IDOK!=dlg.DoModal())return;
//DEL m_filename=dlg.GetPathName();
//DEL
//DEL CFile fp(m_savefile,CFile::modeCreate|CFile::modeWrite);
//DEL fp.SeekToBegin();
//DEL fp.Write("KEY|",sizeof("KEY|"));
//DEL fp.Write(m_filekey,8);
//DEL UpdateData(FALSE);
//DEL }
void CDES_SYSDlg::OnLoadKey()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
m_keyname=dlg.GetPathName();
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -