📄 desdlg.cpp
字号:
// desDlg.cpp : implementation file
//
#include "stdafx.h"
#include "des.h"
#include "desDlg.h"
#include "mydes.h"
#include "FILEDES.h"
#include <string>
#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()
/////////////////////////////////////////////////////////////////////////////
// CDesDlg dialog
CDesDlg::CDesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDesDlg)
m_key = _T("");
m_Code = _T("");
m_Decode = _T("");
m_codefilepath = _T("");
m_decodefilepath = _T("");
m_testspeed = _T("");
m_help = _T("");
//}}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_Text(pDX, IDC_EDIT1, m_key);
DDV_MaxChars(pDX, m_key, 8);
DDX_Text(pDX, IDC_EDIT2, m_Code);
DDX_Text(pDX, IDC_EDIT3, m_Decode);
DDX_Text(pDX, IDC_EDIT4, m_codefilepath);
DDX_Text(pDX, IDC_EDIT5, m_decodefilepath);
DDX_Text(pDX, IDC_EDIT6, m_testspeed);
DDX_Text(pDX, IDC_EDIT7, m_help);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDesDlg, CDialog)
//{{AFX_MSG_MAP(CDesDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Encrypt, OnEncrypt)
ON_BN_CLICKED(IDC_ClearAll, OnClearAll)
ON_BN_CLICKED(IDC_BUTTON4, OnCLEARCODE)
ON_BN_CLICKED(IDC_CLEARDECODE, OnCleardecode)
ON_BN_CLICKED(IDC_DeEncrypt, OnDeEncrypt)
ON_BN_CLICKED(IDC_SAVECODETOFLIE, OnSavecodetoflie)
ON_BN_CLICKED(IDC_CODEPATH, OnCodepath)
ON_BN_CLICKED(IDC_FILEENCRYPT, OnFileencrypt)
ON_BN_CLICKED(IDC_DECODEPATH, OnDecodepath)
ON_BN_CLICKED(IDC_FILEDEENCRYPT, OnFiledeencrypt)
//}}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
// TODO: Add extra initialization here
m_help="Tips:\r\n1.本程序可以加密1-4096个字符的任意长度字符串\r\n2.密钥长度不能过短或者为空\r\n3.加密结果将用16进制字符显示\r\n4.本加密程序同时支持中/英文字符(包括密钥和明文)\r\n5.本加密程序支持特殊字符[例如换行符(回车键)]\r\n\r\nAttention:\r\n1.加密时,密文的最后一个字符请不要使用半角美元符号'$'\r\n2.字符串加密最多支持加密4096个英文字符(解密\r\n最多为8192个十六进制字符),如果需要加密的内容过多,请使用文件加密\r\n3.为了保证您的数据安全,建议不要使用默认密钥,并保持您的密钥在八位以上\r\n4.如果您有兴趣可以改写源码为3个密钥的\r\n";
UpdateData(false);
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::OnEncrypt()
{
// TODO: Add your control notification handler code here
UpdateData();
myDES* my_des = new myDES();
if (m_key.IsEmpty()) return;
my_des->InitializeKey((LPCSTR)m_key);
char temp[32] = {0};
my_des->EncryptAnyLength((LPCSTR)m_Code);
CString s1,s2;
s1.Format("%s",my_des->GetCiphertextAnyLength());
GetDlgItem(IDC_EDIT3)->SetWindowText(s1);
}
void CDesDlg::OnClearAll()
{
// TODO: Add your control notification handler code here
m_Code.Empty();
m_key.Empty();
m_Decode.Empty();
UpdateData(false);
}
void CDesDlg::OnCLEARCODE()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_EDIT2)->SetWindowText("");
GetDlgItem(IDC_EDIT2)->Invalidate();
// m_Code="";
// UpdateData(false);
}
void CDesDlg::OnCleardecode()
{
// TODO: Add your control notification handler code here
CEdit* edit3=(CEdit*)GetDlgItem(IDC_EDIT3);
//edit3->SetSel(0,-1);
//edit3->Clear();
//edit3->SetWindowText("");
GetDlgItem(IDC_EDIT3)->SetWindowText("");
GetDlgItem(IDC_EDIT3)->Invalidate();
// m_Decode.Empty();
// UpdateData(false);
}
void CDesDlg::OnDeEncrypt()
{
// TODO: Add your control notification handler code here
UpdateData();
myDES* my_des = new myDES();
if (m_key.IsEmpty()) return;
my_des->InitializeKey((LPCSTR)m_key);
char temp[32] = {0};
CString s2;
my_des->DecryptAnyLength((LPCSTR)m_Decode);
s2.Format("%s",my_des->GetPlaintextAnyLength());
GetDlgItem(IDC_EDIT2)->SetWindowText(s2);
}
void CDesDlg::OnSavecodetoflie()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
CString m_savefile;
m_savefile=dlg.GetPathName();
UpdateData(TRUE);
HANDLE hfile;
hfile=CreateFile(m_savefile,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwWrites;
WriteFile(hfile,m_Decode,strlen(m_Decode),&dwWrites,NULL);
CloseHandle(hfile);
}
void CDesDlg::OnCodepath()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_decodefilepath="";
UpdateData(false);
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
m_codefilepath=dlg.GetPathName();
if (m_decodefilepath=="")
{m_decodefilepath=m_codefilepath+".des";
GetDlgItem(IDC_EDIT5)->SetWindowText(m_decodefilepath);
}
UpdateData(FALSE);
}
void CDesDlg::OnFileencrypt()
{
// TODO: Add your control notification handler code here
/* UpdateData(TRUE);
if(m_codefilepath=="")return;
CFile ff(m_codefilepath,CFile::modeRead);
DWORD dwFileLen=ff.GetLength();
char *pBuf;
/* CFile fp(m_decodefilepath,CFile::modeCreate|CFile::modeWrite);*/
/*CTime tm1,tm2;
tm1=CTime::GetCurrentTime();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
ff.Read(pBuf,dwFileLen);
ff.Close();
//MessageBox(pBuf);
CString s=pBuf;
FileEncrypt(s);
tm2=CTime::GetCurrentTime();
CTimeSpan tmSpan=tm2-tm1;
CString strspan=tmSpan.Format("Seconds:%S");
m_testspeed=strspan+"s";
// fp.Close();
GetDlgItem(IDC_EDIT6)->SetWindowText(m_testspeed);
UpdateData(FALSE);*/
//AfxMessageBox("加密成功!");*/
////////////////////////////////////////////////////////////////
//-------------------------------------------------
UpdateData(TRUE);
if (m_key=="") {MessageBox("请输入密钥");return;
}
if(m_codefilepath=="")return;
CFile ff(m_codefilepath,CFile::modeRead);
long lFileLen=ff.GetLength();
long c=lFileLen/8;
long d=lFileLen%8;
if (m_codefilepath=="")
m_decodefilepath=m_codefilepath+".aen";
CFile fp(m_decodefilepath,CFile::modeCreate|CFile::modeWrite);
fp.SeekToBegin();
ff.SeekToBegin();
//CString inbuff;
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));
}*/
memcpy(skey,(LPCTSTR)m_key,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), 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);
}
tm2=CTime::GetCurrentTime();
CTimeSpan tmSpan=tm2-tm1;
CString strspan=tmSpan.Format("Seconds:%S");
m_testspeed=strspan+"s";
ff.Close();
fp.Close();
AfxMessageBox("加密成功!");
UpdateData(FALSE);
}
void CDesDlg::FileEncrypt(CString file)
{
myDES* my_des = new myDES();
my_des->InitializeKey((LPCSTR)m_key);
char temp[32] = {0};
my_des->EncryptAnyLength((LPCSTR)file);
CString s1;
s1.Format("%s",my_des->GetCiphertextAnyLength());
CString path;
CodeToFile(s1,m_decodefilepath);
}
void CDesDlg::CodeToFile(CString s1,CString path)
{HANDLE hfile;
hfile=CreateFile(path,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwWrites;
WriteFile(hfile,s1,strlen(s1),&dwWrites,NULL);
CloseHandle(hfile);
}
void CDesDlg::OnDecodepath()
{
// 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_decodefilepath=dlg.GetPathName();
UpdateData(FALSE);/*All Files(*.*)|*.*||"//All Files(*.*)\0*.**/
}
void CDesDlg::OnFiledeencrypt()
{
// TODO: Add your control notification handler code here
/*UpdateData(TRUE);
if(m_codefilepath=="")return;
CFile fp(m_decodefilepath,CFile::modeRead);
DWORD dwFileLen=fp.GetLength();
char *pBuf;
/* CFile fp(m_decodefilepath,CFile::modeCreate|CFile::modeWrite);*/
/* CTime tm1,tm2;
tm1=CTime::GetCurrentTime();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
fp.Read(pBuf,dwFileLen);
fp.Close();
//MessageBox(pBuf);
CString codedfile=pBuf;
FileDeEncrypt(codedfile);*/
// fp.Close();
/* GetDlgItem(IDC_EDIT6)->SetWindowText(m_testspeed);
UpdateData(FALSE);*/
// AfxMessageBox("解密成功!");
UpdateData(TRUE);
if (m_key=="") {MessageBox("请输入密钥");return;
}
if(m_codefilepath=="")return;
CFile ff(m_decodefilepath,CFile::modeRead);
long lFileLen=ff.GetLength();
long c=lFileLen/8;
long d=lFileLen%8;
CFile fp(m_codefilepath,CFile::modeCreate|CFile::modeWrite);
fp.SeekToBegin();
ff.SeekToBegin();
//CString inbuff;
DES jm;
char inbuff[8],oubuff[8],skey[8];
memcpy(skey,(LPCTSTR)m_key,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);
}
if(d>0)
{
AfxMessageBox("文件可能已损坏或非经des加密过");
}
tm2=CTime::GetCurrentTime();
CTimeSpan tmSpan=tm2-tm1;
CString strspan=tmSpan.Format("Seconds:%S");
m_testspeed=strspan+"s";
ff.Close();
fp.Close();
AfxMessageBox("解密成功!");
UpdateData(FALSE);
}
void CDesDlg::FileDeEncrypt(CString file)
{ UpdateData();
myDES* my_des = new myDES();
my_des->InitializeKey((LPCSTR)m_key);
char temp[32] = {0};
CString s2;
my_des->DecryptAnyLength((LPCSTR)file);
s2.Format("%s",my_des->GetPlaintextAnyLength());
CodeToFile(s2,m_codefilepath);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -