forked from rakshitsharmaa/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffin.cpp
55 lines (53 loc) · 1.33 KB
/
affin.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
#include<bits/stdc++.h>
using namespace std;
int modInverse(int k){
for(int i=0; i<26; i++){
int flag= (i*k)%26;
if(flag==1){
return i;
}
}
return -1;
}
string encrypt(string plain_text, int key1, int key2){
string cipher_text= "";
for(int i=0; i<plain_text.size(); i++){
if(isupper(plain_text[i])){
cipher_text+= char((key1* int(plain_text[i]-65)+ key2)%26 +65);
}else{
cipher_text+= char((key1* int(plain_text[i]-97)+ key2)%26 +97);
}
}
return cipher_text;
}
string decrypt(string cipher_text, int key1, int key2){
string plain_text= "";
int key_inverse= modInverse(key1);
if(key_inverse==-1){
return "Not exist";
}
cout<<"Key Inverse: "<<key_inverse<<endl;
for(int i=0; i<cipher_text.size(); i++){
if(isupper(cipher_text[i])){
plain_text+= char(((((cipher_text[i]-65- key2)%26+26)%26)*key_inverse)%26+65);
}else{
plain_text+= char(((((cipher_text[i]-97- key2)%26+26)%26)*key_inverse)%26+97);
}
}
return plain_text;
}
int main(){
string plain_text;
cout<<"Enter plain text: ";
cin>>plain_text;
int key1, key2;
cout<<"Enter first key: ";
cin>>key1;
cout<<"Enter second key: ";
cin>>key2;
cout<<"\nEncryption:\n";
string cipher_text= encrypt(plain_text, key1, key2);
cout<<"Cipher: "<<cipher_text<<endl;
cout<<"\nDecryption:\n";
cout<<"Plain Text: "<<decrypt(cipher_text, key1, key2)<<endl;
}