📄 cryptographydlg.cpp
字号:
// CryptographyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Cryptography.h"
#include "CryptographyDlg.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
#include <math.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()
/////////////////////////////////////////////////////////////////////////////
// CCryptographyDlg dialog
CCryptographyDlg::CCryptographyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCryptographyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCryptographyDlg)
m_nPasswordType = -1;
m_nFirstText = _T("");
m_nLastText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCryptographyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCryptographyDlg)
DDX_Radio(pDX, IDC_RADIO_CAESAR, m_nPasswordType);
DDX_Text(pDX, IDC_EDIT1, m_nFirstText);
DDX_Text(pDX, IDC_EDIT2, m_nLastText);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCryptographyDlg, CDialog)
//{{AFX_MSG_MAP(CCryptographyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EXIT_PROGRAME, OnExitPrograme)
ON_BN_CLICKED(IDC_INCREASE_SECRET, OnIncreaseSecret)
ON_BN_CLICKED(IDC_BREAK_SECRET, OnBreakSecret)
ON_BN_CLICKED(IDC_ABOUT_AUTHOR, OnAboutAuthor)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCryptographyDlg message handlers
BOOL CCryptographyDlg::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 CCryptographyDlg::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 CCryptographyDlg::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 CCryptographyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
///////////////////根据密钥词来构造矩阵,已知构造的矩阵为5*5的形式////////////////////////
////////////////////密钥为:MONARCHY/////////////////////////////////////////
//////////////////////该距阵在playfair加密解密算法中要用到///////////////////////////////
char Metrix[5][5] = {
'M','O','N','A','R',
'C','H','Y','B','D',
'E','F','G','I','K',
'L','P','Q','S','T',
'U','V','W','X','Z',
};
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////对文件加密/////////////////////////////////////////
void CCryptographyDlg::OnIncreaseSecret()
{
UpdateData(TRUE);
if(m_nPasswordType == -1)
{
MessageBox("请选择你的加密类型!",NULL,MB_OK);
return;
}
UpdateData(TRUE);
if (m_nFirstText == "")
{
MessageBox("请输入你要加密的明文!",NULL,MB_OK);
return;
}
UpdateData(TRUE);
switch(m_nPasswordType)
{
case 0: //Caesar密码加密
{
UpdateData(TRUE); //获取文本框中输入的内容
CString str = "";
char tempChar; //定义一个临时的字符变量
tempChar = m_nFirstText.GetAt(0);
int Length = m_nFirstText.GetLength();
m_nFirstText.MakeUpper();
for (int i = 0;i < Length; i ++)
{
tempChar = m_nFirstText.GetAt(i);
if (' ' == tempChar) //对于空格符,直接保留空格,不做转换
{
tempChar = tempChar;
}
else if (tempChar == 'X' || tempChar == 'Y' || tempChar == 'Z')
{
tempChar = tempChar + 3 - 26;
}
else
{
tempChar = tempChar + 3;
}
str = str + tempChar;
}
m_nLastText = str;
UpdateData(FALSE); //将转换后内容输出到文本框中
}
break;
case 1: //Playfair密码加密
{
int Length = 0; //密文长度
int LengthKey = 0; //密钥长度
int i = 0;
int j = 0;
char tempChar1; //用于保存加密后该字符对第一个字符的值
char tempChar2; //用于保存加密后该字符对第二个字符的值
CString StrCipherTemp = "";//定义密文
CString strKey = "monarchy";
strKey.MakeUpper();
Length = m_nFirstText.GetLength();
m_nFirstText.MakeUpper();
LengthKey = strKey.GetLength();
//当一个字母对的两个字母相同时,那
//char FillChar = 'x'; //么在他们之间加上一个填充字母x。
int RowX1; //第一个字母的行号
int ColumnY1 ; //第一个字母的列号
int RowX2; //第二个字母的行号
int ColumnY2; //第二个字母的列号
////////////////////////////对密文加密/////////////////////////////////
for (i = 0;i < Length;i = i + 2)
{
if (m_nFirstText.GetAt(i) == m_nFirstText.GetAt(i + 1))
{
m_nFirstText.Insert(i + 1,'X'); //在字符串的指定位置插入一个字符
Length = m_nFirstText.GetLength(); //字符串的长度增加1
}
//如果字符个数为奇数,在字符串的末尾追加一个字符'x'.
if (Length % 2 != 0)
{
m_nFirstText.Insert(Length,'X');
}
//碰到字母j则将字母J所在的位置用字母i来代替
if(m_nFirstText.GetAt(i) == 'J')
m_nFirstText.SetAt(i,'I');
if(m_nFirstText.GetAt(i + 1) == 'J')
m_nFirstText.SetAt(i + 1,'I');
//获得指定字符对的行号和列号
RowX1 = GetX(m_nFirstText.GetAt(i));
ColumnY1 = GetY(m_nFirstText.GetAt(i));
RowX2 = GetX(m_nFirstText.GetAt(i + 1));
ColumnY2 = GetY(m_nFirstText.GetAt(i + 1));
//如果该字母对落在矩阵同一行则由其右边的字母来代替
if(RowX1 == RowX2) //行号相同
{
tempChar1 = Metrix[RowX1][((ColumnY1 + 1) % 5)];
tempChar2 = Metrix[RowX1][((ColumnY2 + 1) % 5)];
}
//如果该字母对落在矩阵同一列则由其下边的字母来代替
else if(ColumnY1 == ColumnY2) //列号相同
{
tempChar1 = Metrix[((RowX1 + 1) % 5)][ColumnY1];
tempChar2 = Metrix[((RowX2 + 1) % 5)][ColumnY1];
}
////对于其他情况的,该字母所在的行为密文所在行,
////另一字母所在列为密文所在列
else
{
tempChar1 = Metrix[RowX1][ColumnY2];
tempChar2 = Metrix[RowX2][ColumnY1];
}
StrCipherTemp = StrCipherTemp + tempChar1 + tempChar2;
m_nLastText = StrCipherTemp;
m_nLastText.MakeLower();
UpdateData(FALSE);
}
}
break;
case 2: //Vigenere密码加密
{
int Length = 0; //定义明文的长度
int LengthKey = 0; //定义密钥的长度
int i = 0;
int j = 0;
CString strKey = "deceptive"; //定义密钥
strKey.MakeUpper(); //将密钥字符串全部变为大些
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -