📄 desdemodlg.cpp
字号:
// DesDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DesDemo.h"
#include "DesDemoDlg.h"
#include "FileCryDlg.h"
#include "GaussRandom.h"
#include "VerifyDlg.h"
#include "Des.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()
/////////////////////////////////////////////////////////////////////////////
// CDesDemoDlg dialog
CDesDemoDlg::CDesDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDesDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDesDemoDlg)
m_nCrackMode = 0;
m_bLog = TRUE;
m_CrackTimes = 0;
m_nDesMode = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pszPlainText = NULL;
m_pszCry = NULL;
m_pszKey = NULL;
m_nLength = 0;
m_bCracking = FALSE;
m_pCrackThread = NULL;
ZeroMemory(&m_CrackThreadData,sizeof(m_CrackThreadData));
}
void CDesDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDesDemoDlg)
DDX_Radio(pDX, IDC_RADIO1, m_nCrackMode);
DDX_Check(pDX, IDC_CHECK_RECORDE, m_bLog);
DDX_Text(pDX, IDC_EDIT_TIMES, m_CrackTimes);
DDV_MinMaxUInt(pDX, m_CrackTimes, 0, 100);
DDX_Radio(pDX, IDC_RADIO_ONE, m_nDesMode);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDesDemoDlg, CDialog)
//{{AFX_MSG_MAP(CDesDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_ENC, OnButtonEnc)
ON_BN_CLICKED(IDC_BUTTON_DECODE, OnButtonDecode)
ON_BN_CLICKED(IDC_BUTTON_CRACK, OnButtonCrack)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_FILECRY, OnButtonFilecry)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
ON_BN_CLICKED(IDC_BUTTON_VERIFY, OnButtonVerify)
ON_BN_CLICKED(IDC_RADIO_ONE, OnRadioOne)
ON_BN_CLICKED(IDC_RADIO_THREE, OnRadioThree)
ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDesDemoDlg message handlers
BOOL CDesDemoDlg::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
GetDlgItem(IDC_EDIT_CRACKFILE)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_VERIFY)->EnableWindow(FALSE);
((CComboBox*)GetDlgItem(IDC_COMBO_ROUND))->SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDesDemoDlg::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 CDesDemoDlg::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 CDesDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDesDemoDlg::OnButtonEnc()
{
int nLen = GetDlgItem(IDC_EDIT_PLAINTEXT)->GetWindowTextLength();
while((nLen%8) != 0)
nLen++;
if(m_pszPlainText)
delete []m_pszPlainText;
if(m_pszCry)
delete m_pszCry;
if(m_pszKey)
delete m_pszKey;
m_pszPlainText = new TCHAR[nLen+1];
m_pszCry = new TCHAR[nLen+1];
m_pszKey = new TCHAR[9];
m_nLength = nLen;
memset(m_pszPlainText,0,nLen+1);
memset(m_pszCry,0,nLen+1);
GetDlgItem(IDC_EDIT_PLAINTEXT)->GetWindowText(m_pszPlainText,nLen+1);
GetDlgItem(IDC_EDIT_KEY)->GetWindowText(m_pszKey,9);
int curSel = ((CComboBox*)GetDlgItem(IDC_COMBO_ROUND))->GetCurSel();
CString sRound;
((CComboBox*)GetDlgItem(IDC_COMBO_ROUND))->GetLBText(curSel,sRound);
int nRound = 3;
sscanf((LPTSTR)(LPCTSTR)sRound,"%d",&nRound);
CDes des(nRound);
des.Encrypt(m_pszCry,m_pszPlainText,nLen,m_pszKey,8);
GetDlgItem(IDC_EDIT_CRY)->SetWindowText(m_pszCry);
}
void CDesDemoDlg::OnButtonDecode()
{
if(m_nLength == 0)
return ;
TCHAR* szDest;
szDest = new TCHAR[m_nLength+1];
memset(szDest,0,m_nLength+1);
//GetDlgItem(IDC_EDIT_KEY)->GetWindowText(szKey,8+1);
int curSel = ((CComboBox*)GetDlgItem(IDC_COMBO_ROUND))->GetCurSel();
CString sRound;
((CComboBox*)GetDlgItem(IDC_COMBO_ROUND))->GetLBText(curSel,sRound);
int nRound = 3;
sscanf((LPTSTR)(LPCTSTR)sRound,"%d",&nRound);
CDes des(nRound);
des.Decode(szDest,m_pszCry,m_nLength,m_pszKey,8);
GetDlgItem(IDC_EDIT_PLAINTEXT2)->SetWindowText(szDest);
delete[] szDest;
}
void CDesDemoDlg::OnButtonCrack()
{
UpdateData(TRUE);
if(m_bCracking == FALSE)
{
StartCrack();
}
else
{
m_CrackThreadData.bKill = TRUE;
}
}
void CDesDemoDlg::StartCrack()
{
//m_CrackTheadData.szKeyOrg
m_CrackThreadData.nCrackTimes = m_CrackTimes;
m_CrackThreadData.pCrackWnd = this;
m_CrackThreadData.bLogIt = m_bLog;
m_CrackThreadData.bSuccess = FALSE;
m_CrackThreadData.bKill = FALSE;
m_bCracking = TRUE;
GetDlgItem(IDC_BUTTON_CRACK)->SetWindowText("停止攻击");
GetDlgItem(IDC_BUTTON_VERIFY)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_KEYORG)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO1)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_TIMES)->EnableWindow(FALSE);
GetDlgItem(IDC_SPIN1)->EnableWindow(FALSE);
GetDlgItem(IDC_CHECK_RECORDE)->EnableWindow(FALSE);
m_bCracking = TRUE;
m_pCrackThread = AfxBeginThread(CrackProc,&m_CrackThreadData);
}
void CDesDemoDlg::EndCrack()
{
GetDlgItem(IDC_BUTTON_VERIFY)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_KEYORG)->EnableWindow(TRUE);
GetDlgItem(IDC_RADIO1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_TIMES)->EnableWindow(TRUE);
GetDlgItem(IDC_SPIN1)->EnableWindow(TRUE);
GetDlgItem(IDC_CHECK_RECORDE)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_CRACK)->SetWindowText("攻击");
if(m_CrackThreadData.bSuccess)
{
char cKey[9];
memcpy(cKey,m_CrackThreadData.szKeyCracked,8);
cKey[8] = '\0';
GetDlgItem(IDC_EDIT_KEYCRACKED)->SetWindowText(cKey);//m_CrackThreadData.szKeyCracked);
}
else
{
GetDlgItem(IDC_BUTTON_VERIFY)->EnableWindow(TRUE);
AfxMessageBox("攻击失败!");
}
m_pCrackThread = NULL;
m_bCracking = FALSE;
}
void CDesDemoDlg::OnDestroy()
{
CDialog::OnDestroy();
if(m_pCrackThread)
{
DWORD dwRet = WaitForSingleObject(m_pCrackThread->m_hThread,500);
if(dwRet == WAIT_TIMEOUT)
TerminateThread(m_pCrackThread->m_hThread,0);
}
if(m_pszPlainText)
delete []m_pszPlainText;
if(m_pszCry)
delete m_pszCry;
if(m_pszKey)
delete m_pszKey;
}
void CDesDemoDlg::OnButtonFilecry()
{
CFileCryDlg dlg;
dlg.DoModal();
}
void CDesDemoDlg::OnRadio1()
{
GetDlgItem(IDC_EDIT_CRACKFILE)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(FALSE);
}
void CDesDemoDlg::OnRadio2()
{
GetDlgItem(IDC_EDIT_CRACKFILE)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(TRUE);
}
BOOL CDesDemoDlg::GenerateCrackFile()
{
if(GetDlgItem(IDC_EDIT_KEYORG)->GetWindowTextLength() == 0)
{
AfxMessageBox("请输入密钥!");
return FALSE;
}
char szKey[9];
GetDlgItem(IDC_EDIT_KEYORG)->GetWindowText(szKey,9);
if(m_nCrackMode == 0)
{
m_difCrack.GenerateCrackData(25,szKey,8);
GetDlgItem(IDC_BUTTON_CRACK)->EnableWindow(TRUE);
}
else
{
FILE* fileSrc = NULL,*fileDest =NULL;
CString strSrc,strDest;
GetDlgItem(IDC_EDIT_CRACKFILE)->GetWindowText(strSrc);
strDest = "crackdata.dat";
try
{
char szSrc[64];
char szDest[64];
CDes des(3);
fileSrc = fopen(strSrc,"r");
if(!fileSrc)
{
CString str = "打开文件:";
str +=strSrc;
AfxMessageBox(str);
}
fileDest = fopen(strDest,"w+");
if(!fileDest)
{
CString str = "打开文件:";
str +=strDest;
AfxMessageBox(str);
}
while(!feof(fileSrc))
{
unsigned int nRead = fread(szSrc,sizeof(char),64,fileSrc);
if(nRead == 0)
break;
if((nRead<64)&&(nRead>0))
memset(szSrc+nRead,0,64-nRead);
des.Encrypt(szDest,szSrc,64,szKey,8);
fwrite(szSrc,sizeof(char),64,fileDest);
fwrite(szDest,sizeof(char),64,fileDest);
}
fclose(fileSrc);
fclose(fileDest);
GetDlgItem(IDC_BUTTON_CRACK)->EnableWindow(TRUE);
}
catch(char* pE)
{
if(fileSrc)
fclose(fileSrc);
if(fileDest)
fclose(fileDest);
AfxMessageBox(pE);
return FALSE;
}
}
return TRUE;
}
void CDesDemoDlg::OnButtonBrowse()
{
CFileDialog file(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"All Files (*.*)|*.*||",NULL);
if(file.DoModal() == IDOK)
{
CString strFile = file.GetPathName();
GetDlgItem(IDC_EDIT_CRACKFILE)->SetWindowText(strFile);
}
}
void CDesDemoDlg::OnButtonVerify()
{
CVerifyDlg dlg;
GetDlgItem(IDC_EDIT_KEYORG)->GetWindowText(dlg.m_cOrgKey,9);
GetDlgItem(IDC_EDIT_KEYCRACKED)->GetWindowText(dlg.m_cCrackKey,9);
dlg.DoModal();
}
UINT CDesDemoDlg::CrackProc(LPVOID pData)
{
CrackThreadData* pCrackData = (CrackThreadData*)pData;
CDesDemoDlg* pDlg = (CDesDemoDlg*)pCrackData->pCrackWnd;
CDifCrack difCrack;
difCrack.LogIt(pCrackData->bLogIt);
if(pDlg->m_nCrackMode == 0)
{
char chKey[8];
int nLen = 0;
int nTimes = 0;
bool bGetIt = false;
while(1)
{
if(pCrackData->bKill)
break;
if(pCrackData->nCrackTimes > 0)
nTimes++;
pDlg->GenerateCrackFile();
if(difCrack.ReadCrackData())
difCrack.CrackIt();
if(difCrack.GetKey(chKey,nLen,3))//破解成功
{
bGetIt = true;
break;
}
else
{
if((pCrackData->nCrackTimes > 0)&&(nTimes >= pCrackData->nCrackTimes))
break;
else
continue;
}
}
if(bGetIt)
{
memcpy(pCrackData->szKeyCracked,chKey,8);
pCrackData->bSuccess = TRUE;
}
else pCrackData->bSuccess = FALSE;
}
else
{
if(pDlg->GenerateCrackFile() == FALSE)
{
pCrackData->bSuccess = FALSE;
return 0L;
}
if(difCrack.ReadCrackData())
difCrack.CrackIt();
char chKey[8];
int nLen = 0;
pCrackData->bSuccess = difCrack.GetKey(chKey,nLen,3);
if(pCrackData->bSuccess)
memcpy(pCrackData->szKeyCracked,chKey,8);
}
pDlg->EndCrack();
return 0L;
}
void CDesDemoDlg::OnRadioOne()
{
GetDlgItem(IDC_COMBO_ROUND)->EnableWindow(TRUE);
}
void CDesDemoDlg::OnRadioThree()
{
GetDlgItem(IDC_COMBO_ROUND)->EnableWindow(FALSE);
}
void CDesDemoDlg::OnButtonAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -