📄 cryptographydlg.cpp
字号:
// CryptographyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Cryptography.h"
#include "CryptographyDlg.h"
#include <iostream>
#include <fstream>
using namespace std;
#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_StrCipInPut = _T("");
m_StrPlainTextOut = _T("");
m_StrPlaintTextInPut = _T("");
Choose = 1;
m_StrPass = _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_Text(pDX, IDC_EDIT_Cip_InOrOut, m_StrCipInPut);
DDX_Text(pDX, IDC_EDIT_PlainText_Out, m_StrPlainTextOut);
DDX_Text(pDX, IDC_EDIT_PlaintText_InPut, m_StrPlaintTextInPut);
DDX_Text(pDX, IDC_EDIT_Pass, m_StrPass);
//}}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_BUTTON_Encrypt, OnBUTTONEncrypt)
ON_BN_CLICKED(IDC_BUTTON_Decrypt, OnBUTTONDecrypt)
ON_BN_CLICKED(IDC_RADIO_Caesar, OnRADIOCaesar)
ON_BN_CLICKED(IDC_RADIO_Playfair, OnRADIOPlayfair)
ON_BN_CLICKED(IDC_RADIO_Vigenere, OnRADIOVigenere)
ON_BN_CLICKED(IDC_RADIO_Permutation, OnRADIOPermutation)
ON_BN_CLICKED(IDC_BUTTON_Choose, OnBUTTONChoose)
//}}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
((CButton *)GetDlgItem(IDC_RADIO_Caesar))->SetCheck(1);
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;
}
//加密
void CCryptographyDlg::OnBUTTONEncrypt()
{
// TODO: Add your control notification handler code here
int i;
i = Choose;
switch(i)
{
case 1:
Caesar();
break;
case 2:
Playfair();
break;
case 3:
Vigenere();
break;
case 4:
Permutation();
break;
default:
break;
}
}
//解密
void CCryptographyDlg::OnBUTTONDecrypt()
{
// TODO: Add your control notification handler code here
int i;
i = Choose;
switch(i)
{
case 1:
CaesarDecrypt();
break;
case 2:
PlayfairDecrypt();
break;
case 3:
VigenereDecrypt();
break;
case 4:
PermutationDecrypt();
break;
default:
break;
}
}
void CCryptographyDlg::OnRADIOCaesar()
{
// TODO: Add your control notification handler code here
Choose = 1;
}
void CCryptographyDlg::OnRADIOPlayfair()
{
// TODO: Add your control notification handler code here
Choose = 2;
}
void CCryptographyDlg::OnRADIOVigenere()
{
// TODO: Add your control notification handler code here
Choose = 3;
}
void CCryptographyDlg::OnRADIOPermutation()
{
// TODO: Add your control notification handler code here
Choose = 4;
}
//选择明文
void CCryptographyDlg::OnBUTTONChoose()
{
// TODO: Add your control notification handler code here
UpdateData(true);
static char BASED_CODE szFilter[] = "文本文件(*.txt)|*.txt";
//创建文件对话框对象
CFileDialog file(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,NULL,NULL);
if (file.DoModal() == IDOK)
{
CString FilePath = file.GetPathName(); //获取文件路径
CString FileName = file.GetFileTitle(); //获取文件名
UpdateData(false);
Openfile(FilePath);
}
}
//根据文件路径读取文件
void CCryptographyDlg::Openfile(CString FilePath)
{
UpdateData(true);
char FileArray[1000];
memset(&FileArray,0,1000);
ifstream myFile;
myFile.open(((char *)FilePath.GetBuffer(1)),ios_base::in);
while(myFile.getline(FileArray,1000)) //每次读取一行
{
m_StrPlaintTextInPut += FileArray;
}
myFile.close();
UpdateData(false);
}
//凯撒密码
void CCryptographyDlg::Caesar()
{
UpdateData(TRUE);
CString table = "abcdefghijklmnopqrstuvwxyz"; //字母表
CString str;
int i = 0,j = 0,k = 0;
int Length = m_StrPlaintTextInPut.GetLength(); //获取明文长度
while (i<Length)
{
j= (table.Find(m_StrPlaintTextInPut.GetAt(i))+3)%26; //通过将字母按顺序推后起3位起到加密作用
i++;
str += table.GetAt(j);
}
m_StrCipInPut = str;
UpdateData(FALSE);
}
void CCryptographyDlg::CaesarDecrypt()
{
UpdateData(TRUE);
CString table = "abcdefghijklmnopqrstuvwxyz"; //字母表
CString str,str1;
int i = 0,j = 0,k = 0;
int Length = m_StrCipInPut.GetLength(); //获取密文长度
str1 = m_StrCipInPut;
while (i<Length) //通过将字母按顺序前移起3位起到解密作用
{
j= ((table.Find(str1.GetAt(i))-3)+26)%26;
i++;
str += table.GetAt(j);
}
m_StrPlainTextOut = str;
UpdateData(FALSE);
}
//Playfair密码
void CCryptographyDlg::Playfair()
{
UpdateData(TRUE);
m_StrCipInPut = "";
//构建Playfair密表
CString str[20];
char FileArray[5];
memset(&FileArray,0,5);
ifstream myFile;
int i = 0,j = 0,m = 0,n = 0,l = 0,q = 0,p = 0,row = 0,col = 0;
myFile.open("Playfair.txt",ios_base::in); //读取Playfair密表
while(myFile.getline(FileArray,20)&&i<5)
{
str[i].Format("%s",FileArray);
i++;
}
myFile.close();
CString str1;
m_StrCipInPut = m_StrPlaintTextInPut;
int Length = m_StrCipInPut.GetLength();
//相同对中的字母加分隔符(如x)
for (i = 0,j = 1;j<Length;i = i+2,j = j+2)
{
if (m_StrCipInPut.GetAt(i) == m_StrCipInPut.GetAt(j))
{
m_StrCipInPut.Insert(j,'x');
Length = Length+1;
}
}
//若明文长度为奇数则在其末尾添加一个x
if(Length%2 == 1)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -