📄 desdlg.cpp
字号:
// DESDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DES.h"
#include "DESDlg.h"
#include "DES1.h"
#include "tablearray.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DES_CIPHER_TAG 0x43534544
#define MAXRANGE_OF_PROGRESS 1024
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDESDlg dialog
CDESDlg::CDESDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDESDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDESDlg)
m_edit = _T("");
m_mode = -1;
m_plainText = _T("");
m_cipherText = _T("");
m_key = _T("");
m_key2 = _T("");
m_ispwd = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDESDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDESDlg)
DDX_Control(pDX, IDC_PROGRESS1, m_prgs);
DDX_Control(pDX, IDC_DIRECTIONICON, m_directicon);
DDX_Control(pDX, IDC_OP, m_op);
DDX_Text(pDX, IDC_EDIT1, m_edit);
DDX_CBIndex(pDX, IDC_MODE, m_mode);
DDX_Text(pDX, IDC_SRCPATH, m_plainText);
DDX_Text(pDX, IDC_TARGETPATH, m_cipherText);
DDX_Text(pDX, IDC_KEY, m_key);
DDV_MaxChars(pDX, m_key, 8);
DDX_Text(pDX, IDC_KEY2, m_key2);
DDX_Check(pDX, IDC_ISPWD, m_ispwd);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDESDlg, CDialog)
//{{AFX_MSG_MAP(CDESDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_SRC, OnSrc)
ON_BN_CLICKED(ID_TARGET, OnTarget)
ON_LBN_SELCHANGE(IDC_OP, OnSelchangeOp)
ON_BN_CLICKED(IDC_CRYPT, OnCrypt)
ON_CBN_SELCHANGE(IDC_MODE, OnSelchangeMode)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDESDlg message handlers
BOOL CDESDlg::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
int a=m_op.AddString("解密");
a=m_op.AddString("加密");
// TODO: Add extra initialization here
CComboBox *cmb=(CComboBox*)GetDlgItem(IDC_MODE);
cmb->SetCurSel(0);
m_prgs.SetRange(0,MAXRANGE_OF_PROGRESS);
//m_prgs.SetStep(1);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDESDlg::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 CDESDlg::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 CDESDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDESDlg::OnSrc()
{
// TODO: Add your control notification handler code here
CFileDialog fd(TRUE);
if(fd.DoModal()==IDOK)
m_plainText=fd.GetPathName();
UpdateData(FALSE);
}
void CDESDlg::OnTarget()
{
// TODO: Add your control notification handler code here
CFileDialog fd(TRUE);
if(fd.DoModal()==IDOK)
m_cipherText=fd.GetPathName();
UpdateData(FALSE);
}
void CDESDlg::OnSelchangeOp()
{
// TODO: Add your control notification handler code here
UpdateData();
HICON icon;
if(m_op.GetCurSel())
icon=LoadIcon(AfxGetInstanceHandle( ),MAKEINTRESOURCE(IDI_DOWNARROW));
else
icon=LoadIcon(AfxGetInstanceHandle( ),MAKEINTRESOURCE(IDI_UPARROW));
m_directicon.SetIcon(icon);
// m_directicon.RedrawWindow();
}
void CDESDlg::OnCrypt()
{
// TODO: Add your control notification handler code here
UpdateData();
m_edit="";
if(m_ispwd)
TextCrypt();
else
FileCrypt();
}
void CDESDlg::OnSelchangeMode()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_mode==1)
((CWnd*)GetDlgItem(IDC_KEY2))->EnableWindow(TRUE);
else
((CWnd*)GetDlgItem(IDC_KEY2))->EnableWindow(FALSE);
}
void CDESDlg::TextCrypt()
{
CDES des;
CString errorstring;
UCHAR text[8];
UCHAR key[8]={0,0,0,0,0,0,0,0};
UCHAR key2[8]={0,0,0,0,0,0,0,0};
UCHAR *result;
memcpy(key,m_key.GetBuffer(8),m_key.GetLength());
memcpy(key2,m_key2.GetBuffer(8),m_key2.GetLength());
m_edit="";
try{
if (m_op.GetCurSel())
{//encrypt
if (m_plainText.IsEmpty() ||m_key.IsEmpty())
throw errorstring="明文或密钥为空";
m_cipherText="";
UpdateData(FALSE);
int plainLen=m_plainText.GetLength();
LPCSTR plainbuf=m_plainText.GetBuffer(plainLen);
while(plainLen>0)
{
memset(text,0,8);
memcpy(text,plainbuf,(plainLen>=8)?8:plainLen);
switch(m_mode){
case 0: //PURE DES
result=des.Crypto(text,key,m_op.GetCurSel());
break;
case 1: //3DES
result=des.TripleDES(text,key,key2,m_op.GetCurSel());
break;
case 2://DES with CBC
result=des.CBCDES(text,key,m_op.GetCurSel());
break;
}
CString s((char*)result,8);
m_cipherText+=s;
s.Format(" %x %x ",((ULONG*)result)[0],((ULONG*)result)[1]);
m_edit+=s;
UpdateData(FALSE);
plainLen-=8;
plainbuf+=(plainbuf+8<m_plainText.GetBuffer(1)+m_plainText.GetLength())?8:(m_plainText.GetLength()%8);
}
MessageBox("成功加密");
}
else
{//decrypt
if (m_cipherText.IsEmpty() ||m_key.IsEmpty())
throw errorstring="密文或密钥为空";
m_plainText="";
UpdateData(FALSE);
int cipherLen=m_cipherText.GetLength();
LPCSTR cipherbuf=m_cipherText.GetBuffer(cipherLen);
while(cipherLen>0)
{
memset(text,0,8);
memcpy(text,cipherbuf,(cipherLen>=8)?8:cipherLen);
switch(m_mode){
case 0: //PURE DES
result=des.Crypto(text,key,m_op.GetCurSel());
break;
case 1: //3DES
result=des.TripleDES(text,key,key2,m_op.GetCurSel());
break;
case 2://DES with CBC
result=des.CBCDES(text,key,m_op.GetCurSel());
break;
}
CString s((char*)result,8);
m_plainText+=s;
m_edit+=s;
UpdateData(FALSE);
cipherLen-=8;
cipherbuf+=(cipherbuf+8<m_cipherText.GetBuffer(1)+m_cipherText.GetLength())?8:(m_cipherText.GetLength()%8);
}
MessageBox("成功解密");
}
}
catch(CString e){
MessageBox(errorstring,"错误:文本加密");
return;
}
}
void CDESDlg::FileCrypt()
{
CDES des;
CFile plain,cipher;
CString errorstring;
try{
if (m_plainText.IsEmpty() || m_cipherText.IsEmpty() || m_key.IsEmpty())
throw errorstring="明文路径,密文路径,或密钥为空";
plain.Open(m_plainText,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
cipher.Open(m_cipherText,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
if(plain.GetLength()==0 && m_op.GetCurSel()==1)
throw errorstring="明文文件长度为0,无法加密";
if( cipher.GetLength()==0 && m_op.GetCurSel()==0)
throw errorstring="密文文件长度为0,无法解密";
//get KEY
plain.SeekToBegin();
cipher.SeekToBegin();
UCHAR text[8],*result;
UCHAR key[8]={0,0,0,0,0,0,0,0};
UCHAR key2[8]={0,0,0,0,0,0,0,0};
memcpy(key,(UCHAR *)m_key.GetBuffer(8),m_key.GetLength());
memcpy(key2,(UCHAR *)m_key2.GetBuffer(8),m_key2.GetLength());
static UCHAR txtBuf[1024*1024];
static UCHAR cifBuf[1024*1024];
if (m_op.GetCurSel())
{//encrypt
cipher.Close();
cipher.Open(m_cipherText,CFile::modeCreate|CFile::modeWrite);
ULONG cipherfilesize[2];
cipherfilesize[0]=plain.GetLength();
cipherfilesize[1]=DES_CIPHER_TAG;
int inteval=cipherfilesize[0]/MAXRANGE_OF_PROGRESS;
m_prgs.SetStep(1);
int actrdcnt=0;
memset(txtBuf,0,1024*1024);
while((actrdcnt=plain.ReadHuge(txtBuf,1024*1024))>0){
memset(cifBuf,0,1024*1024);
int offset=0;
while(actrdcnt>0)
{memset(text,0,8);
memcpy(text,txtBuf+offset,(actrdcnt>=8)?8:actrdcnt);
switch(m_mode){
case 0: //PURE DES
result=des.Crypto(text,key,m_op.GetCurSel());
break;
case 1: //3DES
result=des.TripleDES(text,key,key2,m_op.GetCurSel());
break;
case 2://DES with CBC
result=des.CBCDES(text,key,m_op.GetCurSel());
break;
}//switch
memcpy(cifBuf+offset,result,8);
offset+=8;
actrdcnt-=8;
delete result;
(offset > inteval*(offset/inteval))?m_prgs.StepIt():0;
}//inner while
cipher.Write(cifBuf,offset);
memset(txtBuf,0,1024*1024);
}//while
cipher.Write(cipherfilesize,sizeof(cipherfilesize));
MessageBox("成功加密");
}
else
{//decrypt
plain.Close();
plain.Open(m_plainText,CFile::modeCreate|CFile::modeWrite);
long lengthblk[2];
cipher.Seek(-8,CFile::end);
cipher.Read(lengthblk,8);
cipher.SeekToBegin();
if (lengthblk[1]!=DES_CIPHER_TAG)
throw errorstring="DES 密文标记未发现,不是加密文件";
memset(cifBuf,0,1024*1024);
int actrdcnt=0;
while((actrdcnt=cipher.Read(cifBuf,1024*1024))>0 ){
int offset=0;
while(actrdcnt>0 && lengthblk[0]>0)
{
memset(text,0,8);
memcpy(text,cifBuf+offset,8);
switch(m_mode){
case 0: //PURE DES
result=des.Crypto(text,key,m_op.GetCurSel());
break;
case 1:
result=des.TripleDES(text,key,key2,m_op.GetCurSel());
break;
case 2:
result=des.CBCDES(text,key,m_op.GetCurSel());
break;
}
memcpy (txtBuf+offset,result,(lengthblk[0]>=8)?8:(const int)lengthblk[0]);
offset+=(lengthblk[0]>=8)?8:(const int)lengthblk[0];
lengthblk[0]-=8;
actrdcnt-=8;
delete result;
}//inner while
plain.Write(txtBuf,offset);
}//while
MessageBox("成功解密");
}
}
catch (CString e)
{
cipher.Close();
plain.Close();
MessageBox(e,"错误",MB_OK|MB_ICONSTOP);
return;
}
cipher.Close();
plain.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -