📄 desdlg.cpp
字号:
{
m=(m+1)%4;
if(!m) j++;
}
}
}
/********非常重要的成员函数:16轮变换的详细过程*********/
void CDESDlg::Encrypt_16(void){
int i=1;
DWORDLONG L[17],R[17],B[9],EK,PSB;
L[0]=m_dwlData>>32; //右移32位将m_dwlData各二进制位右移32位,移到右端的低位舍去,高位补0.
R[0]=m_dwlData&0xffffffff;//m_dwlData与0xffffffff按位与运算可使得高32位置为0(即高32位被清零)
/* 所以64位的中间数据m_dwlData左右两部分分为独立的32位数据,但L[0]与R[0]仍是DWORDLONG类型64位,只是其高32位被置为0罢了*/
//以下循环为:16轮变换的详细过程
for(i=1;i<=16;i++){
L[i]=R[i-1];
R[i-1]=Permutation(R[i-1],dwlData_Expansion,48); //Expansion R
EK=R[i-1]^m_dwl_K[i]; //扩展后的48位R[i-1]与该轮密钥m_dwl_K[i]异或
PSB=Generate_B(EK,B); //代换函数F(R,k)中S盒的作用及置换函数P产生32位输出赋给PSB
R[i]=L[i-1]^PSB;
}
//32位互换
R[16]<<=32;
m_dwlData=R[16]|L[16];
m_dwlData=Permutation(m_dwlData,dwlData_FP,64); //Inverse Initial Permutation IP-1
}
//进入S盒时的作用函数
DWORDLONG CDESDlg::Generate_B(DWORDLONG EKPara,DWORDLONG *block){
int i,m,n;
DWORDLONG tmp=0;
for(i=8;i>0;i--){
block[i]=EKPara&0x3f;
m=(int)(block[i]&0x20)>>4;
m|=block[i]&0x01;
n=(int)(block[i]<<1)>>2;
block[i]=Des_S[i][m][n];
EKPara>>=6;
}
for(i=1;i<=8;i++){
tmp|=block[i];
tmp<<=4;
}
tmp>>=4;
tmp=Permutation(tmp,dwlData_P,32); //P Permutation
return tmp;
}
/**********加密函数*********/
unsigned char * CDESDlg::EncryptData(unsigned char *block){
unsigned char *EncrytedData=new unsigned char(15);
m_dwlData=Char_to_Byte(block,0);
m_dwlData=Permutation(m_dwlData,dwlData_IP,64); //Initial Permutation IP
Encrypt_16(); //16轮变换的详细过程
/*加密后m_dwlData转化为字符串输出*/
DWORDLONG bit8=m_dwlData;
for(int i=0;i<8;i++){
EncrytedData[7-i]=(unsigned char)(bit8&0xff);
bit8>>=8;
}
EncrytedData[8]='\0';
return EncrytedData;
}
/**************解密函数***********/
unsigned char* CDESDlg::DecryptData(unsigned char *desData){
int i=1;
unsigned char *DescryptedData=new unsigned char(15);
DWORDLONG L[17],R[17],B[9],EK,PSB;
DWORDLONG dataPara;
dataPara=Char_to_Byte(desData,0);
dataPara=Permutation(dataPara,dwlData_IP,64);
R[16]=dataPara>>32;
L[16]=dataPara&0xffffffff;
for(i=16;i>=1;i--){
R[i-1]=L[i];
L[i]=Permutation(L[i],dwlData_Expansion,48);
EK=L[i]^m_dwl_K[i];
PSB=Generate_B(EK,B);
L[i-1]=R[i]^PSB;
}
L[0]<<=32;
dataPara=L[0]|R[0];
dataPara=Permutation(dataPara,dwlData_FP,64);
for(i=0;i<8;i++){
DescryptedData[7-i]=(unsigned char)(dataPara&0xff);
dataPara>>=8;
}
DescryptedData[8]='\0';
return DescryptedData;
}
void CDESDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDESDlg)
DDX_Text(pDX, IDC_EDIT_DATA, m_strData);
DDV_MaxChars(pDX, m_strData, 8);
DDX_Text(pDX, IDC_EDIT_KEY, m_strKey);
DDV_MaxChars(pDX, m_strKey, 8);
DDX_Text(pDX, IDC_EDIT_OUTPUT, m_strOutput);
DDV_MaxChars(pDX, m_strOutput, 8);
//}}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_BUTTON_ENCRYPT, OnButtonEncrypt)
ON_BN_CLICKED(IDC_BUTTON_DECRYPT, OnButtonDecrypt)
//}}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
CStatic*pWnd=(CStatic*)GetDlgItem(IDC_STATIC_OUTPUT);
CString str1;
str1="\n 姓名:林乃臻\n";
str1=str1+" 学号:20042205041\n";
pWnd->SetWindowText(str1);
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;
}
BOOL CDESDlg::IsValidate()
{
UpdateData();
m_strKey.TrimLeft();
if(m_strKey.IsEmpty()||strlen(m_strKey)<8){
MessageBox("密钥输入无效!"); return FALSE;
}
m_strData.TrimLeft();
if(m_strData.IsEmpty()){
MessageBox("明文/密文输入无效!"); return FALSE;
}
return TRUE;
}
void CDESDlg::OnButtonEncrypt()
{
if(!IsValidate()) return;
UpdateData();
CString str;
char _key[9];
unsigned char _data[9];
for(int i=0;i<8;i++)
{
_key[i]=m_strKey[i];
_data[i]=m_strData[i];
}
_key[8]='\0';
_data[8]='\0';
EncryptKey(_key);
// unsigned char *result=EncryptData((unsigned char*)_data);
m_strOutput=EncryptData((unsigned char*)_data);
str="密钥:"+m_strKey;
str=str+"\n明文:"+m_strData;
str=str+"\nAfter Encrypted:"+m_strOutput;
CStatic*pWnd=(CStatic*)GetDlgItem(IDC_STATIC_OUTPUT);
pWnd->SetWindowText(str);
UpdateData(FALSE);
}
void CDESDlg::OnButtonDecrypt()
{
if(!IsValidate()) return;
UpdateData();
CString str;
char _key[9];
unsigned char _data[9];
for(int i=0;i<8;i++)
{
_key[i]=m_strKey[i];
_data[i]=m_strData[i];
}
_key[8]='\0';
_data[8]='\0';
EncryptKey(_key);
unsigned char *result=DecryptData((unsigned char*)_data);
m_strOutput=result;
str="密钥:"+m_strKey;
str=str+"\n密文:"+m_strData;
str=str+"\nAfter Decrypted:"+m_strOutput;
CStatic*pWnd=(CStatic*)GetDlgItem(IDC_STATIC_OUTPUT);
pWnd->SetWindowText(str);
UpdateData(FALSE);
}
void CDESDlg::OnOK()
{
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -