forked from ElectronicsClub-IITK/ENIGMA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek1_ENIGMA.cpp
More file actions
47 lines (46 loc) · 1012 Bytes
/
Copy pathweek1_ENIGMA.cpp
File metadata and controls
47 lines (46 loc) · 1012 Bytes
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
#include <iostream>
using namespace std;
int inverse_a(int a){
for (int i=1;i<128;i++){
if ((a*i)%128==1){
return i;
}
}
return -1;
}
string affine_decode(string s, int a, int b){
string result = "";
for (char c:s){
result+=(char)(((a*(c-b))%128+128)%128);
}
return result;
}
string affine_encode(string s, int a, int b){
string result = "";
for (char c:s){
result+=(char)(((a*c+b)%128+128)%128);
}
return result;
}
int main(){
int a, b;
cin>>a>>b;
if (inverse_a(a)==-1){
cout<<"a must be co-prime to 128"<<endl;
return 0;
}
a=a%128;
b=b%128;
string s;
int choice;
cin>>choice;
cin.ignore();
getline(cin, s);
if (choice==1){
cout<<affine_encode(s, a, b)<<endl;
} else if (choice==2){
cout<<affine_decode(s, inverse_a(a), b)<<endl;
} else {
cout<<"Invalid choice"<<endl;
}
}