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

📄 myplayfairdlg.cpp

📁 在程序中由playfair(char *s,int length) 的实现加密和解密;解密原理与加密类似
💻 CPP
字号:
// MyPlayFairDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyPlayFair.h"
#include "MyPlayFairDlg.h"

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

char key[5][5]={{'p','l','a','y','f'},{'i','r','b','c','d'},{'e','g','h','k','m'},{'n','o','q','s','t'},{'u','v','w','x','z'}};
void PlayFair(char*,int,int);

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

/////////////////////////////////////////////////////////////////////////////
// CMyPlayFairDlg dialog

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

void CMyPlayFairDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyPlayFairDlg)
	DDX_Text(pDX, IDC_ciphertext, m_cipher);
	DDV_MaxChars(pDX, m_cipher, 30);
	DDX_Text(pDX, IDC_orginaltext, m_orginal);
	DDV_MaxChars(pDX, m_orginal, 30);
	DDX_Text(pDX, IDC_plaintext, m_plain);
	DDV_MaxChars(pDX, m_plain, 30);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyPlayFairDlg, CDialog)
	//{{AFX_MSG_MAP(CMyPlayFairDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_quit, Onquit)
	ON_BN_CLICKED(ID_De, OnDe)
	ON_BN_CLICKED(ID_En, OnEn)
	ON_BN_CLICKED(IDC_BUTTON1, Onfilename)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPlayFairDlg message handlers

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

void CMyPlayFairDlg::Onquit() 
{
	CDialog::OnCancel();	
}

void CMyPlayFairDlg::OnDe() 
{
	char str2[30];
	int i=0,num;
	CString t(30,'0');
	GetDlgItemText(IDC_ciphertext,t);

    num=sizeof(t)+1;
    PlayFair(str2,num,0);

		for(i=0;i<num;i++)
			t.SetAt(i,str2[i]);
		
		MessageBox("解密成功!!",NULL,MB_OK);
		SetDlgItemText(IDC_orginaltext,t);
	
}

void CMyPlayFairDlg::OnEn() 
{
	char str1[30],str2[30];
	int i=0,j=0,num;
	CString s(30,'0'),t(30,'0');
	GetDlgItemText(IDC_plaintext,t);
	//过滤空格和\t
    for(i;i!=sizeof(t)+1;i++)
    {
        if((t[i]==' ')||(t[i]=='\t'))
            continue;
        str1[i]=t[i];
	}
    
    num=i;//计数
	for(i=0;i<num;i++)
		{
			if((str1[i]==str1[i+1])&&(j%2==0)&&((i+1)!=num))
    		{
				str2[j]=str1[i];
				str2[j+1]='x';
        		j+=2;
    		}
            else
			{
				str2[j]=str1[i];
        		j+=1;
    		}
		
		}
		if(j%2!=0)
		{
			str2[j]='x';
			num=j+1;
		}
		else num=j;
	    PlayFair(str2,num,1);
		for(i=0;i<num;i++)
			s.SetAt(i,str2[i]);
		
		MessageBox("加密成功!!",NULL,MB_OK);
		SetDlgItemText(IDC_ciphertext,s);
}

void PlayFair(char* str2,int num,int flag)
{
	int pos,i,j;
	int p,q,m,n;
    for(pos=0;pos<num;pos+=2)
	{
		for(i=0;i<5;i++)
		for(j=0;j<5;j++)
        {
            if(key[i][j]==str2[pos])
            {
                p=i;
                q=j;
            }
            if(key[i][j]==str2[pos+1])
            {
                m=i;
                n=j;
            }
        }
		if((p!=m)&&(q!=n))
		{  
			str2[pos]=key[p][n];
			str2[pos+1]=key[m][q];
		}
		if(p==m)
		{
			if(flag==1)
			{
				if(q==4) q=0;
				else q=q+1;
				if(n==4) n=0;
				else n=n+1;
			}
			if(flag==0)
			{
				if(q==0) q=4;
				else q=q-1;
				if(n==0) n=4;
				else n=n-1;
			}
            str2[pos]=key[p][q];
            str2[pos+1]=key[m][n];
        }
		if(q==n)
		{
			if(flag==1)
			{
				if(p==4) p=0;
				else p=p+1;
				if(m==4) m=0;
				else m=m+1;
			}
			if(flag==0)
			{
				if(p==0) p=4;
				else p=p-1;
				if(m==0) m=4;
				else m=m-1;
			}
			str2[pos]=key[p][q];
            str2[pos+1]=key[m][n];
		}
	}
   
}

void CMyPlayFairDlg::Onfilename() 
{
		CFileDialog f(TRUE);
	if(f.DoModal() == IDOK)
	{
		//AfxMessageBox(f.GetPathName());
		m_csInFile = f.GetPathName();
		UpdateData(0);
	}
}

⌨️ 快捷键说明

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