C++ 文本加密解密程序代码详解:小白入门指南
这段代码是一个简单的文本加密和解密程序。它的主要功能是读取一个输入文件 ('in.txt'),对其中的每一行进行加密处理,然后将结果写入一个输出文件 ('output.txt') 中。
代码中包含了一些 C++ 标准库的头文件,用于引入一些必要的函数和数据结构:
- #include
: 用于输入输出操作,例如 cin和cout - #include
: 用于文件操作,例如 ifstream和ofstream - #include
: 用于标准输入输出库,例如 printf和scanf - #include
: 用于字符串操作,例如 strcpy - #include
: 用于字符串类,例如 string - #include
: 用于集合容器,例如 set - #include : 用于映射容器,例如
map - #include
: 用于标准库函数,例如 rand - #include
: 用于动态数组,例如 vector - #include
: 用于算法操作,例如 sort
接下来,代码定义了一些辅助函数和变量:
- typedef pair<char, int> PAIR: 定义一个名为 PAIR 的类型别名,表示一个字符和其出现次数的键值对
- bool cmp_by_value(const PAIR& lhs, const PAIR& rhs): 定义一个比较函数,用于按值降序排序 PAIR 类型
- struct CmpByValue: 定义一个结构体,用于比较函数对象,实现按值降序排序 PAIR 类型
- int s[14]: 定义一个数组,用于存放一些预定义的数值
- int search(int s[], int y, int x): 定义一个搜索函数,用于在数组 s 中查找元素 x 的位置
在主函数 main() 中,代码首先通过 ifstream 打开输入文件 'in.txt',然后使用 while 循环遍历文件的每一行。对于每一行,代码进行以下操作:
- 创建 map 对象: 代码创建了一个
map对象m,用于统计每个字符出现的次数。 - 统计字符频率: 代码使用一个
range-based for循环,遍历当前行的每个字符,并更新m中对应字符的出现次数。 - 输出字符频率: 代码使用另一个
range-based for循环,遍历m中的每个键值对,输出每个字符和对应的出现次数。 - 将 map 转换为 vector: 代码将
m转换为一个vector对象m_vec,并按照字符出现次数的降序进行排序。 - 计算 x 和 y: 根据题目要求,代码根据排序后的结果,找到两个字符
c和f,并根据一定的规则计算出x和y的值。具体计算方法可以参考代码注释中的解释。 - 搜索 x: 代码使用自定义的
search函数,在数组s中查找x的位置。如果找到了,输出找到的位置和对应的字母。 - 计算 a 的逆: 代码根据找到的位置,计算出
a的逆ani。 - 解密操作: 代码对输入的每个字符进行解密操作,将解密后的结果写入输出文件。
最后,代码关闭输入和输出文件,输出程序运行时间。
总之,该代码是一个简单的文本加密解密程序,通过一些简单的数学运算和字符串处理,对输入文件进行加密处理,然后将结果写入输出文件。
代码注释:
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<char, int> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
return lhs.second > rhs.second;
}
struct CmpByValue {
bool operator()(const PAIR& lhs, const PAIR& rhs) {
return lhs.second > rhs.second;
}
};
int s[14] = { 1,1,3,9,5,21,7,15,11,19,17,23,25,25 };
int search(int s[], int y, int x) /*被调用的函数search()*/
{
int i, f = -1;
for (i = 0; i < 14; i++)
{
if (x == s[i])//查找到所需的元素,跳出循环
{
f = i + 1;
break;
}
}
return(f);
}
int main() {
clock_t start,end;
start = clock();
ifstream in('in.txt');
ofstream out('output.txt');
string filename;
string line;
if (in) // 有该文件
{
while (getline(in, line)) // line中不包括每行的换行符
{
map<char, int> m;
for (auto i : line)
{
m[i]++;
}
for (auto& i : m)
{
cout << i.first << ' ' << i.second << endl;
}
vector<PAIR> m_vec(m.begin(), m.end());
sort(m_vec.begin(), m_vec.end(), CmpByValue());
int j;
j = m_vec.size();
char* p_str = new char[j];
for (int i = 0; i != m_vec.size(); ++i)
{
cout << m_vec[i].first << ' ' << m_vec[i].second << endl;
}
for (int i = 0; i != m_vec.size(); ++i)
{
p_str[i] = m_vec[i].first;
//cout << ' p_str ' << p_str[i] << endl;
}
int a, b, c, d, e, f;
a = 4;
b = 1;
c = m_vec[0].first - 65;
cout << p_str[0] << endl;
d = 19;
e = 1;
//f = m_vec[1].first - 65;
//方程不变量赋值
int x = 0;
int y = 0;
int pos=-1;
for (int n = 1; n < j; n++) {
cout << p_str[n] << endl;
f = m_vec[n].first - 65;
cout << 'm_vec[n].firs ' << m_vec[n].first<< f << endl;
//方程找t的匹配字符从第二频次开始 n=1开始 ,m_vec[1].first
cout << '第 n: ' << n << ' 频次 ' << endl;
//解x和y
int p = 1;
do {
x = (f - c + 26 * p);
p++;
} while (x % 15 != 0);
x = x / 15;
cout << ' x: ' << x << endl;
p = 0;
do {
//cout << '求y的 p: ' << p << endl;
y = c - 4 * x + 26 * p;
p++;
} while (y < 1);
cout << a << ' | ' << b << ' | ' << c << ' | ' << d << ' | ' << e << ' | ' << f << endl;
cout << ' x: ' << x << endl;
cout << ' y: ' << y << endl;
pos = search(s, 10, x); /*调用函数search()完成顺序查找*/
cout << 'pos数' << pos << endl;
if (pos < 0) /*输出要找的数的位置*/
{
cout << '没找到要找的数' << endl;
}
else
{
cout << '您要找的数: ' << x << ' 在数组中第 个元素处 ' << pos << endl;
cout << '找到要的字母' << m_vec[n].first << endl;
break;
}
}
int ani = 0;
if (pos % 2 == 0)
{
ani = s[pos - 2];
}
else { ani = s[pos - 1]; }
cout << '您要找的a逆: ' << ani << endl;
cout << '解密函数x=' << ani << '(y- ' << y << ')mod26 ' << endl;
int sl;
sl = line.length();
char ch[500];
char re[500];
strcpy_s(ch, line.c_str());
for(int p=0;p<sl;p++)
{
re[p] =( ani * ((ch[p] - 65) - y)) % 26 + 97;
if ((ani * ((ch[p] - 65) - y)) < 0) {
re[p] = re[p] + 26;
}
cout << re[p];
out << re[p]; // 输入到2.txt中
}
}
}
else // 没有该文件
{
cout << 'no such file' << endl;
}
in.close();
out.close();
system('pause');
end = clock();
cout << end - start << 'ms' << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/154 著作权归作者所有。请勿转载和采集!