实验: 古典加密算法 caesar cipher

<2011_0123>

caesar cipher

 

参考资料:

 

http://www.codeproject.com/KB/recipes/Encryption_Techniques.aspx?display=Print
Classical Encryption Techniques

 

<<密码学导引>>

 

<2011_0123_1836><home><LostSpeed><caesarCipher>
* 实现了caesar Cipher的标准编解码, 选择性编码, 暴力解码.
* 封装了CCaesarCipher类, 方便以后复用.

实验: 古典加密算法 caesar cipher_第1张图片

 

实验: 古典加密算法 caesar cipher_第2张图片

实验: 古典加密算法 caesar cipher_第3张图片

实验: 古典加密算法 caesar cipher_第4张图片

// CaesarCipher.h: interface for the CCaesarCipher class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_CAESARCIPHER_H__C5B13609_B24E_47CA_8DA2_2B05BE39D4CF__INCLUDED_) #define AFX_CAESARCIPHER_H__C5B13609_B24E_47CA_8DA2_2B05BE39D4CF__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 /** * @note * caesar cipher 明文有效输入为: a~z, ' ' * 无效输入原样照印到密文 * * caesar cipher 密文有效输入为: A~Z, ' ' * 无效输入原样照印到明文 * * 总结: 如果不是有效输入, 解码后无法得到正确的明文. * 比如编码前明文中有大写字符 */ /** caesar cipher 原始字母移位数 */ #define G_INT_CAESARCIPHER_LETTER_SHIFTING_NUMBER (INT)3 /** caesar cipher 原始移位方向是向右移动 */ #define G_INT_CAESARCIPHER_SHIFTING_DIRECTION_LEFT (INT)1 #define G_INT_CAESARCIPHER_SHIFTING_DIRECTION_RIGHT (INT)0 const char szCaesarCipherInCharset[] = "abcdefghijklmnopqrstuvwxyz"; const char szCaesarCipherOutCharset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; class CCaesarCipher { public: CCaesarCipher(); virtual ~CCaesarCipher(); public: INT CheckInput_PlainText(CString strIn);/**< 编码前, 检查明文输入 */ INT CheckInput_CipherText(CString strIn);/**< 解码前, 检查密文输入 */ INT Encode();/**< 编码 */ INT Decode();/**< 解码 */ INT DecodeByForce();/**< 遍历nKey, 输出所有的解码结果 */ void SetPlainText( CString strPlainText, INT nKey = G_INT_CAESARCIPHER_LETTER_SHIFTING_NUMBER, INT nDirection = G_INT_CAESARCIPHER_SHIFTING_DIRECTION_RIGHT); CString GetCipherText(); /** * @name SetCipherText * @param CString strCipherText, 给定的密文 * @param INT nKey, 原始明文的密钥 * @param INT nDirection, 原始明文的移位方向 * @return CString */ void SetCipherText( CString strCipherText, INT nKey = G_INT_CAESARCIPHER_LETTER_SHIFTING_NUMBER, INT nDirection = G_INT_CAESARCIPHER_SHIFTING_DIRECTION_RIGHT); CString GetPlainText(); private: CHAR EncodeChar(CHAR cPlainChar); CHAR EncodeChar(CHAR cPlainChar, PCHAR pcCharset, INT nKey, INT nDirection); CHAR DecodeChar(CHAR cCipherChar); CHAR DecodeChar(CHAR cCipherChar, PCHAR pcCharset, INT nKey, INT nDirection); CHAR DecodeChar(CHAR cCipherChar, INT nKey, INT nDirection); BOOL IsOneOfCharset(CString strCharset, CHAR cIn); void SetKey(INT nKey, INT nDirection); private: CString m_strPlainText;/**< 明文 */ INT m_nKey;/**< 凯撒密码的密钥 */ INT m_nDirection;/**< 密钥的移位方向 */ CString m_strCipherext;/**< 密文 */ }; #endif // !defined(AFX_CAESARCIPHER_H__C5B13609_B24E_47CA_8DA2_2B05BE39D4CF__INCLUDED_)

// CaesarCipher.cpp: implementation of the CCaesarCipher class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "FrameworkOfXtremeDlg.h" #include "CaesarCipher.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CCaesarCipher::CCaesarCipher() : m_nKey(G_INT_CAESARCIPHER_LETTER_SHIFTING_NUMBER), m_nDirection(G_INT_CAESARCIPHER_SHIFTING_DIRECTION_RIGHT) { } CCaesarCipher::~CCaesarCipher() { } BOOL CCaesarCipher::IsOneOfCharset(CString strCharset, CHAR cIn) { CString strIn = cIn; return (-1 != strCharset.FindOneOf(strIn)); } CHAR CCaesarCipher::EncodeChar(CHAR cPlainChar, PCHAR pcCharset, INT nKey, INT nDirection) { INT n = 0; INT nOffsetChar = 'A' - 'a'; CHAR cCipher = cPlainChar + nOffsetChar; INT nLenCharset = strlen(pcCharset); for(n = 0; n < nLenCharset; n++) { if(cCipher == *(pcCharset + n)) break; } if(G_INT_CAESARCIPHER_SHIFTING_DIRECTION_RIGHT == nDirection) n += nKey; else n -= nKey; if(n < 0) n += nLenCharset; else if((n + 1) > nLenCharset) n -= nLenCharset; cCipher = *(pcCharset + n); return cCipher; } CHAR CCaesarCipher::EncodeChar(CHAR cPlainChar) { CHAR cCipher = '/0'; if(IsOneOfCharset(szCaesarCipherInCharset, cPlainChar)) cCipher = EncodeChar(cPlainChar, (PCHAR)szCaesarCipherOutCharset, m_nKey, m_nDirection); else cCipher = cPlainChar; return cCipher; } CHAR CCaesarCipher::DecodeChar(CHAR cCipherChar, PCHAR pcCharset, INT nKey, INT nDirection) { INT n = 0; INT nOffsetChar = 'A' - 'a'; CHAR cPlain = cCipherChar - nOffsetChar; INT nLenCharset = strlen(pcCharset); for(n = 0; n < nLenCharset; n++) { if(cPlain == *(pcCharset + n)) break; } if(G_INT_CAESARCIPHER_SHIFTING_DIRECTION_RIGHT == nDirection) n -= nKey; else n += nKey; if(n < 0) n += nLenCharset; else if((n + 1) > nLenCharset) n -= nLenCharset; cPlain = *(pcCharset + n); return cPlain; } CHAR CCaesarCipher::DecodeChar(CHAR cCipherChar) { CHAR cPlain = '/0'; if(IsOneOfCharset(szCaesarCipherOutCharset, cCipherChar)) cPlain = DecodeChar(cCipherChar, (PCHAR)szCaesarCipherInCharset, m_nKey, m_nDirection); else cPlain = cCipherChar; return cPlain; } CHAR CCaesarCipher::DecodeChar(CHAR cCipherChar, INT nKey, INT nDirection) { CHAR cPlain = '/0'; if(IsOneOfCharset(szCaesarCipherOutCharset, cCipherChar)) cPlain = DecodeChar(cCipherChar, (PCHAR)szCaesarCipherInCharset, nKey, nDirection); else cPlain = cCipherChar; return cPlain; } INT CCaesarCipher::CheckInput_PlainText(CString strIn) {/**< 编码前, 检查明文输入 */ INT nRc = S_OK; CHAR cTmp = '/0'; int n = 0; int nLen = strIn.GetLength(); for(n = 0; n < nLen; n++) { cTmp = strIn.GetAt(n); if(!IsOneOfCharset(szCaesarCipherInCharset, cTmp) && cTmp != ' ') { nRc = S_FALSE; break; } } return nRc; } INT CCaesarCipher::CheckInput_CipherText(CString strIn) {/**< 解码前, 检查密文输入 */ INT nRc = S_OK; CHAR cTmp = '/0'; int n = 0; int nLen = strIn.GetLength(); for(n = 0; n < nLen; n++) { cTmp = strIn.GetAt(n); if(!IsOneOfCharset(szCaesarCipherOutCharset, cTmp) && cTmp != ' ') { nRc = S_FALSE; break; } } return nRc; } INT CCaesarCipher::Encode() {/**< 编码 */ CHAR cTmp = '/0'; CHAR cTmpShifting = '/0'; INT n = 0; INT nLenPlainText = m_strPlainText.GetLength(); m_strCipherext.Empty(); if(nLenPlainText <= 0) return S_FALSE; for(n = 0; n < nLenPlainText; n++) { cTmp = m_strPlainText.GetAt(n); cTmpShifting = EncodeChar(cTmp); m_strCipherext += cTmpShifting; } return S_OK; } INT CCaesarCipher::Decode() {/**< 解码 */ CHAR cTmp = '/0'; CHAR cTmpShifting = '/0'; INT n = 0; INT nLenCipherText = m_strCipherext.GetLength(); m_strPlainText.Empty(); if(nLenCipherText <= 0) return S_FALSE; for(n = 0; n < nLenCipherText; n++) { cTmp = m_strCipherext.GetAt(n); cTmpShifting = DecodeChar(cTmp); m_strPlainText += cTmpShifting; } return S_OK; } INT CCaesarCipher::DecodeByForce() {/**< 遍历nKey, 输出所有的解码结果 */ CString strTmp; CString strPlainTmp; CHAR cTmp = '/0'; CHAR cTmpShifting = '/0'; INT n = 0; INT nLenCipherText = m_strCipherext.GetLength(); m_strPlainText.Empty(); if(nLenCipherText <= 0) return S_FALSE; INT nKeyBegin = 0; INT nKeyEnd = 25; INT nShiftingDirectionBegin = 0; INT nShiftingDirectionEnd = 1; INT nKey = 0; INT nShiftingDirection = 0; for(nShiftingDirection = nShiftingDirectionBegin; nShiftingDirection <= nShiftingDirectionEnd; nShiftingDirection++) { for(nKey = nKeyBegin; nKey <= nKeyEnd; nKey++) { strPlainTmp.Empty(); for(n = 0; n < nLenCipherText; n++) { cTmp = m_strCipherext.GetAt(n); cTmpShifting = DecodeChar(cTmp, nKey, nShiftingDirection); strPlainTmp += cTmpShifting; } strTmp.Format("[密钥=%d][方向=%s]明文[%s]", nKey, (G_INT_CAESARCIPHER_SHIFTING_DIRECTION_LEFT == nShiftingDirection) ? "左" : "右", strPlainTmp); m_strPlainText += strTmp; m_strPlainText += "/r/n"; } } return S_OK; } void CCaesarCipher::SetCipherText(CString strCipherText, INT nKey, INT nDirection) { m_strCipherext = strCipherText; SetKey(nKey, nDirection); } CString CCaesarCipher::GetPlainText() { return m_strPlainText; } void CCaesarCipher::SetPlainText(CString strPlainText, INT nKey, INT nDirection) { m_strPlainText = strPlainText; SetKey(nKey, nDirection); } CString CCaesarCipher::GetCipherText() { return m_strCipherext; } void CCaesarCipher::SetKey(INT nKey, INT nDirection) { if(nKey >= 0) m_nKey = nKey; if(nDirection >= 0) m_nDirection = nDirection; }

void CMainiDlg::OnEncode() { UpdateData(TRUE); if(S_OK != m_CaesarCipher.CheckInput_PlainText(m_strPlainText)) { AfxMessageBox("明文有效范围: 'a'~'z' 和 ' ', 请检查输入的明文"); return; } /** * 如果采用变形的 caesar cipher, 需要指定nKey, nDirection * 如果采用caesar cipher原始定义, 不需要指定nKey, nDirection */ //m_CaesarCipher.SetPlainText(m_strPlainText, -1, -1); m_CaesarCipher.SetPlainText(m_strPlainText); m_CaesarCipher.Encode(); m_strCipherText = m_CaesarCipher.GetCipherText(); UpdateData(FALSE); }

void CMainiDlg::OnDecode() { UpdateData(TRUE); if(S_OK != m_CaesarCipher.CheckInput_CipherText(m_strCipherText)) { AfxMessageBox("密文有效范围: 'A'~'Z' 和 ' ', 请检查输入的密文"); return; } /** * 如果采用变形的 caesar cipher, 需要指定nKey, nDirection * 如果采用caesar cipher原始定义, 不需要指定nKey, nDirection */ //m_CaesarCipher.SetCipherText(m_strCipherText, -1, -1); m_CaesarCipher.SetCipherText(m_strCipherText); m_CaesarCipher.Decode(); m_strPlainText = m_CaesarCipher.GetPlainText(); UpdateData(FALSE); }

void CMainiDlg::OnEncodeParam() { UpdateData(TRUE); if(S_OK != m_CaesarCipher.CheckInput_PlainText(m_strPlainText)) { AfxMessageBox("明文有效范围: 'a'~'z' 和 ' ', 请检查输入的明文"); return; } int nKey = m_ctrKey.GetCurSel(); int nDirection = m_ctrDirection.GetCurSel(); /** * 如果采用变形的 caesar cipher, 需要指定nKey, nDirection * 如果采用caesar cipher原始定义, 不需要指定nKey, nDirection */ //m_CaesarCipher.SetPlainText(m_strPlainText, -1, -1); m_CaesarCipher.SetPlainText(m_strPlainText, nKey, nDirection); m_CaesarCipher.Encode(); m_strCipherText = m_CaesarCipher.GetCipherText(); UpdateData(FALSE); }

void CMainiDlg::OnDecodeForce() { UpdateData(TRUE); if(S_OK != m_CaesarCipher.CheckInput_CipherText(m_strCipherText)) { AfxMessageBox("密文有效范围: 'A'~'Z' 和 ' ', 请检查输入的密文"); return; } /** * 如果采用变形的 caesar cipher, 需要指定nKey, nDirection * 如果采用caesar cipher原始定义, 不需要指定nKey, nDirection */ //m_CaesarCipher.SetCipherText(m_strCipherText, -1, -1); m_CaesarCipher.SetCipherText(m_strCipherText); m_CaesarCipher.DecodeByForce(); m_strPlainText = m_CaesarCipher.GetPlainText(); UpdateData(FALSE); }

工程下载点: http://download.csdn.net/source/2998890

 

你可能感兴趣的:(加密,算法,File,360,encryption,interface)