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

📄 beaufortdlg.cpp

📁 包涵了密码学教程里面的大部分加密算法
💻 CPP
字号:
// BeaufortDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Crypt.h"
#include "BeaufortDlg.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

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

static CString alphabet = "abcdefghijklmnopqrstuvwxyz";
static CString ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/////////////////////////////////////////////////////////////////////////////
// CBeaufortDlg dialog


CBeaufortDlg::CBeaufortDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBeaufortDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBeaufortDlg)
	m_strCipher = _T("");
	m_strPlainText = _T("");
	m_strKey = _T("");
	//}}AFX_DATA_INIT
}


void CBeaufortDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBeaufortDlg)
	DDX_Text(pDX, IDC_Cipher, m_strCipher);
	DDX_Text(pDX, IDC_PlainText, m_strPlainText);
	DDX_Text(pDX, IDC_Key, m_strKey);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBeaufortDlg, CDialog)
	//{{AFX_MSG_MAP(CBeaufortDlg)
	ON_BN_CLICKED(IDC_Encrypt, OnBeaufortEncrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBeaufortDlg message handlers

/*
	Beaufort加密体制,是一种多表简单加法密码。
*/
void CBeaufortDlg::OnBeaufortEncrypt() 
{
	// TODO: Add your control notification handler code here
	int  i, j, k, l, milestone;
	int  pos1, pos2;
	int  m_iPlainTextLen, m_iKeyLen;
	char m_chPlainText[1000], m_chKey[1000], m_chCipher[1000];
	int  m_iPlainText[1000], m_iKey[1000], m_iCipher[1000];

	for(i=0; i<1000; i++)
	{
		m_chPlainText[i] = '\0';
		m_chKey[i] = '\0';
		m_chCipher[i] = '\0';
	}
    
	UpdateData(true);

    m_iPlainTextLen = m_strPlainText.GetLength();
	for(i=0; i<m_iPlainTextLen; i++)
		m_chPlainText[i] = m_strPlainText.GetAt(i);  
	//AfxMessageBox(m_chPlainText);

	for(i=0; i<m_iPlainTextLen; i++)
	{
		if(65<=m_chPlainText[i] && m_chPlainText[i]<=90)
			pos1 = ALPHABET.Find(m_chPlainText[i]);
		else if(97<=m_chPlainText[i] && m_chPlainText[i]<=122)
			pos1 = alphabet.Find(m_chPlainText[i]);
		
		m_iPlainText[i] = pos1;
	}
   
	m_iKeyLen = m_strKey.GetLength();
	for(i=0; i<m_iKeyLen; i++)
		m_chKey[i] = m_strKey.GetAt(i);
	for(i=0; i<m_iKeyLen; i++)
	{
		if(65<=m_chKey[i] && m_chKey[i]<=90)
			pos2 = ALPHABET.Find(m_chKey[i]);
		else if(97<=m_chKey[i] && m_chKey[i]<=122)
			pos2 = alphabet.Find(m_chKey[i]);

		m_iKey[i] = pos2;
	}
    
	//加密过程
	if(m_iPlainTextLen <= m_iKeyLen)
	{
		for(i=0; i<m_iPlainTextLen; i++)
		{
			m_iCipher[i] = (m_iKey[i] + 25 - m_iPlainText[i]) % 26;
			m_chCipher[i] = alphabet.GetAt(m_iCipher[i]);
		}
	}
    else
	{
		int quotient = (int)m_iPlainTextLen/m_iKeyLen;
		int compliment = m_iPlainTextLen%m_iKeyLen;
		for(i=0; i<quotient; i++)
			for(j=0; j<m_iKeyLen; j++)
			{
                k = i*m_iKeyLen + j;
				l = (m_iKey[j] + 25 - m_iPlainText[k]) % 26;
				m_iCipher[k] = l;
				m_chCipher[k] = alphabet.GetAt(l);
			}
		
		milestone = quotient * m_iKeyLen;
		for(i=0; i<compliment; i++)
		{		
			j = milestone + i;
			m_iCipher[j] = (m_iKey[i] + 25 - m_iPlainText[j]) % 26;
			m_chCipher[j] = alphabet.GetAt(m_iCipher[j]);
		}
	}

	m_strCipher.Format("%s", m_chCipher);

	UpdateData(false);
}

⌨️ 快捷键说明

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