forked from abhishekdoifode1/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Affine_Cipher.cpp
39 lines (39 loc) · 929 Bytes
/
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
#include<iostream>
#include<string>
using namespace std;
string encrypt(string m,int k1,int k2) {
for (int i = 0; i < m.length(); i++) {
if(m[i]!=' ')
m[i] =(char)(((k1 * (m[i]-'A') + k2) % 26) + 'A');
}
return m;
}
string decrypt(string c,int k1,int k2) {
int inverse = 0;
int flag = 0;
for (int i = 0; i < 26; i++) {
flag = (k1 * i) % 26;
if (flag == 1)
inverse = i;
}
for (int i = 0; i < c.length(); i++) {
if(c[i] != ' ')
c[i] =(char)((inverse * (c[i]-'A'- k2) % 26) + 'A');
}
return c;
}
int main(void) {
string text;
int k1,k2;
cout<<"-----AFFINE CIPHER-----";
cout<<"\nEnter string to encrypt:";
cin>>text;
//k1:first key should have have multiplicative inverse.
cout<<"Enter keys:";
cin>>k1>>k2;
string encrypted_text=encrypt(text,k1,k2);
cout<<"Encrypted Text:"<<encrypted_text;
cout<<"\nKeys:"<<k1<<' '<<k2;
cout<<"\nDecrypted Text:"<<decrypt(encrypted_text,k1,k2);
return 0;
}