⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 desexampledlg.cpp

📁 网络安全编程之des加密算法实现有demo
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// DESEXAMPLEDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DESEXAMPLE.h"
#include "DESEXAMPLEDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "math.h"

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDESEXAMPLEDlg dialog

CDESEXAMPLEDlg::CDESEXAMPLEDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDESEXAMPLEDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDESEXAMPLEDlg)
	m_MIYAO = _T("");
	m_MIWEN = _T("");
	m_YUANWEN = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDESEXAMPLEDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDESEXAMPLEDlg)
	DDX_Control(pDX, IDC_EDIT_PASSWORD, m_PASSWORD);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_MIYAO);
	DDX_Text(pDX, IDC_EDIT_DE, m_MIWEN);
	DDX_Text(pDX, IDC_EDIT_EN, m_YUANWEN);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDESEXAMPLEDlg, CDialog)
	//{{AFX_MSG_MAP(CDESEXAMPLEDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_DE, OnButtonDe)
	ON_BN_CLICKED(IDC_BUTTON_EN, OnButtonEn)
	ON_BN_CLICKED(IDC_BUTTON_ENABLE, OnButtonEnable)
	ON_BN_CLICKED(IDC_BUTTON_EOPEN, OnButtonEopen)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDESEXAMPLEDlg message handlers

BOOL CDESEXAMPLEDlg::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
    enBool=FALSE;
	deBool=FALSE;
	GetDlgItem(IDC_BUTTON_EN)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON_DE)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON_ENABLE)->EnableWindow(0);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDESEXAMPLEDlg::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 CDESEXAMPLEDlg::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 CDESEXAMPLEDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDESEXAMPLEDlg::OnButtonDe() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	DESJIEMI();
	deBool=FALSE;
	GetDlgItem(IDC_BUTTON_EN)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON_DE)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON_ENABLE)->EnableWindow(1);
}

void CDESEXAMPLEDlg::OnButtonEn() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
    initial();
	DESDIEDAI();
	enBool=FALSE;
	GetDlgItem(IDC_BUTTON_EN)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON_DE)->EnableWindow(1);
}

void CDESEXAMPLEDlg::OnButtonEnable() 
{
	// TODO: Add your control notification handler code here
	m_PASSWORD.GetWindowText(m_MIYAO);
	int passwordLength,sourceLength;
	passwordLength=m_MIYAO.GetLength();
	sourceLength=m_YUANWEN.GetLength();
	if ( (enBool==FALSE && deBool==FALSE) || sourceLength!=8 )
	{
		MessageBox( "文件不合法,请重新打开文件!","提示",MB_OK | MB_ICONINFORMATION );
		return;
	}
	if ( (passwordLength==8) && (enBool==TRUE) )
	{
		GetDlgItem(IDC_BUTTON_EN)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON_ENABLE)->EnableWindow(0);
		return;
	}
	if ( (passwordLength==8) && (deBool==TRUE) )
	{
		GetDlgItem(IDC_BUTTON_DE)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON_ENABLE)->EnableWindow(0);
		return;
	}
	else
	{
		MessageBox( "密钥不能少于8位!","提示",MB_OK | MB_ICONINFORMATION );
		return;
	}
}

void CDESEXAMPLEDlg::OnButtonEopen() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	MessageBox( "所打开文本必须有且仅有8个英文字母!","提示",MB_OK | MB_ICONINFORMATION );
	source_string.Empty();	
	FileDialog();           //弹出打开文件对话框
	m_MIWEN.Empty();
	m_YUANWEN=source_string;
	enBool=TRUE;
	deBool=FALSE;
	GetDlgItem(IDC_BUTTON_ENABLE)->EnableWindow(1);
	UpdateData(false);
}

HBRUSH CDESEXAMPLEDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if ( (nCtlColor==CTLCOLOR_EDIT) ||
		 (nCtlColor==CTLCOLOR_DLG)  ||
		 (nCtlColor==CTLCOLOR_STATIC) )
	{
		pDC->SetBkMode (TRANSPARENT);
		if( nCtlColor==CTLCOLOR_STATIC )
			pDC->SetTextColor(RGB(0,0,255));
		hbr = m_BkBrush;
	}
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CDESEXAMPLEDlg::FileDialog(void)
{
	CString strFile_open;
	CStdioFile file_open;
	char cBuffer[8];
	//保存被打开文件的完整文本内容
	CString sFilter= "文本文档(*.txt)|*.txt|所有文件(*.*)|*.*|";
	//过滤文件类型
	CFileDialog dlgOpen(TRUE,0,0,OFN_LONGNAMES,(LPCTSTR)sFilter,this);
	//生成打开文件对话框实例
	if ( dlgOpen.DoModal()==IDOK )
	{
		strFile_open= dlgOpen.GetPathName(); //获得完整文件路径名
		file_open.Open( strFile_open,CFile::modeRead );//读方式打开文件
		file_open.SeekToBegin();           //指针移到文件头
		UINT nBytesRead= file_open.Read( cBuffer,1000 );//读文件并返回文件长度
		for ( int i=0;i<(signed int)nBytesRead;i++ )
		{
			source_string+=cBuffer[i];  //保存源文件时用source_string
		}
		
		file_open.Close();
	}
}



void CDESEXAMPLEDlg::initial()
{
	Mingwenerjinzhi();
	formKey();
}

void CDESEXAMPLEDlg::Mingwenerjinzhi()
{
	FILE *fp;
	char ch;
	static int i,j,str[8];
	m_YUANWEN=source_string;
	fp=fopen("二进制明文.txt","w");
	for(i=0;i<8;i++)
	{
		ch=m_YUANWEN.GetAt(i);
		if (ch==EOF) break;
		for(j=0;j<8;j++)
		{
			str[j]=((int)ch)%2;
			ch=(char)ch/2;
		}
		for(j=7;j>=0;j--)
		{
			fprintf(fp,"%d ",str[j]);
		}
	}

	fclose(fp);
}

void CDESEXAMPLEDlg::MIYAOERJINZHI()
{
	FILE *fp;
	char ch;
	static int i,j,str[8];
	fp=fopen("二进制密钥.txt","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 ",str[j]);
	}
	fclose(fp);
}

void CDESEXAMPLEDlg::formKey()
{
	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};
		
	MIYAOERJINZHI();
	FILE *fp;
	fp=fopen("二进制明文.txt","r");
	for(i=1;i<=64;i++)    
		fscanf(fp,"%d\t",&m[i]);
	fclose(fp);
    fp=fopen("二进制密钥.txt","r");
	for(i=1;i<=64;i++)    
		fscanf(fp,"%d\t",&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;i<=28;i++)
	{
		C[i]=C3[i];
		C[i+28]=D3[i];
	}
	for(i=1;i<=48;i++)
		K3[i]=C[PC_2[i-1]];//生成子密钥k3
    for(i=1;i<27;i++)//循环左移两位
	{
		C4[i]=C3[i+2];
		D4[i]=D3[i+2];
	}
	C4[27]=C3[1];
	D4[27]=D3[1];
	C4[28]=C3[2];
	D4[28]=D3[2];
	for(i=1;i<=28;i++)
	{
		C[i]=C4[i];
		C[i+28]=D4[i];
	}
	for(i=1;i<=48;i++)
		K4[i]=C[PC_2[i-1]];//生成子密钥k4
	for(i=1;i<27;i++)//循环左移两位
	{
		C5[i]=C4[i+2];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -