C++ 解密函数实现实验报告
C++ 解密函数实现实验报告
实验目的
学习使用 C++ 语言实现解密函数,并对给定的字符串进行解密。
实验原理
- 首先读取输入文件中的每一行字符串。
- 对于每一行字符串,使用
map容器统计每个字符出现的次数。 - 将
map容器中的键值对转换成vector,并根据值的大小进行排序。 - 根据排序后的
vector,确定解密函数的参数 'a' 和 'c'。 - 根据解密函数的参数 'a' 和 'c',解出参数 'x' 和 'y'。
- 使用解密函数对每个字符进行解密,并将结果写入输出文件。
实验步骤
- 打开输入文件和输出文件。
- 读取输入文件的每一行字符串。
- 使用
map容器统计每个字符出现的次数。 - 将
map容器中的键值对转换成vector,并根据值的大小进行排序。 - 根据排序后的
vector,确定解密函数的参数 'a' 和 'c'。 - 根据解密函数的参数 'a' 和 'c',解出参数 'x' 和 'y'。
- 使用解密函数对每个字符进行解密,并将结果写入输出文件。
- 关闭输入文件和输出文件。
实验代码
#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() {
ifstream in('D:/test/1/in.txt');
ofstream out('D:/test/1/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');
return 0;
}
实验结果
根据给定的输入文件中的字符串,使用解密函数成功解密出原始字符串,并将解密后的字符串写入输出文件。
实验结论
通过对解密函数进行实现和应用,可以对给定的字符串进行解密,并得到原始字符串。这为我们理解和应用解密函数提供了实践基础。
原文地址: https://www.cveoy.top/t/topic/3qa 著作权归作者所有。请勿转载和采集!