物联网安全技术——凯撒密码

#include<iostream>
#include<string>
#include<vector>
#include<cctype>
#include<fstream>
#include<sstream>
using namespace std;
vector<string> EncryptedMessage;
vector<string> word;
vector<vector<string>> dic;

string encryption(const string message,int i) {
	string m = message;
	for (auto j = 0; j < m.length(); j++) {
		if (isalpha(m[j])) {
			if (isupper(m[j])) {
				m[j] = (m[j] - 'A' + i) % 26 + 'A';
			}
			else {
				m[j] = (m[j] - 'a' + i) % 26 + 'a';
			}
		}
	}
	//cout << message << endl;
	return m;
}

string decryption(string m,int i) {
	string c = m;
	for (auto j = 0; j < m.length(); j++) {
		if (isalpha(m[j])) {
			if (isupper(m[j])) {
				c[j] = (m[j] - 'A' + 26 - i) % 26 + 'A';
			}
			else {
				c[j] = (m[j] - 'a' + 26 - i) % 26 + 'a';
			}
		}
	}
	
	return c;
}

void readFile() {
	string path = "《英汉汉英词典》.txt";
	fstream file;
	string ReadMessage;
	file.open(path);
	string s;
	int count = 0;
	if (file.is_open()) {
		cout << "文件已打开" << endl;
		while (getline(file, s))
		{
			int tt = tolower(s[0]) - 'a';
			for (auto i = 0; i < s.length(); i++) {
				if (isspace(s[i])) {
					int index = i;
					s = s.substr(0, index);
					s[0] = tolower(s[0]);
					word.push_back(s);
					//cout << s << " " << s.length() << endl;
					/*if (tt != s[0] - 'a') {
						dic.push_back(word);
						word.clear();
						word.push_back(s);
						count++;
					}
					else {
						word.push_back(s);
						count++;
					}*/
					//cout << s << endl;
				}
			}
		}
	/*	cout << count << endl;*/
	}

}

vector<string> splitWord(string m) {
	vector<string> vec;
	int index1 = 0, index2;
	string word;
	for (auto i = 0; i < m.length(); i++) {
		if (m[i] == ' '||ispunct(m[i])||i==m.length()-1) {
			if (i == m.length() - 1) {
				index2 = i + 1;
			}
			else {
				index2 = i;
			}
			word = m.substr(index1, index2 - index1);
			index1 = index2+1;
			word[0] = tolower(word[0]);
			vec.push_back(word);
		}
	}
	return vec;
}

int searchWord(string w) {
	for (auto i = 0; i < word.size(); i++) {
		if (w == word[i]) {
			return 1;
		}
		
	}
	return 0;
}


double getRate(string m) {
	double count = 0.0;
	vector<string> vec = splitWord(m);
	for (auto i = 0; i < vec.size(); i++) {
		//cout << vec[i] << endl;
		if (searchWord(vec[i])) {
			count++;
		}
	}
	double rate = count / vec.size();
	return rate;
}


int main() {
	readFile();
	double rate = 0;
	string correctMessage;
	string message = "Liu Weifu, meet me at the party"; //明文
	int key = 5;
	string m = encryption(message, key);
	cout <<"密文 "<< m << endl;
	for (auto i = 1; i < 26; i++) {	
		EncryptedMessage.push_back(decryption(m, i));
	}
	for (auto k = 0; k < EncryptedMessage.size(); k++) {
		cout << "key = " << k+1 << " ";
		cout << EncryptedMessage[k] << " ";
		cout << getRate(EncryptedMessage[k]) << endl;
		if (rate < getRate(EncryptedMessage[k])) {
			rate = getRate(EncryptedMessage[k]);
			correctMessage = EncryptedMessage[k];
		}
	}
	cout << "明文为:" << correctMessage << endl;
	system("pause");
	return 0;
}
凯撒密码(caeser)是罗马扩张时期朱利斯•凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令。它将字母表中的字母移动一定位置而实现加密。 古罗马随笔作家修托尼厄斯在他的作品中披露,凯撒常用一种“密表”给他的朋友写信。这里所说的密表,在密码学上称为“凯撒密表”。用现代的眼光看,凯撒密表是一种相当简单的加密变换,就是把明文中的每一个字母用它在字母表上位置后面的第三个字母代替。古罗马文字就是现在所称的拉丁文,其字母就是我们从英语中熟知的那26个拉丁字母。因此,凯撒密表就是用d代a,用e代b,……,用z代w。这些代替规则也可用一张表格来表示,所以叫“密表”。 基本原理 在密码学中存在着各种各样的置换方式,但所有不同的置换方式都包含2个相同的元素。密钥和协议(算法)。凯撒密码的密钥是3,算法是将普通字母表中的字母用密钥对应的字母替换。置换加密的优点就在于它易于实施却难于破解. 发送方和接收方很容易事先商量好一个密钥,然后通过密钥从明文中生成密文,即是敌人若获取密文,通过密文直接猜测其代表的意义,在实践中是不可能的。 凯撒密码的加密算法极其简单。其加解密过程如下: 加密解密算法 凯撒密码的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左或向右移动一个固定数目的位置。例如,当偏移量是左移3的时候(解密时的密钥就是3): 明文字母表:ABCDEFGHIJKLMNOPQRSTUVWXYZ 密文字母表:DEFGHIJKLMNOPQRSTUVWXYZABC 使用时,加密者查找明文字母表中需要加密的消息中的每一个字母所在位置,并且写下密文字母表中对应的字母。需要解密的人则根据事先已知的密钥反过来操作,得到原来的明文。例如: 明文:THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG 密文:WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ 凯撒密码的加密、解密方法还能够通过同余数的数学方法进行计算。首先将字母用数字代替,A=0,B=1,...,Z=25。此时偏移量为n的加密方法即为: E (x)= (x+n) mod 26 解密就是: D (x)= (x-n) mod 26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值