---
title: "基于VC的Ceasar加密和解密技术"
author: "zero_dian"
date: "2006-01-07T13:19:00Z"
tags: []
slug: "vc-ceasar"
draft: false
lastmod: "2006-01-07T13:19:00Z"
id: 442646
url: "https://zero-dian.iteye.com/blog/442646"
---

**基于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++

**三、应用程序界面设计及变量的连接和方法的名称**

**![](/images/blog_csdn_net/zero_dian/Ceasar.JPG)**

**对象**

**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 &gt; 1000

#pragma once

#endif // \_MSC\_VER &gt; 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&lt;nCount;i++)

{

if((\*pResourceText&gt;='A'&&\*pResourceText&lt;='Z')||(\*pResourceText&gt;='a'&&\*pResourceText&lt;='z'))

{

if(\*pResourceText&gt;='A'&&\*pResourceText&lt;='Z')

{

for(j=0;j&lt;26;j++)

if(upperArray\[j]==\*pResourceText)

{

nTemp1=j;

break;

}

nTemp2=(nTemp1+nKey%26)%26;

\*pEncryptText=upperArray\[nTemp2];

}

else

{

for(j=0;j&lt;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&lt;nCount;i++)

{

if((\*pEncryptText&gt;='A'&&\*pEncryptText&lt;='Z')||(\*pEncryptText&gt;='a'&&\*pEncryptText&lt;='z'))

{

if(\*pEncryptText&gt;='A'&&\*pEncryptText&lt;='Z')

{

for(j=0;j&lt;26;j++)

if(upperArray\[j]==\*pEncryptText)

{

nTemp1=j;

break;

}

nTemp2=(nTemp1-nKey%26+26)%26;

\*pOpenText=upperArray\[nTemp2];

}

else

{

for(j=0;j&lt;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&lt;nCount;i++)

{

if((\*pGetEncryptText&gt;='A'&&\*pGetEncryptText&lt;='Z')||(\*pGetEncryptText&gt;='a'&&\*pGetEncryptText&lt;='z'))

{

if(\*pGetEncryptText&gt;='A'&&\*pGetEncryptText&lt;='Z')

{

for(j=0;j&lt;26;j++)

if(upperArray\[j]==\*pGetEncryptText)

{

nTemp1=j;

break;

}

nTemp2=(nTemp1-nNumber+26)%26;

\*pForceOpenText=upperArray\[nTemp2];

}

else

{

for(j=0;j&lt;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!");

}