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

📄 sdesdlg.cpp

📁 简单的DES加密解密模拟程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// sdesDlg.cpp : implementation file
//
#include "iostream.h"
#include "stdlib.h"
#include "stdafx.h"
#include "sdes.h"
#include "sdesDlg.h"
#include "fstream.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CString mwpathName;
CString miwpathName;
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()

/////////////////////////////////////////////////////////////////////////////
// CSdesDlg dialog

CSdesDlg::CSdesDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSdesDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSdesDlg)
	m_mwfilepath = _T("");
	m_miwfilepath = _T("");
	m_key = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSdesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSdesDlg)
	DDX_Text(pDX, IDC_EDIT1, m_mwfilepath);
	DDX_Text(pDX, IDC_EDIT2, m_miwfilepath);
	DDX_Text(pDX, IDC_EDIT3, m_key);
	DDV_MinMaxInt(pDX, m_key, 0, 1023);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSdesDlg, CDialog)
	//{{AFX_MSG_MAP(CSdesDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, mwlocation)
	ON_BN_CLICKED(IDC_BUTTON2, miwlocation)
	ON_BN_DOUBLECLICKED(IDC_BUTTON1, OnDoubleclickedButton1)
	ON_BN_CLICKED(IDC_BUTTON4, jiami)
	ON_BN_CLICKED(IDC_BUTTON3, jiemi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSdesDlg message handlers

BOOL CSdesDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CSdesDlg::mwlocation() 
{
	// TODO: Add your control notification handler code here
   CFileDialog mwlocation(TRUE,NULL,"",OFN_HIDEREADONLY,"ALL Files(*.*)|*.*||",NULL);
   mwlocation.m_ofn.lpstrTitle="明文位置";
     if(mwlocation.DoModal()==IDOK)
   {  mwpathName=mwlocation.GetPathName();
     SetDlgItemText(IDC_EDIT1,mwpathName);
   m_mwfilepath=mwpathName;
   UpdateData(FALSE);
   
   }
}

void CSdesDlg::miwlocation() 
{
	// TODO: Add your control notification handler code here
   CFileDialog miwlocation(TRUE,NULL,"",OFN_HIDEREADONLY,"ALL Files(*.*)|*.*||",NULL);
   miwlocation.m_ofn.lpstrTitle="密文位置";
     if(miwlocation.DoModal()==IDOK)
   {  miwpathName=miwlocation.GetPathName();
     SetDlgItemText(IDC_EDIT2,miwpathName);
   m_miwfilepath=miwpathName;
   UpdateData(FALSE);
   
   }
}

void CSdesDlg::OnDoubleclickedButton1() 
{
	// TODO: Add your control notification handler code here
	
}


void CSdesDlg::jiami() // 加密函数
{fstream fin,fout;
UpdateData(true);
 if(m_mwfilepath=="")
 {AfxMessageBox("请选择明文文件");
 }
 else if(m_miwfilepath=="")
 {AfxMessageBox("请选择密文文件");
 }
 else{
 char ch;
 fin.open(m_mwfilepath,ios::in|ios::binary|ios::nocreate);
 if(!fin) {AfxMessageBox("打开明文文件失败!文件不存在或被保护!");
 exit(1);}
 //UpdateData(true);
 fout.open(m_miwfilepath,ios::out|ios::binary);
  ch=fin.get();
 while(!fin.eof())
 {
//生成密钥k1,k2;
int k=m_key;//10位密钥

 int tempm=ch;

int tempk=k;
	 int key[11];

key[0]=0;
for(int j=10;j>=1;j--)//取出密钥各位的值

{if(tempk&1)
{ key[j]=1;}
else {key[j]=0;}
     tempk=tempk>>1;
}
int changep10[11];
changep10[0]=0;
//p10置换
changep10[1]=key[3]	;
changep10[2]=key[5]	;
changep10[3]=key[2]	;
changep10[4]=key[7]	;
changep10[5]=key[4]	;
changep10[6]=key[10]	;
changep10[7]=key[1]	;
changep10[8]=key[9]	;
changep10[9]=key[8]	;
changep10[10]=key[6];
int zuoyi1[11];
zuoyi1[0]=0;
//左右5位各循环左移一位
 zuoyi1[1]=changep10[2];	
 zuoyi1[2]=changep10[3];
 zuoyi1[3]=changep10[4];
 zuoyi1[4]=changep10[5];
 zuoyi1[5]=changep10[1];
 zuoyi1[6]=changep10[7];
 zuoyi1[7]=changep10[8];
zuoyi1[8]=changep10[9];
 zuoyi1[9]=changep10[10];
  zuoyi1[10]=changep10[6];
int changep8[9];//p8置换
 changep8[0]=0;
changep8[1]=zuoyi1[6];
 changep8[2]=zuoyi1[3];
 changep8[3]=zuoyi1[7];
  changep8[4]=zuoyi1[4];
  changep8[5]=zuoyi1[8];
  changep8[6]=zuoyi1[5];
  changep8[7]=zuoyi1[10];
  changep8[8]=zuoyi1[9];
int key1[9];
key1[0]=0;
//生成k1
key1[1]=changep8[1];
 key1[2]=changep8[2];
key1[3]= changep8[3];
 key1[4]=changep8[4];
 key1[5]=changep8[5];
 key1[6]=changep8[6];
  key1[7]=changep8[7];
  key1[8]=changep8[8];
int zuoyi2[11];//左右5位各循环左移2位
 zuoyi2[0]=0;
 zuoyi2[1]=zuoyi1[3];
  zuoyi2[2]=zuoyi1[4];
  zuoyi2[3]=zuoyi1[5];
  zuoyi2[4]=zuoyi1[1];
  zuoyi2[5]=zuoyi1[2];
   zuoyi2[6]=zuoyi1[8];
   zuoyi2[7]=zuoyi1[9];
   zuoyi2[8]=zuoyi1[10];
   zuoyi2[9]=zuoyi1[6];
   zuoyi2[10]=zuoyi1[7];

   changep8[1]=zuoyi2[6];//p8置换
 changep8[2]=zuoyi2[3];
 changep8[3]=zuoyi2[7];
  changep8[4]=zuoyi2[4];
  changep8[5]=zuoyi2[8];
  changep8[6]=zuoyi2[5];
  changep8[7]=zuoyi2[10];
  changep8[8]=zuoyi2[9];
int key2[9];//生成k2
key2[0]=0;
key2[1]=changep8[1];
 key2[2]=changep8[2];
key2[3]= changep8[3];
 key2[4]=changep8[4];
 key2[5]=changep8[5];
 key2[6]=changep8[6];
  key2[7]=changep8[7];
  key2[8]=changep8[8];

  //加密开始
 int mingw[9];
 mingw[0]=0;
for(int i=8;i>=1;i--)//取出明文一个字符各二进制位的值
 {if(tempm&1)
{mingw[i]=1;}
else{ mingw[i]=0;}
    tempm=tempm>>1;
 }
int ip[9];//ip置换
ip[0]=0;
ip[1]=mingw[2];
ip[2]=mingw[6];
ip[3]=mingw[3];
ip[4]=mingw[1];
ip[5]=mingw[4];
ip[6]=mingw[8];
ip[7]=mingw[5];
ip[8]=mingw[7];

int ep[9];//右4位扩展置换
 ep[0]=0;
ep[1]=ip[8];
ep[2]=ip[5];
ep[3]=ip[6];
ep[4]=ip[7];
ep[5]=ip[6];
ep[6]=ip[7];
ep[7]=ip[8];
ep[8]=ip[5];

int yihuo[9];//异或
yihuo[0]=0;
yihuo[1]=ep[1]^key1[1];
yihuo[2]=ep[2]^key1[2];
yihuo[3]=ep[3]^key1[3];
yihuo[4]=ep[4]^key1[4];
yihuo[5]=ep[5]^key1[5];
yihuo[6]=ep[6]^key1[6];
yihuo[7]=ep[7]^key1[7];
yihuo[8]=ep[8]^key1[8];

int hang[3];//生成行号
hang[0]=0;
hang[1]=yihuo[1];
hang[1]=hang[1]<<1;
hang[1]=hang[1]|yihuo[4];
hang[2]=yihuo[5];
hang[2]=hang[2]<<1;
hang[2]=hang[2]|yihuo[8];
int lie[3];//生成列号
 lie[0]=0;
lie[1]=yihuo[2];
lie[1]=lie[1]<<1;
lie[1]=lie[1]|yihuo[3];
lie[2]=yihuo[6];
lie[2]=lie[2]<<1;
lie[2]=lie[2]|yihuo[7];
int s0[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};//s0盒子
int s1[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};//s0盒子
int r1=hang[1];
int r2=hang[2];
int c1=lie[1];
int c2=lie[2];
int out[3];
 out[0]=0;
out[1]=s0[r1][c1];
out[2]=s1[r2][c2];
int outbit1[3];
outbit1[0]=0;
int outbit2[3];
outbit2[0]=0;
for(int x=2;x>=1;x--)//取输出前两位
{if (out[1]&1==1)
{outbit1[x]=1;}
    
else {outbit1[x]=0;}
//outbit1[x]=outbit1[x]>>1;
out[1]=out[1]>>1;
}
for(int y=2;y>=1;y--)//取输出后两位
{if (out[2]&1==1)
{outbit2[y]=1;}
    

⌨️ 快捷键说明

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