-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffine_cipher.cpp
59 lines (55 loc) · 1.48 KB
/
affine_cipher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*******************************************
* Name: affine_cipher.cpp
* Author: kongtaoxing
* Powered by Beijingjiaotong University
******************************************/
#include<iostream>
#include<string>
using namespace std;
int chengfaniyuan(int num){
for( int i = 0; i < 26; i++){
if( num * i % 26 == 1){
return i;
}
}
}
string encrypt(string s, int a, int b) {
for (int i = 0; i < s.length(); i++) {
s[i] = (a * s[i] - 65 + b) % 26;
if (s[i] < 0) s[i] += 26; //C语言-2%26竟然等于-2,不够26需要手动加一下
s[i] += 65;
}
return s;
}
string decrypt(string s, int a, int b) {
for (int i = 0; i < s.length(); i++) {
s[i] = (chengfaniyuan(a) * (s[i] - 65 - b)) % 26;
if (s[i] < 0) s[i] += 26;
s[i] += 65;
}
return s;
}
int main() {
cout << "你想加密还是解密,加密输入e,解密输入d:";
char c;
cin >> c;
cout << "\n请输入需要加/解密的字母串:";
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] >= 97 && s[i] <= 122) {
s[i] -= 32;
}
}
cout << "\n请输入a:";
int a;
cin >> a;
cout << "\n请输入b:";
int b;
cin >> b;
if (c == 'e') cout << "\n加密后的字符串为:" << encrypt(s, a, b) << endl;
else if (c == 'd') cout << "\n解密后的字符串为:" << decrypt(s, a, b) << endl;
else cout << "你的输入有误,请重新输入!" << endl;
system("pause");
return 0;
}