基于VC的Ceasar加密和解密技术
一、Clear原理
对每一明文字母P,代换成密文字母C:
C=E(P)=(P+3) mod (26)
移位就可以是任意整数K,这样就得到了一般的Ceasar算法:
C=E(P)=(P+K) mod (26)
这里K的取值范围从1到25。解密算法是:
P=D(C)=(C-K)mod (26)
二、开发平台及语言
1、开发平台:Microsoft Visual Studio c++ 6.0
2.语言: c++
三、应用程序界面设计及变量的连接和方法的名称
对象
| ID | Caption |
连接变量或方法
|
成组框 | IDC_STATIC | 产生密钥 | |
编辑框 | IDC_NUMBER_EDIT | 无 | m_NumberEdit/CEdit |
编辑框 | IDC_RESOURCE_EDIT | 无 |
m_ResourceEdit/CEdit
OnChangeResourceEdit()
|
编辑框 | IDC_ENCRYPT_EDIT | 无 | m_EncryptEdit/CEdit |
编辑框 | IDC_OPEN_EDIT | 无 | m_OpenEdit/CEdit |
编辑框 | IDC_LINE | 无 | m_Line/CEdit |
编辑框 | IDC_CHAR | 无 | m_Char/CEdit |
编辑框 | IDC_KEY_EDIT | 无 | m_KeyEdit/CEdit |
命令按钮 | IDC_CLEARNUM_BUTTON | 重置 | OnClearnumButton() |
命令按钮 | IDC_HIDEKEY_BUTTON | 隐藏密钥 | OnHidekeyButton() |
命令按钮 | IDC_SHOWKEY_BUTTON | 显示密钥 | OnShowkeyButton() |
命令按钮 | IDC_CLEARKEY_BUTTON | 重置 | OnClearkeyButton() |
命令按钮 | IDC_HIDETEXT_BUTTON | 隐藏密文 | OnHidetextButton() |
命令按钮 | IDC_SHOWTEXT_BUTTON | 显示密文 | OnShowtextButton() |
命令按钮 | IDC_OK_BUTTON | 确定 | OnOkButton() |
命令按钮 | IDC_CLEAR_BUTTON | Clear | OnClearButton() |
命令按钮 | IDC_EXIT_BUTTON | Exit | OnExitButton() |
命令按钮 | IDC_ENCRYPT_BUTTON | 加密 | OnEncryptButton() |
命令按钮 | IDC_OPEN_BUTTON | 解密 | OnOpenButton() |
编辑框 | IDC_FORCE_OPEN_EDIT | 无 | m_ForceOpenEdit/CEdit |
编辑框 | IDC_GETENCRYPT_EDIT | 无 | m_GetEncryptEdit/CEdit |
菜单 | ID_ENCRYPT_MENU | 加密 | OnEncryptMenu() |
菜单 | ID_OPEN_MENU | 解密 | OnOpenMenu() |
菜单 | ID_CLEAR_MENU | 清除 | OnClearMenu() |
菜单 | ID_EXIT_MENU | 退出 | OnExitMenu() |
菜单 | ID_ABOUT_MENU | 关于 | OnAboutMenu() |
四、CeasarDlg.h声明
// CeasarDlg.h : header file
#if !defined(AFX_CEASARDLG_H__E2ECB937_D748_475D_BDA2_6D8728998EFC__INCLUDED_)
#define AFX_CEASARDLG_H__E2ECB937_D748_475D_BDA2_6D8728998EFC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
Const char lowerArray[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'};
const char upperArray[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'};
/////////////////////////////////////////////////////////////////////////////
// CCeasarDlg dialog
class CCeasarDlg : public CDialog
{
// Construction
public:
CCeasarDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CCeasarDlg)
enum { IDD = IDD_CEASAR_DIALOG };
CEdit m_GetEncryptEdit;
CEdit m_GetEnryptEdit;
CEdit m_ForceOpenEdit;
CEdit m_NumberEdit;
CEdit m_KeyEdit;
CEdit m_EncryptEdit;
CEdit m_ResourceEdit;
CEdit m_OpenEdit;
CEdit m_Line;
CEdit m_Char;
CEdit m_keyEdit;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCeasarDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CCeasarDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnClearkeyButton();
afx_msg void OnHidekeyButton();
afx_msg void OnShowkeyButton();
afx_msg void OnChangeResourceEdit();
afx_msg void OnClearButton();
afx_msg void OnExitButton();
afx_msg void OnHidetextButton();
afx_msg void OnShowtextButton();
afx_msg void OnEncryptButton();
afx_msg void OnOpenButton();
afx_msg void OnOkButton();
afx_msg void OnClearnumButton();
afx_msg void OnEncryptMenu();
afx_msg void OnOpenMenu();
afx_msg void OnExitMenu();
afx_msg void OnClearMenu();
afx_msg void OnAboutMenu();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CEASARDLG_H__E2ECB937_D748_475D_BDA2_6D8728998EFC__INCLUDED_)
五、映射消息的应用程序代码
1、void CCeasarDlg::OnClearkeyButton()
{
// TODO: Add your control notification handler code here
m_KeyEdit.SetSel(0,-1);
m_KeyEdit.ReplaceSel("");
}
2、void CCeasarDlg::OnHidekeyButton()
{
// TODO: Add your control notification handler code here
m_KeyEdit.ShowWindow(SW_HIDE);
}
3、void CCeasarDlg::OnShowkeyButton()
{
// TODO: Add your control notification handler code here
m_KeyEdit.ShowWindow(SW_SHOW);
}
4、void CCeasarDlg::OnChangeResourceEdit()
{
// TODO: Add your control notification handler code here
CString METext;
char Count[10];
int nCharNum,nLineNum;
m_ResourceEdit.GetWindowText(METext);
nCharNum=METext.GetLength();
nLineNum=m_ResourceEdit.GetLineCount();
itoa(nCharNum,Count,10);
m_Char.SetWindowText(Count);
itoa(nLineNum,Count,10);
m_Line.SetWindowText(Count);
}
5、void CCeasarDlg::OnClearButton()
{
// TODO: Add your control notification handler code here
m_ResourceEdit.SetSel(0,-1);
m_ResourceEdit.ReplaceSel("");
m_EncryptEdit.SetSel(0,-1);
m_EncryptEdit.ReplaceSel("");
m_OpenEdit.SetSel(0,-1);
m_OpenEdit.ReplaceSel("");
UpdateData(FALSE);
}
6、void CCeasarDlg::OnExitButton()
{
// TODO: Add your control notification handler code here
OnOK();
}
7、void CCeasarDlg::OnHidetextButton()
{
// TODO: Add your control notification handler code here
m_EncryptEdit.ShowWindow(SW_HIDE);
}
8、void CCeasarDlg::OnShowtextButton()
{
// TODO: Add your control notification handler code here
m_EncryptEdit.ShowWindow(SW_SHOW);
}
9、void CCeasarDlg::OnEncryptButton()
{
// TODO: Add your control notification handler code here
char sArray[10];
int nCount;
int nKey;
m_Char.GetWindowText(sArray,10);
nCount=atoi(sArray);
m_KeyEdit.GetWindowText(sArray,10);
nKey=atoi(sArray);
char sResourceText[300];
char sEncryptText[300];
char *pResourceText=sResourceText;
char *pEncryptText=sEncryptText;
m_ResourceEdit.GetWindowText(sResourceText,300);
int nTemp1,nTemp2,i,j;
for(i=0;i<nCount;i++)
{
if((*pResourceText>='A'&&*pResourceText<='Z')||(*pResourceText>='a'&&*pResourceText<='z'))
{
if(*pResourceText>='A'&&*pResourceText<='Z')
{
for(j=0;j<26;j++)
if(upperArray[j]==*pResourceText)
{
nTemp1=j;
break;
}
nTemp2=(nTemp1+nKey%26)%26;
*pEncryptText=upperArray[nTemp2];
}
else
{
for(j=0;j<26;j++)
if(lowerArray[j]==*pResourceText)
{
nTemp1=j;
break;
}
nTemp2=(nTemp1+nKey%26)%26;
*pEncryptText=lowerArray[nTemp2];
}
}
else
*pEncryptText=*pResourceText;
pResourceText++;
pEncryptText++;
}
*pEncryptText='\0';
m_EncryptEdit.SetWindowText(sEncryptText);
}
10、void CCeasarDlg::OnOpenButton()
{
// TODO: Add your control notification handler code here
char sArray[10];
int nCount;
int nKey;
m_Char.GetWindowText(sArray,10);
nCount=atoi(sArray);
m_KeyEdit.GetWindowText(sArray,10);
nKey=atoi(sArray);
char sOpenText[300];
char sEncryptText[300];
char *pOpenText=sOpenText;
char *pEncryptText=sEncryptText;
m_EncryptEdit.GetWindowText(sEncryptText,300);
int nTemp1,nTemp2,i,j;
for(i=0;i<nCount;i++)
{
if((*pEncryptText>='A'&&*pEncryptText<='Z')||(*pEncryptText>='a'&&*pEncryptText<='z'))
{
if(*pEncryptText>='A'&&*pEncryptText<='Z')
{
for(j=0;j<26;j++)
if(upperArray[j]==*pEncryptText)
{
nTemp1=j;
break;
}
nTemp2=(nTemp1-nKey%26+26)%26;
*pOpenText=upperArray[nTemp2];
}
else
{
for(j=0;j<26;j++)
if(lowerArray[j]==*pEncryptText)
{
nTemp1=j;
break;
}
nTemp2=(nTemp1-nKey%26+26)%26;
*pOpenText=lowerArray[nTemp2];
}
}
else
*pOpenText=*pEncryptText;
pEncryptText++;
pOpenText++;
}
*pOpenText='\0';
m_OpenEdit.SetWindowText(sOpenText);
}
11、void CCeasarDlg::OnOkButton()
{
// TODO: Add your control notification handler code here
char sArray[10];
int nCount;
int nNumber;
m_Char.GetWindowText(sArray,10);
nCount=atoi(sArray);
m_NumberEdit.GetWindowText(sArray,10);
nNumber=atoi(sArray);
char sForceOpenText[300];
char sGetEncryptText[300];
char *pForceOpenText=sForceOpenText;
char *pGetEncryptText=sGetEncryptText;
m_GetEncryptEdit.GetWindowText(sGetEncryptText,300);
int nTemp1,nTemp2,i,j;
for(i=0;i<nCount;i++)
{
if((*pGetEncryptText>='A'&&*pGetEncryptText<='Z')||(*pGetEncryptText>='a'&&*pGetEncryptText<='z'))
{
if(*pGetEncryptText>='A'&&*pGetEncryptText<='Z')
{
for(j=0;j<26;j++)
if(upperArray[j]==*pGetEncryptText)
{
nTemp1=j;
break;
}
nTemp2=(nTemp1-nNumber+26)%26;
*pForceOpenText=upperArray[nTemp2];
}
else
{
for(j=0;j<26;j++)
if(lowerArray[j]==*pGetEncryptText)
{
nTemp1=j;
break;
}
nTemp2=(nTemp1-nNumber+26)%26;
*pForceOpenText=lowerArray[nTemp2];
}
}
else
*pForceOpenText=*pGetEncryptText;
pGetEncryptText++;
pForceOpenText++;
}
*pForceOpenText='\0';
m_ForceOpenEdit.SetWindowText(sForceOpenText);
}
12、void CCeasarDlg::OnClearnumButton()
{
// TODO: Add your control notification handler code here
m_NumberEdit.SetSel(0,-1);
m_NumberEdit.ReplaceSel("");
m_ForceOpenEdit.SetSel(0,-1);
m_ForceOpenEdit.ReplaceSel("");
m_GetEncryptEdit.SetSel(0,-1);
m_GetEncryptEdit.ReplaceSel("");
}
13、void CCeasarDlg::OnEncryptMenu()
{
// TODO: Add your command handler code here
OnEncryptButton();
}
14、void CCeasarDlg::OnOpenMenu()
{
// TODO: Add your command handler code here
OnOpenButton();
}
15、void CCeasarDlg::OnExitMenu()
{
// TODO: Add your command handler code here
OnExitButton();
}
16、void CCeasarDlg::OnClearMenu()
{
// TODO: Add your command handler code here
OnClearButton();
}
17、void CCeasarDlg::OnAboutMenu()
{
// TODO: Add your command handler code here
MessageBox("This is a Clear!");
}
本文介绍了一种基于VC++的Ceasar加密和解密技术实现方案,详细展示了使用Microsoft Visual Studio C++ 6.0进行开发的具体步骤,包括应用程序界面设计、变量连接和方法命名等,并提供了部分核心代码示例。
1296

被折叠的 条评论
为什么被折叠?



