📄 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_PROGRESS1, m_ctlProgress);
DDX_Control(pDX, IDC_KEYSTATIC, m_ctlText2);
DDX_Control(pDX, IDC_STATICTEXT, m_ctlText);
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(ID_EXIT, OnExit)
ON_BN_CLICKED(IDC_EXITBUTTON, OnExitbutton)
ON_COMMAND(ID_AUTHOR, OnAuthor)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_CIPHERBUTTON, OnCipherbutton)
ON_BN_CLICKED(IDC_DECIPHERBUTTON, OnDecipherbutton)
ON_COMMAND(ID_CLEAR, OnClear)
ON_COMMAND(IDC_DEFAULTFILL, OnDefaultfill)
ON_COMMAND(IDM_SHIFT, OnShift)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_SFILEOPEN, OnSfileopen)
ON_COMMAND(ID_CFILEOPEN, OnCfileopen)
ON_COMMAND(ID_SFILESAVE, OnSfilesave)
ON_COMMAND(IDC_MFILESAVE, OnMfilesave)
//}}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
m_ctlProgress.SetRange(0,100);
m_runable=true;
m_IsChinese=false;
m_seeable=false;
CBitmap m_bmp;
m_bmp.LoadBitmap(IDB_BITMAPBRUSH);
m_brush2.CreatePatternBrush(&m_bmp);
m_exitBtn.SubclassDlgItem(IDC_EXITBUTTON,this);
m_exitBtn.SetIcon(IDI_ICONEXIT,IDI_ICONEXIT2);
m_exitBtn.SetShade(CShadeButtonST::SHS_HARDBUMP);
//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);
m_brush.CreateSolidBrush(RGB(179,185,233 ));
setMyFont();
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();
}
}
HCURSOR CDESDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDESDlg::OnExit()
{
OnOK();
}
void CDESDlg::OnExitbutton()
{
OnOK();
}
void CDESDlg::OnAuthor()
{
AfxMessageBox("中山大学软件学院\n\n软件工程00级 吴丹\n\n",MB_ICONINFORMATION);
}
void CDESDlg::setMyFont()
{
m_font.CreateFont(32,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,
"隶书");
m_font2.CreateFont(18,0,0,0,FW_BOLD,0,0,0,DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,
"新宋体");
m_ctlText.SetFont(&m_font);
m_ctlText2.SetFont(&m_font2);
return;
}
HBRUSH CDESDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_DLG)
{
return (HBRUSH)m_brush2.GetSafeHandle();
}
else if(nCtlColor!=CTLCOLOR_EDIT)
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(25,115,152));
return (HBRUSH)m_brush2.GetSafeHandle();
}
// TODO: Return a different brush if the default is not desired
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
//以下为DES加密解密处理函数
void CDESDlg::SOURCE_BIO()
{
char ch;
static int i,j,str[8];
int r=0;
int len=m_strText.GetLength();
m_fSize=0;
m_StrSource="";
while(8*m_fSize<len){
for(i=0;i<8;i++)
{
if(8*m_fSize+i==len)
{
r=i;
break;
}
ch=m_strText.GetAt(8*m_fSize+i);
if(ch>0)
str[7]=0;
else{ //unicode字符
str[7]=1;
ch=0-ch;
}
for(j=0;j<7;j++)
{
str[j]=ch%2;
ch=ch/2;
}
for(j=7;j>=0;j--)
m_StrSource+=char(str[j]+'0');
}
m_fSize++;
if(m_fSize%50==0)
m_ctlProgress.SetPos(int(100*8*(m_fSize-1)/float(len)));
}//while
if(r)
for(i=0;i<(8-r)*8;i++)
m_StrSource+='0';
m_ctlProgress.SetPos(100);
}
void CDESDlg::KEY_BIO()
{
UpdateData(TRUE);
if(m_StrKey.GetLength()!=8){
MessageBox("密钥必须为八位","密钥错!",MB_ICONSTOP|MB_OK);
m_runable=false;
}
else{
m_runable=true;
char ch;
static int i,j,str[8];
for(i=0;i<8;i++)
{
ch=m_StrKey.GetAt(i);
for(j=0;j<8;j++)
{
str[j]=ch%2;
ch=ch/2;
}
for(j=7;j>=0;j--)
Key[i*8+j]=str[j];
}
}
}
void CDESDlg::DES_ENCIPHER()
{
int m[65], //用来存放二进制的明文
m1[65], //经过初始置换后的明文二进制
i,C0[29],D0[29],k0[57];
//IP初始置换表
int ip[64]={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 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};
//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};
m_ctlProgress.SetPos(0);
SOURCE_BIO();
/************************************************************/
//生成子密钥
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_strText=""; //设置字符串变量为空
long len=m_StrSource.GetLength();
long count=0;
m_ctlProgress.SetPos(0);
while((m_fSize--)>0){//while
for(i=1;i<=64;i++){
m[i]=(m_StrSource.GetAt(count)-'0');
count++;
}
for(i=1;i<=64;i++)
m1[i]=m[ip[i-1]];
for(i=1;i<=32;i++)
L0[i]=m1[i]; //明文左侧的初始化
for(i=33;i<=64;i++)
R0[i-32]=m1[i]; //明文右侧的初始化
/**************************************************/
//进行十六次迭代
int RE[33],LE[33],Lx[33],Rx[33];
for(i=1;i<=32;i++){
RE[i]=R0[i];
LE[i]=L0[i];
}
for(num=1;num<=16;num++){
DES_ITERATIVE(LE,RE,Lx,Rx,K[num]);
for(i=1;i<=32;i++){
LE[i]=Lx[i];
RE[i]=Rx[i];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -