📄 cipherdlg.cpp
字号:
// CipherDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Cipher.h"
#include "CipherDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CCipherDlg dialog
CCipherDlg::CCipherDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCipherDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCipherDlg)
m_vPlain = _T("");
m_caesar = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
int j=0;
for(char i='A';i<='Z';i++) //初始化字符数组
{
character[j]=i;
j++;
}
m_caesar=-1; //初始化加密方式
}
void CCipherDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCipherDlg)
DDX_Control(pDX, IDC_KEY, m_Key);
DDX_Control(pDX, IDC_CIPERTEXT, m_CiperText);
DDX_Text(pDX, IDC_PLAINTEXT, m_vPlain);
DDX_Radio(pDX, IDC_CAESAR, m_caesar);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCipherDlg, CDialog)
//{{AFX_MSG_MAP(CCipherDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CAESAR, OnCaesar)
ON_BN_CLICKED(IDC_COLUMN, OnColumn)
ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_VIGENERE, OnVigenere)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCipherDlg message handlers
BOOL CCipherDlg::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 CCipherDlg::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 CCipherDlg::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 CCipherDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCipherDlg::OnCaesar()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_PLAINTEXT)->SetWindowText("abcde");
m_Key.EnableWindow(false);
m_Key.SetWindowText("k=3");
m_CiperText.SetWindowText("");
flag=0; //选择凯撒
}
void CCipherDlg::OnColumn()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_PLAINTEXT)->SetWindowText("whatyoucanfromthisbook");
m_Key.SetWindowText("k=5");
m_Key.EnableWindow(false);
m_CiperText.SetWindowText("");
flag=2; //选择列换位
}
void CCipherDlg::OnEncrypt()
{
// TODO: Add your control notification handler code here
CString msg;
GetDlgItemText(IDC_PLAINTEXT,msg);
CString key;
GetDlgItemText(IDC_KEY,key);
switch(flag)
{
case 0:msg=Caesar(msg,msg.GetLength()); //选择凯撒算法
break;
case 1:msg=Vigenere(msg,key,msg.GetLength(),key.GetLength()); //选择维吉尼亚算法
break;
//msg=Vigenere();
case 2:msg=Column(msg,msg.GetLength()); //选择列换位算法
break;
//
default:AfxMessageBox("选择加密方法先!"); //处理出错
break;
}
m_CiperText.SetWindowText(msg);
}
void CCipherDlg::OnReset()
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_PLAINTEXT,"");
if(flag==1)
{
m_Key.EnableWindow(true);
m_Key.SetWindowText("");
}
m_CiperText.SetWindowText("");
}
void CCipherDlg::OnVigenere()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_PLAINTEXT)->SetWindowText("howareyou");
m_Key.SetWindowText("your");
m_Key.EnableWindow(true);
m_CiperText.SetWindowText("");
flag=1; //选择维吉尼亚
}
CString CCipherDlg::Column(CString m, int length=1) //列换位算法
{
int n=0,i=0,j=0,count=0;
if(m=="") //确定输入明文
{
AfxMessageBox("输入明文先!");
return("");
}
for(i=0;i<length;i++) //将小写转换为大写
if(m[i]>='a'&&m[i]<='z')
m.SetAt(i,m[i]-32);
if(length%5!=0) //确定矩阵的行数
{
n=length/5+1;
for(i=length;i<n*5;i++)
m.Insert(i,'X');
}
else
n=length/5;
CString tmp=m; //暂存变量
if(n>1) //列换位算法
{
for(i=0;i<=4;i++)
for(j=0;j<n;j++) //列优先赋值
{
int pos=i+5*j;
tmp.SetAt(count,m[pos]);
count++;
}
}
for(i=0;i<count;i++) //修改信息
m.SetAt(i,tmp[i]);
return m;
}
CString CCipherDlg::Vigenere(CString m, CString k, int lm=1, int lk=1) //维吉尼亚算法
{
int i=0;
CString tmp=m; //暂存变量
if(m=="") //确定输入明文
{
AfxMessageBox("输入明文先!");
}
if(k=="") //确定输入密钥
{
AfxMessageBox("输入密钥先!");
return("");
}
for(;i<lm;i++) //将密钥循环扩充至和明文相同的长度
tmp.SetAt(i,k[i%lk]);
k=tmp; //得到新的密钥
for(i=0;i<lm;i++)
{
if(m[i]>='a'&&m[i]<='z') //若明文字符为小写,转换为大写
m.SetAt(i,m[i]-32);
if(k[i]>='a'&&k[i]<='z') //若密钥字符为小写,转换为大写
k.SetAt(i,k[i]-32);
int pos=((m[i]-64)+(k[i]-64)-1)%26; //维吉尼亚算法,(列+行偏移) mod 26
if(pos==0) //确定临界值
pos=26;
m.SetAt(i,character[pos-1]);//修改信息
}
SetDlgItemText(IDC_KEY,k);
return m;
}
CString CCipherDlg::Caesar(CString m, int length) //凯撒算法
{
int i=0;
int k=3;
if(m=="") //确定输入明文
{
AfxMessageBox("输入明文先!");
return("");
}
for(;i<length;i++)
{
if(m[i]>='a'&&m[i]<='z') //若是小写,转换为大写
m.SetAt(i,m[i]-32);
int pos=(m[i]-65+k)%26; //凯撒算法
m.SetAt(i,character[pos]); //修改信息
}
return m;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -