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

📄 古典密码学dlg.cpp

📁 古典密码学之维吉尼亚的MFC实现
💻 CPP
字号:
// 古典密码学Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "古典密码学.h"
#include "古典密码学Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

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

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_EDIT1, m_source);
	DDX_Text(pDX, IDC_EDIT2, m_dest);
	DDX_Text(pDX, IDC_EDIT3, m_key);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, Onadd)
	ON_BN_CLICKED(IDC_BUTTON2, Onout)
	ON_BN_CLICKED(IDC_BUTTON3, OnOpen)
	ON_BN_CLICKED(IDC_BUTTON4, OnSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

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

//加密按键
void CMyDlg::Onadd() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
	 if(!m_key.GetLength())
		MessageBox("未输入密匙,请输入!","警告!",MB_ICONEXCLAMATION);

	 int i, j;
     for(i = 0; i < CHARSUM; i++)
     {
         for(j = 0; j < CHARSUM; j++)
         {
              table[i][j] = MINCHAR1 + (i + j) % CHARSUM;
         }
     }
	     char key[256];
	   	 char str1[256];
		 char str2[256];
		 strncpy(key,(LPCTSTR)m_key,sizeof(key));  
		 strncpy(str1,(LPCTSTR)m_source,sizeof(str1));  
         Encode(key, str1, str2);
		 m_dest.Format("%s", str2); 
		 m_source.Format("%s",str1);
   UpdateData(FALSE);

}

//解密按键
void CMyDlg::Onout() 
{
	// TODO: Add your control notification handler code here
    if(!m_key.GetLength())
	MessageBox("未输入密匙,请输入!","警告!",MB_ICONEXCLAMATION);	    
	UpdateData(TRUE);
	 int i, j;
     for(i = 0; i < CHARSUM; i++)
     {
         for(j = 0; j < CHARSUM; j++)
         {
              table[i][j] = MINCHAR1 + (i + j) % CHARSUM;
         }
     }
	     char key[256];
	   	 char str1[256];
		 char str2[256];
		 strncpy(key,(LPCTSTR)m_key,sizeof(key));  
		 strncpy(str1,(LPCTSTR)m_source,sizeof(str1));  
         Dncode(key, str1, str2);
		 m_dest.Format("%s", str2); 
		 m_source.Format("%s",str1);
   UpdateData(FALSE);
}

//加密
bool CMyDlg::Encode(char *key, char *source, char *dest)
{
 char* tempSource = source;
     char* tempKey = key;
     char* tempDest = dest;
     do
     {
         *tempDest = table[(*tempKey) - MINCHAR1][(*tempSource) - MINCHAR1];
         tempDest++;
         if(!(*(++tempKey)))
              tempKey = key;
     }while(*tempSource++);
     dest[strlen(source)] = 0;
     return true;

}

//解密
bool CMyDlg::Dncode(char *key, char *source, char *dest)
{
     char* tempSource = source;
     char* tempKey = key;
     char* tempDest = dest;
     char offset;
     do
     {
         offset = (*tempSource) - (*tempKey);
         offset = offset >= 0 ? offset : offset + CHARSUM;
         *tempDest = MINCHAR1 + offset;
         tempDest++;
         if(!(*(++tempKey)))
              tempKey = key;
     }while(*++tempSource);
     dest[strlen(source)] = 0;
     return true;

}



void CMyDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	CString strFileName,t;
	int i;
	UpdateData(TRUE);   
	strFileName="test_";
	CFileDialog dlg(FALSE,"text",strFileName, //FALSE为"打开文件"窗口,"ctext"为确定时默认的文件类型
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		"初始配置文件(*.text)|*.text||",NULL);
	dlg.m_ofn.lpstrInitialDir=".\\res";
	if(dlg.DoModal()==IDCANCEL) return;
	strFileName = dlg.GetPathName();

	
	CString Output,temp;
	Output.Empty();

	for (i=0;i<m_source.GetLength();i++) Output.Insert(65535,m_source.GetAt(i));


	CFile file;
	file.Open(strFileName,CFile::modeCreate|CFile::modeWrite);

	file.Write(Output,Output.GetLength());
	file.Close();

    CMyDlg::MessageBox("保存完毕!","提示",MB_ICONINFORMATION);

}

void CMyDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	CString strFileName;
	CFile file;
	int i,j;
	int sum;
	char buffer[1000];
	for(i=0;i<1000;i++) buffer[i]=' ';
	UpdateData(TRUE);   
	m_source.Empty();
	UpdateData(FALSE);
	
	CFileDialog dlg(TRUE,"text",".text", //TRUE为"打开文件"窗口,"ctext"为确定时默认的文件类型
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		"初始配置文件(*.text)|*.text||",NULL);
	dlg.m_ofn.lpstrInitialDir=".\\res";
	if(dlg.DoModal()==IDCANCEL) return;
	strFileName = dlg.GetPathName();

	file.Open(strFileName,CFile::modeRead);
	file.Read(buffer,file.GetLength());
	file.Close();
	
	i=0;
	sum=0;
	while(buffer[i]!=' ')
	{
		i++;
		sum++;
	}
	
    for(j=0;j<sum;j++)
	{
	    m_source+=buffer[j];
		
	}
	UpdateData(FALSE);
    
	CMyDlg::MessageBox("提取完毕!","提示",MB_ICONINFORMATION);

}

⌨️ 快捷键说明

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