// DES加解密算法Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "DES加解密算法.h"
#include "DES加解密算法Dlg.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_MINGWEN =_T("");
m_MEWEN = _T("");
m_YUANWEN = _T("");
m_MIYAO = _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_EDIT1, m_MINGWEN);
DDV_MaxChars(pDX, m_MINGWEN, 8);
DDX_Text(pDX, IDC_EDIT2, m_MEWEN);
DDX_Text(pDX, IDC_EDIT3, m_YUANWEN);
DDX_Text(pDX, IDC_EDIT4, m_MIYAO);
DDV_MaxChars(pDX, m_MIYAO, 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_JIEMIBUTTON, OnJiemibutton)
ON_BN_CLICKED(IDC_JMBUTTON, OnJmbutton)
ON_BN_CLICKED(IDC_TCHBUTTON, OnTchbutton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDESDlg message handlers
BOOL CDESDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
m_MINGWEN="tsinghua";
m_MIYAO="computer";
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
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::OnJiemibutton()
{
// TODO: Add your control notification handler code here
DESJIEMI();
}
void CDESDlg::OnJmbutton()
{
// TODO: Add your control notification handler code here
UpdateData(true);
initial();
DESDIEDAI();
}
void CDESDlg::OnTchbutton()
{
// TODO: Add your control notification handler code here
exit(0);
}
void CDESDlg::Mingwenerjinzhi()
{
FILE *fp;
char ch;
static int i,j,str[8];
fp=fopen("明文二进制表示.dat","w");
for(i=0;i<8;i++)
{
ch=m_MINGWEN.GetAt(i);
for(j=0;j<8;j++)
{
str[j]=ch%2;
ch=ch/2;
}
for(j=7;j>=0;j--)
fprintf(fp,"%d\n",str[j]);
}
fclose(fp);
}
void CDESDlg::MIYAOERJINZHI()
{
FILE *fp;
char ch;
static int i,j,str[8];
fp=fopen("密钥二进制表示.dat","w");
for(i=0;i<8;i++)
{
ch=m_MIYAO.GetAt(i);
for(j=0;j<8;j++)
{
str[j]=ch%2;
ch=ch/2;
}
for(j=7;j>=0;j--)
fprintf(fp,"%d\n",str[j]);
}
fclose(fp);
}
void CDESDlg::initial()//生成明文的IP和密钥的16个子密钥
{
FILE *fp;
int m[65],m1[65],k[65],i,k0[57],C[57];
int C0[29],D0[29],C1[29],D1[29],C2[29],D2[29],C3[29],D3[29],C4[29],D4[29],C5[29],D5[29],C6[29],D6[29],C7[29],D7[29],C8[29],D8[29],C9[29],D9[29],C10[29],D10[29],C11[29],D11[29],C12[29],D12[29],C13[29],D13[29],C14[29],D14[29],C15[29],D15[29],C16[29],D16[29];
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};
int PC_1[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};
int PC_2[49]={14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32};
Mingwenerjinzhi();
MIYAOERJINZHI();
fp=fopen("明文二进制表示.dat","r");
for(i=1;i<=64;i++)
fscanf(fp,"%d\n",&m[i]);
fclose(fp);
fp=fopen("密钥二进制表示.dat","r");
for(i=1;i<=64;i++)
fscanf(fp,"%d\n",&k[i]);
fclose(fp);
for(i=1;i<=64;i++)
m1[i]=m[ip[i-1]];
for(i=1;i<33;i++)
L0[i]=m1[i];//明文左侧的初始化
for(i=33;i<=64;i++)
R0[i-32]=m1[i];//明文右侧的初始化
for(i=1;i<57;i++)//生成子密钥
k0[i]=k[PC_1[i-1]];
for(i=1;i<29;i++)
C0[i]=k0[i];
for(i=29;i<=56;i++)
D0[i-28]=k0[i];
for(i=1;i<28;i++)//循环左移一位
{
C1[i]=C0[i+1];
D1[i]=D0[i+1];
}
C1[28]=C0[1];
D1[28]=D0[1];
for(i=1;i<=28;i++)
{
C[i]=C1[i];
C[i+28]=D1[i];
}
for(i=1;i<=48;i++)
K1[i]=C[PC_2[i-1]];//生成子密钥k1
for(i=1;i<28;i++) //循环左移一位
{
C2[i]=C1[i+1];
D2[i]=D1[i+1];
}
C2[28]=C1[1];
D2[28]=D1[1];
for(i=1;i<=28;i++)
{
C[i]=C2[i];
C[i+28]=D2[i];
}
for(i=1;i<=48;i++)
K2[i]=C[PC_2[i-1]];//生成子密钥k2
for(i=1;i<27;i++)//循环左移两位
{
C3[i]=C2[i+2];
D3[i]=D2[i+2];
}
C3[27]=C2[1];
D3[27]=D2[1];
C3[28]=C2[2];
D3[28]=D2[2];
for(i=1
- 1
- 2
前往页