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

📄 myfirstdlg.cpp

📁 完整可运行的DES3层加解密程序,是个妹妹写的毕业论文程序,完全自己写的,毕业生这下好了,网上是找不到的
💻 CPP
字号:
// myfirstDlg.cpp : implementation file
//

#include "stdafx.h"
#include "myfirst.h"
#include "myfirstDlg.h"
#include "DES.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()

/////////////////////////////////////////////////////////////////////////////
// CMyfirstDlg dialog

CMyfirstDlg::CMyfirstDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyfirstDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyfirstDlg)
	m_path = _T("");
	m_mm1 = _T("");
	m_mm2 = _T("");
	m_yw = _T("");
	m_mw = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyfirstDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyfirstDlg)
	DDX_Text(pDX, IDC_EDIT5, m_path);
	DDX_Text(pDX, IDC_EDIT1, m_mm1);
	DDX_Text(pDX, IDC_EDIT4, m_mm2);
	DDX_Text(pDX, IDC_EDIT2, m_yw);
	DDX_Text(pDX, IDC_EDIT3, m_mw);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyfirstDlg, CDialog)
	//{{AFX_MSG_MAP(CMyfirstDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDOK2, OnOk2)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyfirstDlg message handlers

BOOL CMyfirstDlg::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
	((CButton *)GetDlgItem(IDC_RADIO7))->SetCheck(TRUE);
	((CButton *)GetDlgItem(IDC_RADIO5))->SetCheck(TRUE);//
	((CButton *)GetDlgItem(IDC_RADIO3))->SetCheck(TRUE);//
	((CButton *)GetDlgItem(IDC_RADIO9))->SetCheck(TRUE);////
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMyfirstDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
			CFileDialog dlg(TRUE);
			
	if(dlg.DoModal() == IDOK)
	{
		m_path = dlg.GetPathName();
		UpdateData(FALSE);
	}
	else
	{
		return;
	}
	
}










int CMyfirstDlg:: CreateRand(int nSeed)
{
int iRand;
try
{
//srand( (unsigned)time( NULL ) );这句在程序开始调用

if (nSeed>0)
{
iRand=rand()%nSeed;
}else
iRand=-1;
}
catch(CException)
{
iRand=-1;
}
return iRand;
}

CString CMyfirstDlg:: CreateRandString()
{
        int iRet=0; 
        CString strTemp;
        CString sRandStr="";
    try
		{ 
           int i,j,k;

              for(i=0;i<8;i++)
				{
					 j=CreateRand(100);
						if (j % 2)
						{
//余数为1:大写字母
							j=CreateRand(26);
							k=65;
						}
						else
						{
//余数为0:数字
							j=CreateRand(10);
							k=48;
						}

							strTemp.Format("%c",j+k);
							sRandStr+=strTemp; 

			  } 
		iRet=1;
		}
	catch (CException) 
	{ 
		iRet=0;
	}
		return sRandStr;
}



void CMyfirstDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CMyfirstDlg::OnOk2() 
{
	// TODO: Add your control notification handler code here
	
}

void CMyfirstDlg::OnCancel2() 
{
	// TODO: Add your control notification handler code here
	
}

void CMyfirstDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	 UpdateData(true);
	m_mm1 = CreateRandString();
		UpdateData(FALSE);
	
}

void CMyfirstDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_mm2 = CreateRandString();
		UpdateData(FALSE);
}

void CMyfirstDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
    UpdateData(true);
	int leng=0;
    leng=m_yw.GetLength();
	if(leng==0)
	{
		MessageBox("请输入原文", "友情提示");
		return;
		
	}
	if(m_mm1.GetLength()!=8)
	{
		MessageBox("请输入8位密钥", "友情提示");
		return;
		
	}
    
	//char [] a=new char [m_mm1.GetLength()]

	char *yw=NULL;
    char *mw=NULL;
	char *my=NULL;
     yw = new char [leng];
	 mw = new char [leng];
	 my = new char [8];
	 for (int i=0;i<leng;i++)
		 yw[i]=m_yw[i];
	 for (int j=0;j<8;j++)
		 my[j]=m_mm1[j];
	if (((CButton *)GetDlgItem(IDC_RADIO7))->GetCheck()==1)
    {
		if(((CButton *)GetDlgItem(IDC_RADIO5))->GetCheck()==1)
		{
			if(CDES::RunDes(0,0,yw,mw,leng,my,0x8))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_mw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("ECB1加密成功", "友情提示");
		
	}
	else
	{
		MessageBox("ECB1加密失败", "友情提示");
	}
	}
	
	else
	{
		if(CDES::RunDes(0,0,yw,mw,leng,my,0x10))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_mw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("ECB3加密成功", "友情提示");
		
	}
	else
	{
		MessageBox("ECB3加密失败", "友情提示");
	}

	}
	}
	else
	{
		if(((CButton *)GetDlgItem(IDC_RADIO5))->GetCheck()==1)
		{
			if(CDES::RunDes(0,1,yw,mw,leng,my,0x8))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_mw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("CBC1加密成功", "友情提示");
		
	}
	else
	{
		MessageBox("CBC1加密失败", "友情提示");
	}
		}
		else
		{
			if(CDES::RunDes(0,1,yw,mw,leng,my,0x10))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_mw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("CBC3加密成功", "友情提示");
		
	}
	else
	{
		MessageBox("CBC3加密失败", "友情提示");
	}
		}

	}
    
	
}

void CMyfirstDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
    UpdateData(true);
	int leng=0;
    leng=m_mw.GetLength();
	char *yw=NULL;
    char *mw=NULL;
	char *my=NULL;
     yw = new char [leng];
	 mw = new char [leng];
	 my = new char [8];
	 for (int i=0;i<leng;i++)
		 yw[i]=m_mw[i];
	 for (int j=0;j<8;j++)
		 my[j]=m_mm1[j];
	
if (((CButton *)GetDlgItem(IDC_RADIO7))->GetCheck()==1)
{
	if (((CButton *)GetDlgItem(IDC_RADIO5))->GetCheck()==1)
	{
	if(CDES::RunDes(1,0,yw,mw,leng,my,0x8))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_yw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("解密成功", "友情提示");
		
	}
	else
	{
		MessageBox("解密失败", "友情提示");
	}
	}
	else
	{if(CDES::RunDes(1,0,yw,mw,leng,my,0x10))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_yw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("解密成功", "友情提示");
		
	}
	else
	{
		MessageBox("解密失败", "友情提示");
	}
	}
}
else
{
	if(((CButton *)GetDlgItem(IDC_RADIO5))->GetCheck()==1)
	{if(CDES::RunDes(1,1,yw,mw,leng,my,0x8))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_yw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("解密成功", "友情提示");
		
	}
	else
	{
		MessageBox("解密失败", "友情提示");
	}}
	else
	{if(CDES::RunDes(1,1,yw,mw,leng,my,0x10))
		{
		 CString b;
		for (int k=0;k<leng;k++)
		     b+=mw[k];
		     m_yw=b;
         
		//m_mw=mw;
		 UpdateData(FALSE);

		MessageBox("解密成功", "友情提示");
		
	}
	else
	{
		MessageBox("解密失败", "友情提示");
	}}
}

}

void CMyfirstDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
    char *data=NULL;
	char *wdata=NULL;
	char *my=NULL;
	my = new char [8];
	CFile *file;
	DWORD flen;
    LPCTSTR fpath=m_path;
   // MessageBox(m_mm2, "友情提示");
	for (int j=0;j<8;j++)
		 my[j]=m_mm2[j];

	file = new CFile;
	if ( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		MessageBox("打开文件失败", "友情提示");
	}
	else
	{

	flen = file->GetLength();

	data = new char[(int)flen];
	wdata = new char[(int)flen];

	file->SeekToBegin();
	file->Read(data, flen);

    if(CDES::RunDes(0,0,data,wdata,(int)flen,my,0x8))
	{

		MessageBox("加密文件成功", "友情提示");
		file->SeekToBegin();
	    file->Write(wdata, flen);
		
	}
	else
	{
		MessageBox("加密文件失败", "友情提示");
	}


	file->SeekToBegin();
	file->Write(wdata, flen);
	delete[] data;

	file->Close();
	}
	delete file;



	
}

void CMyfirstDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
    char *data=NULL;
	char *wdata=NULL;
	char *my=NULL;
	my = new char [8];
	CFile *file;
	DWORD flen;
    LPCTSTR fpath=m_path;
   // MessageBox(m_mm2, "友情提示");
	for (int j=0;j<8;j++)
		 my[j]=m_mm2[j];

	file = new CFile;
	if( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		MessageBox("打开文件失败", "友情提示");
	}
	else
	{

	flen = file->GetLength();
	data = new char[(int)flen];
	wdata = new char[(int)flen];

   


	//解密
	file->SeekToBegin();
	file->Read(data, flen);


 if(CDES::RunDes(1,0,data,wdata,(int)flen,my,0x8))
	{

	  	    MessageBox("解密文件成功", "友情提示");
			file->SeekToBegin();
	        file->Write(wdata, flen);
		
	}
	else
	{
		MessageBox("解密文件失败", "友情提示");
	}

	file->SeekToBegin();
	file->Write(wdata, flen);
	file->Close();
    
	delete[] data;
	delete[] wdata;
	}
	delete file;
	
}

⌨️ 快捷键说明

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