1、DES加密源代码/ DES加密系统Dlg.cpp : implementation file/#include math.h#include stdafx.h#include DES加密系统.h#include DES加密系统Dlg.h#include#includeSet_Table_Value.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public
2、CDialogpublic: 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/ Implementationprotected: /AFX_MSG(C
3、AboutDlg) /AFX_MSG DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD) /AFX_DATA_INIT(CAboutDlg) /AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CAboutDlg) /AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog) /AFX_MSG_MAP(CAbo
4、utDlg) / No message handlers /AFX_MSG_MAPEND_MESSAGE_MAP()/ CDESDlg dialogCDESDlg:CDESDlg(CWnd* pParent /*=NULL*/) : CDialog(CDESDlg:IDD, pParent) /AFX_DATA_INIT(CDESDlg) m_MINGWEN = _T(); m_KEY = _T(); m_MIWEN = _T(); m_YUANWEN = _T(); /AFX_DATA_INIT / Note that LoadIcon does not require a subseque
5、nt 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); DDX_Text(pDX, IDC_EDIT2, m_KEY); DDX_Text(pDX, IDC_EDIT3, m_MIWEN); DDX_Text(pDX, IDC_EDIT4,
6、m_YUANWEN); /AFX_DATA_MAPBEGIN_MESSAGE_MAP(CDESDlg, CDialog) /AFX_MSG_MAP(CDESDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, On_Encrypt) ON_BN_CLICKED(IDC_BUTTON2, On_Decrypt) /AFX_MSG_MAPEND_MESSAGE_MAP()/ CDESDlg message handlersBOOL CDESDlg:OnInitDialog() C
7、Dialog: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 AppendMenu(MF_SEPARATOR); pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); / Set the icon for this dialog. T
8、he framework does this automatically / when the applications 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 controlvoid CDESDlg:OnSysCommand(UI
9、NT 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 automat
10、ically 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
11、); 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 CDESD
12、lg:OnQueryDragIcon() return (HCURSOR) m_hIcon;void CDESDlg:On_Encrypt() /*点击加密按钮,执行加密操作 UpdateData(); GetDlgItemText(IDC_EDIT1,m_MINGWEN);/从明文框当中获得文本,并存于m_MINGWEN中 if(m_MINGWEN.GetLength()!=8)/明文长度不合法时 SetDlgItemText(IDC_EDIT1,请输入8位字符); UpdateData();/一定别忘了刷新框中内容 else /- /- GetDlgItemText(IDC_EDIT2,m
13、_KEY);/从密钥框当中获得文本,并存于m_KEY中 if(m_KEY.GetLength()!=8)/密钥位数不够时,清空密钥 SetDlgItemText(IDC_EDIT2,); UpdateData();/一定别忘了刷新框中内容 /break; else/密钥合法时开始加密 K1_Born();/经过压缩换位,生成48位子密钥K1 K2_Born(); K3_Born(); K4_Born(); K5_Born(); K6_Born(); K7_Born(); K8_Born(); K9_Born(); K10_Born(); K11_Born(); K12_Born(); K13_
14、Born(); K14_Born(); K15_Born(); K16_Born(); LR0_Born();/产生经初始换位的L0,R0 /DES_DIEDAI(L0,R0,K1);/密钥迭代过程 LR1_Born(); LR2_Born(); LR3_Born(); LR4_Born(); LR5_Born(); LR6_Born(); LR7_Born(); LR8_Born(); LR9_Born(); LR10_Born(); LR11_Born(); LR12_Born(); LR13_Born(); LR14_Born(); LR15_Born(); LR16_Born();/*
15、 */ /开始逆初始换位 int R_replace65,i,Result65;/64位原文结果存放于Result中 for(i=1;i33;i+) R_replacei=R16i; R_replacei+32=L16i; for(i=1;i65;i+) Resulti=R_replaceIP_1i-1; /* FILE *fp; fp=fopen(密文二进制表示.dat,w);/建立文件“密文二进制表示.dat” for(i=1;i65;i+) fprintf(fp,%dn,Resulti); /fputc(n,fp); fclose(fp);/下面准备把结果打入密文框 unsigned c
16、har char1,char2,char3,char4,char5,char6,char7,char8; char1=Result1*128+Result2*64+Result3*32+Result4*16+Result5*8+Result6*4+Result7*2+Result8; char2=Result9*128+Result10*64+Result11*32+Result12*16+Result13*8+Result14*4+Result15*2+Result16; char3=Result17*128+Result18*64+Result19*32+Result20*16+Resul
17、t21*8+Result22*4+Result23*2+Result24; char4=Result25*128+Result26*64+Result27*32+Result28*16+Result29*8+Result30*4+Result31*2+Result32; char5=Result33*128+Result34*64+Result35*32+Result36*16+Result37*8+Result38*4+Result39*2+Result40; char6=Result41*128+Result42*64+Result43*32+Result44*16+Result45*8+
18、Result46*4+Result47*2+Result48; char7=Result49*128+Result50*64+Result51*32+Result52*16+Result53*8+Result54*4+Result55*2+Result56; char8=Result57*128+Result58*64+Result59*32+Result60*16+Result61*8+Result62*4+Result63*2+Result64; char Result_str10=char1,char2,char3,char4,char5,char6,char7,char8;/将结果转化
19、为的字符串存入此集合 /printf(%sn,Result_str); CString s=Result_str; SetDlgItemText(IDC_EDIT3,s); UpdateData(); /输入都合法时执行 void CDESDlg:On_Decrypt() /*点击解密按钮,执行解密操作 /* UpdateData(); GetDlgItemText(IDC_EDIT3,m_MIWEN);/从密文框当中获得文本,并存于m_MIWEN中 if(m_MIWEN.GetLength()!=8)/密文长度不合法时 SetDlgItemText(IDC_EDIT3,请输入8位字符); U
20、pdateData();/一定别忘了刷新框中内容 else */ GetDlgItemText(IDC_EDIT2,m_KEY);/从密钥框当中获得文本,并存于m_KEY中 if(m_KEY.GetLength()!=8)/密钥位数不够时,清空密钥 SetDlgItemText(IDC_EDIT2,); UpdateData();/一定别忘了刷新框中内容 /break; else/密钥合法时开始解密 K1_Born();/经过压缩换位,生成48位子密钥K1 K2_Born(); K3_Born(); K4_Born(); K5_Born(); K6_Born(); K7_Born(); K8_
21、Born(); K9_Born(); K10_Born(); K11_Born(); K12_Born(); K13_Born(); K14_Born(); K15_Born(); K16_Born(); LR0_Encrypt();/输入的密文经初始换位的L0,R0 LR1_Decry(); LR2_Decry(); LR3_Decry(); LR4_Decry(); LR5_Decry(); LR6_Decry(); LR7_Decry(); LR8_Decry(); LR9_Decry(); LR10_Decry(); LR11_Decry(); LR12_Decry(); LR13_D
22、ecry(); LR14_Decry(); LR15_Decry(); LR16_Decry();/& /开始逆初始换位 int R_replace65,i,Result65;/64位原文结果存放于Result中 for(i=1;i33;i+) R_replacei=R16i; R_replacei+32=L16i; for(i=1;i65;i+) Resulti=R_replaceIP_1i-1; /下面准备把结果打入原文框 unsigned char char1,char2,char3,char4,char5,char6,char7,char8; char1=Result1*128+Res
23、ult2*64+Result3*32+Result4*16+Result5*8+Result6*4+Result7*2+Result8; char2=Result9*128+Result10*64+Result11*32+Result12*16+Result13*8+Result14*4+Result15*2+Result16; char3=Result17*128+Result18*64+Result19*32+Result20*16+Result21*8+Result22*4+Result23*2+Result24; char4=Result25*128+Result26*64+Resul
24、t27*32+Result28*16+Result29*8+Result30*4+Result31*2+Result32; char5=Result33*128+Result34*64+Result35*32+Result36*16+Result37*8+Result38*4+Result39*2+Result40; char6=Result41*128+Result42*64+Result43*32+Result44*16+Result45*8+Result46*4+Result47*2+Result48; char7=Result49*128+Result50*64+Result51*32
25、+Result52*16+Result53*8+Result54*4+Result55*2+Result56; char8=Result57*128+Result58*64+Result59*32+Result60*16+Result61*8+Result62*4+Result63*2+Result64; char Result_str10=char1,char2,char3,char4,char5,char6,char7,char8;/将结果转化为的字符串存入此集合 CString s=Result_str; SetDlgItemText(IDC_EDIT4,s); UpdateData(); / void CDESDlg:MINGWEN_ERJINZHI()/*明文二进制 FILE *fp; char ch; static int i,j,str8; fp=fopen(明文二进制表示.dat,w);/建立文件“明文二
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1