根据公式c = Ea, b(m) ☰ a * m + b (mod 26);
如果已知a, b, 加密非常简单,代码如下:
#include <bits/stdc++.h>
using namespace std;
int a, b;
void Input()
{
int p, val;
char key;
char key_2[1010];
cout << "请输入a, b的值(中间以空格分开)" << endl;
scanf("%d %d", &a, &b);
cout << "一次输入一个字符请输入 1 否则请输入 2" << endl;
scanf("%d", &p);
if(p == 1)
{
getchar();
while(scanf("%c", &key) != EOF)
{
if(key != '\n')
{
val = ((key - 'A') * a + b) % 26;
key = 'A' + val;
cout << "val = " << val << " key = " << key << endl;
}
}
}
else
{
getchar();
gets(key_2);
int len = strlen(key_2);
for(int i = 0; i < len; ++i)
{
if(key_2[i] >= 'A' && key_2[i] <= 'Z')
{
val = ((key_2[i] - 'A') * a + b) % 26;
key_2[i] = val + 'A';
}
}
cout << key_2 << endl;
}
}
int main()
{
Input();
}