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

📄 zitoudlg.cpp

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

#include "stdafx.h"
#include "Crypt.h"
#include "ZiTouDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CZiTouDlg dialog
static char plaintext[26] = {
	'a','b','c','d','e',
	'f','g','h','i','j',
	'k','l','m','n','o',
	'p','q','r','s','t',
	'u','v','w','x','y','z'};

static char cipher[26] = {
	'c','i','p','h','e',
	'r','a','b','d','f',
	'g','j','k','l','m',
	'n','o','q','s','t',
	'u','v','w','x','y','z'};

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


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


BEGIN_MESSAGE_MAP(CZiTouDlg, CDialog)
	//{{AFX_MSG_MAP(CZiTouDlg)
	ON_BN_CLICKED(IDC_Encrypt, OnZiTouEncrypt)
	ON_BN_CLICKED(IDC_Decrypt, OnZiTouDecrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZiTouDlg message handlers

BOOL CZiTouDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::Create(IDD, pParentWnd);
}


//加密部分
void CZiTouDlg::OnZiTouEncrypt() 
{
	// TODO: Add your control notification handler code here
	int i, pos;
	int m_iPlainTextLen;
	char m_chPos;
	CString m_strTemp;
	CString PlainText = "abcdefghijklmnopqrstuvwxyz";

	UpdateData(true);
	m_strTemp = m_strPlainText;
	m_iPlainTextLen = m_strPlainText.GetLength();
	if(m_iPlainTextLen <= 0)
	{
		AfxMessageBox("No PlainText to encrypt!", MB_ICONSTOP|MB_OK);
		return;
	}

	char* m_chPlainText = new char[m_iPlainTextLen];
	char* m_chCipher = new char[m_iPlainTextLen];
	m_chPlainText = m_strPlainText.GetBuffer(0);
	
	for(i=0; i<m_iPlainTextLen; i++)
	{
		m_chPos = m_chPlainText[i];
		pos = PlainText.Find(m_chPos);
		m_chCipher[i] = cipher[pos];
	}
	m_chCipher[m_iPlainTextLen] = '\0';
	m_strCipher.Format("%s",m_chCipher);
	//m_strPlainText = m_strTemp;

	UpdateData(false);	
}


//解密部分
void CZiTouDlg::OnZiTouDecrypt() 
{
	// TODO: Add your control notification handler code here
	int i, pos;
	int m_iCipherLen;
	char m_chPos;
	static CString Cipher = "cipherabdfgjklmnoqstuvwxyz";
    
	//首先得到密文
	UpdateData(true);
	//AfxMessageBox(m_strCipher);
	//将密文字符串转换为字符数组
	m_iCipherLen = m_strCipher.GetLength();
	if(m_iCipherLen <= 0)
	{
		AfxMessageBox("没有需要解密的密文!", MB_ICONSTOP|MB_OK);
		exit(0);
		//return;
		//OnCancel();
	}
    
	char* m_chCipher = new char[m_iCipherLen];
	char* m_chPlainText = new char[m_iCipherLen];
	m_chCipher = m_strCipher.GetBuffer(0);
    //AfxMessageBox(m_chCipher);

    for(i=0; i<m_iCipherLen; i++)
	{
		m_chPos = m_chCipher[i];
        pos = Cipher.Find(m_chPos);
		m_chPlainText[i] = plaintext[pos];
	}
	m_chPlainText[m_iCipherLen] = '\0';
	m_strPlainText.Format("%s", m_chPlainText);

	UpdateData(false);
}


BOOL CZiTouDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_strPlainText = "";
    m_strCipher = "";
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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