?? 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)
key = _T("12345678");
m_runable=false;
m = _T("");
s = _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_key, key);
DDX_Text(pDX, IDC_m, m);
DDX_Text(pDX, IDC_s, s);
//}}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_go, Ongo)
ON_BN_CLICKED(IDC_back, Onback)
ON_BN_CLICKED(clear1, Onclear1)
ON_BN_CLICKED(clear2, Onclear2)
//}}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
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::Ongo()
{
// TODO: Add your control notification handler code here
KEY_BIO();
if(m_runable)
{
DES_ENCIPHER();
}
}
void CDESDlg::KEY_BIO()
{
UpdateData(TRUE);
if(key.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=key.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};
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];
}
}
//生成完畢
/************************************************************/
/**********/
/*加密過程*/
/**********/
s=""; //設置字符串變量為空
long len=m_StrSource.GetLength();
long count=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];
}
}
//迭代完畢
/************************************************************/
int t[65];
for(i=1;i<=32;i++)
{
t[i]=LE[i];
t[i+32]=RE[i];
}
for(i=1;i<=64;i++) //將密文進行IP逆置換
t1[i]=t[IP_REVERSE[i-1]];
for(i=1;i<=64;i++)
{
s=s+(char)(48+t1[i]); //48為字符0的ASCII碼
if(i%8==0) //逢8位空格
s=s+" ";
}
}//while
UpdateData(false);
MessageBox("加密已完成!","提示",MB_OK|MB_ICONINFORMATION);
}
void CDESDlg::SOURCE_BIO()
{
char ch;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -