?? desdlg.cpp
字號:
// DESDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DES.h"
#include "DESDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//全局變量
bool M[64]; //明文分組
bool X[8][8]; //明文分組經IP置換后的得到的矩陣
bool Y[8][8]; //該矩陣經IP逆置換后的得到密文分組
bool C[64]; //密文分組
bool L[4][8],R[4][8]; //存放每輪迭代后的結果
bool KEY[8][8]; //密鑰
//初始置換函數
void Ip()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
X[i][j]=M[IP[i][j]-1];
}
}
}
//逆初始置換函數
void AdverseIp()
{
int line,row;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
line=(ADVERSEIP[i][j]-1)/8;
row=(ADVERSEIP[i][j]-1)%8;
C[i*8+j]=Y[line][row];
}
}
}
void LeftMove(bool A[8][7],int times)
{
int line,row;
bool temp;
for(int i=0;i<times;i++)
{
temp=A[0][0];
for(int j=0;j<8;j++)
{
for(int k=0;k<7;k++)
{
line=(j*7+k+1)/7%8;
row =(j*7+k+1)%7;
A[j][k]=A[line][row];
}
}
A[7][6]=A[3][6];
A[3][6]=temp;
}
}
void GetSubKey(bool SubKEY[16][8][7])
{
bool PCKEY[8][7]; //存放密鑰經置換1后的結果
bool LoopKEY[8][8]; //存放每次迭代后的子密鑰,用于下一次迭代的計算
for(int i=0;i<8;i++)
{
for(int j=0;j<7;j++)
{
LoopKEY[i][j]=KEY[i][j];
}
}
int line,row;
for(int count=0;count<16;count++)
{
for(i=0;i<8;i++)
{
for(int j=0;j<7;j++)
{
line=(PC_1[i][j]-1)/8;
row =(PC_1[i][j]-1)%8;
PCKEY[i][j]=LoopKEY[line][row];
}
}
//執行左移操作
LeftMove(PCKEY,LMoveTimes[count]);
//生成子密鑰
for(i=0;i<8;i++)
{
for(int j=0;j<6;j++)
{
line=(PC_2[i][j]-1)/7;
row =(PC_2[i][j]-1)%7;
SubKEY[count][i][j]=PCKEY[line][row];
}
}
for(i=0;i<8;i++)
{
for(int j=0;j<7;j++)
{
LoopKEY[i][j]=PCKEY[i][j];
}
}
}
}
void Loop(bool SubKEY[16][8][7],int count)
{
int line,row;
//拆分64位明文為L和R
for(int i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
//由于Li=Ri-1,所以此處便可求出Li
L[i][j]=X[i+4][j];
}
}
//擴充置換E
bool ExtendR[8][6];
for(i=0;i<8;i++)
{
for(int j=0;j<6;j++)
{
line=(E[i][j]-1)/8;
row =(E[i][j]-1)%8;
ExtendR[i][j]=L[line][row];
}
}
//將ExtendR和SubKEY進行異或操作
for(i=0;i<8;i++)
{
for(int j=0;j<6;j++)
{
if(ExtendR[i][j]==SubKEY[count][i][j])
{
ExtendR[i][j]=false;
}
else
{
ExtendR[i][j]=true;
}
}
}
//用S盒對ExtendR進行代換選擇
bool SR[4][8]; //用于存放S盒代換選擇后的結果
for(i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
SR[i][j]=0;
}
}
int s_result;
for(i=0;i<8;i++)
{
line=(int)ExtendR[i][0]*2+(int)ExtendR[i][5];
row =(int)ExtendR[i][1]*8+(int)ExtendR[i][2]*4+(int)ExtendR[i][3]*2+(int)ExtendR[i][4];
s_result=S[i][line][row];
for(int j=3;s_result!=0&&j>=0;j--)
{
SR[i/2][i%2*4+j]=(bool)(s_result%2);
s_result/=2;
}
}
//對S盒代換后的結果SR[4][8]進行P置換
bool PR[4][8];
for(i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
line=(P[i][j]-1)/8;
row =(P[i][j]-1)%8;
PR[i][j]=SR[line][row];
}
}
//對Li-1和PR進行異或操作
for(i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
if(X[i][j]==PR[i][j])
{
R[i][j]=false;
}
else
{
R[i][j]=true;
}
}
}
for(i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
X[i][j] =L[i][j];
X[i+4][j]=R[i][j];
}
}
}
/////////////////////////////////////////////////////////////////////////////
// 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)
// NOTE: the ClassWizard will add member initialization here
//}}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)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}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_ENCRIPT, OnEncript)
ON_BN_CLICKED(IDC_DECRIPT, OnDecript)
//}}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
CString str="0000000000000000000000000000000000000000000000000000000000000000";
SetDlgItemText(IDC_EN_M,str);
str="01001010011001011011010011001011100100100101010101100100";
SetDlgItemText(IDC_KEY,str);
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::OnEncript()
{
// TODO: Add your control notification handler code here
//獲取明文
CString m;
GetDlgItemText(IDC_EN_M,m);
int len;
len=m.GetLength();
if(len!=64)
{
MessageBox("明文長度不正確,請檢查!");
return ;
}
CString str="明文輸入不規范,請檢查!";
for(int i=0;i<64;i++)
{
if(m[i]=='1')
{
M[i]=true;
}
else if(m[i]=='0')
{
M[i]=false;
}
else
{
MessageBox(str);
}
}
//獲取密鑰
CString key;
GetDlgItemText(IDC_EN_KEY,key);
len=key.GetLength();
if(len!=56)
{
MessageBox("密鑰長度不正確,請檢查!");
return ;
}
str="密鑰輸入不規范,請檢查!";
for(i=0;i<56;i++)
{
if(key[i]=='1')
{
KEY[i/7][i%7]=true;
}
else if(key[i]=='0')
{
KEY[i/7][i%7]=false;
}
else
{
MessageBox(str);
}
}
//產生16個子密鑰
bool SubKEY[16][8][7];
GetSubKey(SubKEY);
//初始置換
Ip();
//16輪代換
for(i=0;i<16;i++)
{
Loop(SubKEY,i);
}
//給Y[8][8]賦值
for(i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
Y[i][j] =R[i][j];
Y[i+4][j]=L[i][j];
}
}
//逆初始置換,得到密文
AdverseIp();
//密文輸出
CString c="";
for(i=0;i<64;i++)
{
if(C[i])
{
c+="1";
}
else
{
c+="0";
}
}
SetDlgItemText(IDC_EN_C,c);
}
void CDESDlg::OnDecript()
{
// TODO: Add your control notification handler code here
//獲取明文
CString m;
GetDlgItemText(IDC_DE_C,m);
int len;
len=m.GetLength();
if(len!=64)
{
MessageBox("密文長度不正確,請檢查!");
return ;
}
CString str="密文輸入不規范,請檢查!";
for(int i=0;i<64;i++)
{
if(m[i]=='1')
{
M[i]=true;
}
else if(m[i]=='0')
{
M[i]=false;
}
else
{
MessageBox(str);
}
}
//獲取密鑰
CString key;
GetDlgItemText(IDC_DE_KEY,key);
len=key.GetLength();
if(len!=56)
{
MessageBox("密鑰長度不正確,請檢查!");
return ;
}
str="密鑰輸入不規范,請檢查!";
for(i=0;i<56;i++)
{
if(key[i]=='1')
{
KEY[i/7][i%7]=true;
}
else if(key[i]=='0')
{
KEY[i/7][i%7]=false;
}
else
{
MessageBox(str);
}
}
//產生16個子密鑰
bool SubKEY[16][8][7];
GetSubKey(SubKEY);
bool TKEY[16][8][7];
for(i=0;i<16;i++)
{
for(int j=0;j<8;j++)
{
for(int k=0;k<7;k++)
{
TKEY[15-i][j][k]=SubKEY[i][j][k];
}
}
}
//初始置換
Ip();
//16輪代換
for(i=0;i<16;i++)
{
Loop(TKEY,i);
}
//給Y[8][8]賦值
for(i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
Y[i][j] =R[i][j];
Y[i+4][j]=L[i][j];
}
}
//逆初始置換,得到密文
AdverseIp();
//密文輸出
CString c="";
for(i=0;i<64;i++)
{
if(C[i])
{
c+="1";
}
else
{
c+="0";
}
}
SetDlgItemText(IDC_DE_M,c);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -