📄 desdlg.cpp
字号:
// DESDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DES.h"
#include "DESDlg.h"
#include<math.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()
/////////////////////////////////////////////////////////////////////////////
// CDESDlg dialog
CDESDlg::CDESDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDESDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDESDlg)
m_StrKey = _T("");
m_strText = _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_Control(pDX, IDC_STATICTEXT, m_ctlText);
DDX_Control(pDX, IDC_KEYSTATIC, m_ctlText2);
DDX_Control(pDX, IDC_PROGRESS1, m_ctlProgress);
DDX_Text(pDX, IDC_KEY, m_StrKey);
DDV_MaxChars(pDX, m_StrKey, 8);
DDX_Text(pDX, IDC_EDITBOX, m_strText);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDESDlg, CDialog)
//{{AFX_MSG_MAP(CDESDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDM_SHIFT, OnShift)
ON_COMMAND(IDC_MFILESAVE, OnMfilesave)
ON_COMMAND(ID_SFILESAVE, OnSfilesave)
ON_COMMAND(ID_SFILEOPEN, OnSfileopen)
ON_COMMAND(ID_CFILEOPEN, OnCfileopen)
ON_COMMAND(IDC_DEFAULTFILL, OnDefaultfill)
ON_COMMAND(ID_EXIT, OnExit)
ON_COMMAND(ID_CLEAR, OnClear)
ON_COMMAND(ID_AUTHOR, OnAuthor)
ON_BN_CLICKED(IDC_CIPHERBUTTON, OnCipherbutton)
ON_BN_CLICKED(IDC_DECIPHERBUTTON, OnDecipherbutton)
ON_BN_CLICKED(IDC_EXITBUTTON, OnExitbutton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDESDlg message handlers
BOOL CDESDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
m_strText="##华中科技大学机械科学与工程学院信息与系统技术研究所##";
m_StrKey="software";
UpdateData(false);//注意,在初始化里改变默认设置,改完后一定要刷新
// 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
//decipher icon
/* m_decipherIcon.SubclassDlgItem(IDC_DECIPHERBUTTON,this);
m_decipherIcon.SetIcon(IDI_ICONDECIPHER);
m_decipherIcon.SetShade(CShadeButtonST::SHS_HARDBUMP);
//encipher icon
m_encipherBtn.SubclassDlgItem(IDC_CIPHERBUTTON,this);
m_encipherBtn.SetIcon(IDI_ENCIPHERICON);
m_encipherBtn.SetShade(CShadeButtonST::SHS_HARDBUMP); */
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::OnShift()
{
// TODO: Add your command handler code here
CMenu *m_lMenu;
m_lMenu=GetMenu();
if(m_seeable==false){
m_seeable=true;
((CEdit*)GetDlgItem(IDC_KEY))->SetPasswordChar(0);
m_lMenu->ModifyMenu(IDM_SHIFT,MF_STRING,IDM_SHIFT,"密钥不可见");
}
else{
m_seeable=false;
((CEdit*)GetDlgItem(IDC_KEY))->SetPasswordChar('*');
m_lMenu->ModifyMenu(IDM_SHIFT,MF_STRING,IDM_SHIFT,"密钥可见");
}
Invalidate(true);
}
void CDESDlg::OnMfilesave()
{
// TODO: Add your command handler code here
CFileDialog m_foDlg(FALSE,"des","new.des",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"DES文件(*.des)|*.des|文本文件(*.txt)|*.txt|数据文件(.dat)|*.dat|所有文件(*.*)|*.*||",
NULL);
FileSOpen(m_foDlg);
}
void CDESDlg::OnSfilesave()
{
// TODO: Add your command handler code here
CFileDialog m_foDlg(FALSE,"txt","new.txt",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"文本文件(*.txt)|*.txt|数据文件(.dat)|*.dat|所有文件(*.*)|*.*||",
NULL);
FileSOpen(m_foDlg);
}
void CDESDlg::OnSfileopen()
{
// TODO: Add your command handler code here
CFileDialog m_foDlg(TRUE,"txt","Demo.txt",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"文本文件(*.txt)|*.txt|数据文件(.dat)|*.dat|所有文件(*.*)|*.*||",
NULL);
FileOpen(m_foDlg);
}
void CDESDlg::OnCfileopen()
{
// TODO: Add your command handler code here
CFileDialog m_foDlg(TRUE,"des","Bio.des",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"DES文件(*.des)|*.des|文本文件(*.txt)|*.txt|数据文件(.dat)|*.dat|所有文件(*.*)|*.*||",
NULL);
FileOpen(m_foDlg);
}
void CDESDlg::OnDefaultfill()
{
// TODO: Add your command handler code here
m_strText="请在这里输入文本并开始编辑...";
UpdateData(FALSE);
}
void CDESDlg::OnExit()
{
// TODO: Add your command handler code here
OnOK();
}
void CDESDlg::OnClear()
{
// TODO: Add your command handler code here
m_strText="";
UpdateData(FALSE);
}
void CDESDlg::OnAuthor()
{
AfxMessageBox("中山大学软件学院\n\n软件工程00级 吴丹\n\n",MB_ICONINFORMATION);
}
void CDESDlg::OnCipherbutton()
{
// TODO: Add your control notification handler code here
KEY_BIO();
if(m_runable){
DES_ENCIPHER();
}
}
void CDESDlg::OnDecipherbutton()
{
// TODO: Add your control notification handler code here
KEY_BIO();
if(m_runable){
DES_DECIPHER();
}
}
void CDESDlg::OnExitbutton()
{
// TODO: Add your control notification handler code here
OnOK();
}
void CDESDlg::DES_DECIPHER()
{
int i,j,t2[65]; //用来存放经过IP初始置换后的密文
int IP_REVERSE[64]={40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25};
int ip[65]={58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7}; //IP初始置换表
int k0[57],
C0[29],D0[29];//C代表p56置换后的左半部分,D代表右半部分
//P56置换表
int P56[57]={57,49,41,33,25,17,9,1,58,50,42,34,26,18,
10,2,59,51,43,35,27,19,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,
14,6,61,53,45,37,29,21,13,5,28,20,12,4};
/*******************************************************/
//生成子密钥
for(i=1;i<57;i++)
k0[i]=Key[P56[i-1]];
for(i=1;i<29;i++)
C0[i]=k0[i];
for(i=29;i<=56;i++)
D0[i-28]=k0[i];
int CE[29],DE[29],Cx[29],Dx[29];
for(i=1;i<=28;i++){
CE[i]=C0[i];
DE[i]=D0[i];
}
for(int num=1;num<=16;num++){
DES_GenerateSubkey(CE, DE,
Cx, Dx,K,num);
for(i=1;i<=28;i++){
CE[i]=Cx[i];
DE[i]=Dx[i];
}
}
//生成完毕
/*******************************************************/
/**********/
/*开始解密*/
/**********/
m_ctlProgress.SetPos(0);
m_StrCiphered=m_strText;
m_StrSource="";
m_strText="";
long len=m_StrCiphered.GetLength();
long constlen=len;
if(len%88!=0&&len%88<85){ //密文不完整
MessageBox("密文不完整!","密文错",MB_OK|MB_ICONERROR);
m_runable=false;
}
else{
if(len%88!=0)
len+=(88-len%88);
int x=0;
while((len-=11*8)>=0){
for(i=1;i<=64;i++){
t1[i]=int(m_StrCiphered.GetAt(x)-'0');
if(t1[i]!=0&&t1[i]!=1){ //密文不合规范
MessageBox("密文不符合规范!","密文错",MB_OK|MB_ICONERROR);
m_runable=false;
break;
}
if(i%8==0)
x+=4;
else
x++;
}
if(m_runable==false)
break;
for(i=1;i<=64;i++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -