C++实现古典密码-凯撒密码加密解密算法

第一部分 Caesar密码简介

1.1 基本思想

在密码学中,恺撒密码(英语:Caesar cipher),或称恺撒加密、恺撒变换、变换加密,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以罗马共和时期恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。

1.2 历史沿革

根据苏维托尼乌斯的记载,恺撒曾用此方法对重要的军事信息进行加密:
如果需要保密,信中便用暗号,也即是改变字母顺序,使局外人无法组成一个单词。如果想要读懂和理解它们的意思,得用第4个字母置换第一个字母,即以D代A,余此类推。
同样,奥古斯都也使用过类似方式,只不过他是把字母向右移动一位,而且末尾不折回。每当他用密语写作时,他都用B代表A,C代表B,其余的字母也依同样的规则;他用A代表Z。
另外,有证据表明,恺撒曾经使用过更为复杂的密码系统:文法学家普罗布斯曾经写过一份独具创新的手稿,研究恺撒书信中包含有秘密信息的字母。
已经无法弄清恺撒密码在当时有多大的效果,但是有理由相信它是安全的。因为恺撒大部分敌人都是目不识丁的,而其余的则可能将这些消息当作是某个未知的外语。即使有某个敌人获取了恺撒的加密信息,根据现有的记载,当时也没有任何技术能够解决这一最基本、最简单的替换密码。现存最早的破解方法记载在公元9世纪阿拉伯的阿尔·肯迪的有关发现频率分析的著作中。
文字来源百度百科

第二部分 Caesar密码的C++实现

我在代码中添加了详细的注释,采用面向对象编程的方法,希望本代码对你有所帮助:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
class Caesar {
private:
	int key;
public:
	Caesar(int k) :key(k) {}	//构造函数
	string encrypt(string cleartext) {	//使用移位密钥key对明文字母进行加密
		string ciphertext;
		char temp;
		int i, L;
		L = cleartext.size();
		for (i = 0; i < L; i++) {
			if (cleartext[i] >= 'A' && cleartext[i] <= 'Z') {
				temp = ((cleartext[i] - 'A') + key) % 26 + 'A';
			}
			else if (cleartext[i] >= 'a' && cleartext[i] <= 'z') {
				temp = ((cleartext[i] - 'a') + key) % 26 + 'a';
			}
			else
				temp = cleartext[i];
			ciphertext.push_back(temp);
		}
		return ciphertext;
	}
	vector<string>decrypt(string ciphertext) {	//使用穷举法进行破译
		vector<string>cleartextSet;
		string temptext;
		int i, k, L;
		char temp;
		L = ciphertext.size();
		for (k = 26; k >= 1; k--) {
			for (i = 0; i < L; i++) {
				if (ciphertext[i]>= 'A' && ciphertext[i] <= 'Z') {
					temp = ((ciphertext[i] - 'A') + k) % 26 + 'A';
				}
				else if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') {
					temp = ((ciphertext[i] - 'a') + k) % 26 + 'a';
				}
				else
					temp = ciphertext[i];
				temptext.push_back(temp);
			}
			cleartextSet.push_back(temptext);
			temptext.clear();
		}
		return cleartextSet;
	}
};
int main() {
	int mode, flag = 1;
	while (flag) {
		cout << "----------Welcome to StarKirby Caesar encrypt/decrypt system!----------" << endl;
		cout << "Please select the mode [1->encrypt 2->decrypt 3->exit]" << endl;
		cin >> mode;
		if (mode != 1 && mode != 2 && mode != 3) {
			cout << "Wrong input! Please check your select! [1->encrypt 2->decrypt 3->exit]" << endl;
			cin >> mode;
		}

		if (mode == 1) {
			int myKey;
			cout << "Please input the key: ";
			cin >> myKey;	//输入移位密钥
			Caesar myCaesar(myKey);
			string cleartext;	//待加密的明文
			string ciphertext;	//加密后的密文
			cout << "Please input the cleartext to encrypt: " << endl;
			getchar();
			getline(cin, cleartext);
			ciphertext = myCaesar.encrypt(cleartext);
			cout << "Successfully encrypt the text. The ciphertext is: " << endl;
			cout << ciphertext << endl;
		}
		else if (mode == 2) {
			vector<string>textset;	//用于存放明文
			string ciphertext;		//待解密的密文
			Caesar myCaesar(0);
			cout << "Please input the ciphertext to decrypt: " << endl;
			getchar();
			getline(cin, ciphertext);
			textset = myCaesar.decrypt(ciphertext);
			int k;
			for (k = 0; k < textset.size(); k++) {
				cout << "If (key mod 26) == " << k << ": " << endl;
				cout << textset[k] << endl;
			}
		}
		else {
			cout << "----------Thank you for using!----------" << endl;
			return 0;
		}
		cout << "Continue or not? [0->exit 1 or other->continue]: ";
		cin >> flag;
	}
	return 0;
}

当然有兴趣的同学可以将标准输入输出流改成文件输入输出来进行,具体实现请参见我的另外一篇博客:C++实现DES加密解密算法
P.S. 本代码在VS2019中调试通过!

  • 5
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值