用C++写一个程序实现用凯撒密码对字符串进行加密 输入格式:共两行,一行若干个大写英文字母及空格组成的字符串信息,长度小于255. 输出格式:一行字符串,为上述信息经过凯撒加密后的结果
时间: 2024-05-02 08:22:59 浏览: 14
好的,关于您的问题,我会为您提供代码实现:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[255];
int key;
fgets(str, 255, stdin);
scanf("%d", &key);
for (int i = 0; i < strlen(str); i++) {
char c = str[i];
if (c == ' ') { // 空格不需加密
printf(" ");
} else {
int num = c - 'A'; // 将字符转换为 0~25 之间的数字
num = (num + key) % 26; // 加密
printf("%c", num + 'A'); // 将数字转换为字符
}
}
return 0;
}
```
输入样例:
```
HELLO WORLD
3
```
输出样例:
```
KHOOR ZRUOG
```
相关问题
编写一个C++程序,实现对凯撒加密算法的破解,输入恺撒加密的密文,输出破解后的明文(也是大写字母构成)。
下面是一个简单的C++程序,用于破解凯撒加密算法。该程序可以读取一个密文文件,并输出破解后的明文。程序假设密文只包含大写字母,密钥是一个整数。密钥从0到25进行枚举,程序会将每个密钥应用于密文,并输出解密后的明文。程序使用了词频分析来帮助破解密文。
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
// 词频表,记录每个字母出现的频率
map<char, int> freq_table = {
{'A', 8167},
{'B', 1492},
{'C', 2782},
{'D', 4253},
{'E', 12702},
{'F', 2228},
{'G', 2015},
{'H', 6094},
{'I', 6966},
{'J', 153},
{'K', 772},
{'L', 4025},
{'M', 2406},
{'N', 6749},
{'O', 7507},
{'P', 1929},
{'Q', 95},
{'R', 5987},
{'S', 6327},
{'T', 9056},
{'U', 2758},
{'V', 978},
{'W', 2360},
{'X', 150},
{'Y', 1974},
{'Z', 74}
};
// 统计一个字符串中每个字母出现的频率
map<char, int> count_letters(string s) {
map<char, int> freq;
for (char c : s) {
if (isalpha(c)) {
freq[toupper(c)]++;
}
}
return freq;
}
// 计算一个字符串的得分,用于评估其是否是明文
int score_string(string s) {
int score = 0;
map<char, int> freq = count_letters(s);
for (auto p : freq) {
char c = p.first;
int count = p.second;
if (freq_table.count(c)) {
int expected_count = freq_table[c] * s.size() / 100;
score += abs(count - expected_count);
}
}
return score;
}
// 对一个字符串进行凯撒解密
string caesar_decrypt(string s, int key) {
string plaintext = "";
for (char c : s) {
if (isalpha(c)) {
char base = isupper(c) ? 'A' : 'a';
char decrypted = base + ((c - base - key + 26) % 26);
plaintext += decrypted;
} else {
plaintext += c;
}
}
return plaintext;
}
int main() {
// 读取密文文件
ifstream fin("ciphertext.txt");
string ciphertext;
getline(fin, ciphertext);
// 枚举密钥,对密文进行解密,并评分
int best_score = INT_MAX;
string best_plaintext;
for (int key = 0; key < 26; key++) {
string plaintext = caesar_decrypt(ciphertext, key);
int score = score_string(plaintext);
if (score < best_score) {
best_score = score;
best_plaintext = plaintext;
}
}
// 输出破解后的明文
cout << best_plaintext << endl;
return 0;
}
```
该程序读取一个名为"ciphertext.txt"的文件,该文件包含要破解的密文。程序首先枚举密钥,对密文进行解密,并评分。评分使用了词频分析,计算解密后的明文与英文文本的相似度。程序输出得分最低的解密后的明文。
使用c++编程 任务描述 本关任务:编写一个程序,实现对凯撒加密算法的破解,输入恺撒加密的密文,输出破解后的明文(也是大写字母构成) 例如:明文是"Attack in the morning",密钥k等于3,则密文是"DWWDFN LQ WKH
好的,这里是一个 C++ 的实现:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string ciphertext; // 定义密文字符串
int k; // 定义密钥
getline(cin, ciphertext); // 获取密文
cin >> k; // 获取密钥
string plaintext; // 定义明文字符串
for (char c : ciphertext) {
if (isalpha(c)) { // 判断是否为字母
// 将字母向前移动 k 位,注意回卷到字母表开头
plaintext += static_cast<char>((c - 'A' - k + 26) % 26 + 'A');
} else {
plaintext += c; // 非字母直接拼接到明文字符串中
}
}
cout << plaintext << endl; // 输出解密后的明文
return 0;
}
```
这个程序和前面的 Python 版本类似,首先获取输入的密文和密钥,然后遍历密文中的每个字符。如果是字母,就将其向前移动 k 位,注意回卷到字母表开头。最后将解密后的明文字符串输出即可。需要注意的是,C++ 中字符类型的加减运算会自动将字符转换为其 ASCII 码,因此需要使用显式类型转换将结果转换回字符类型。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)