-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathAffine_substitution_Cipher.cpp
160 lines (121 loc) · 3.03 KB
/
Affine_substitution_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*The Affine Cipher is a type of monoalphabetic substitution cipher.
Here each letter in a message is first converted to a number and then
encrypted using a simple mathematical function. Then the numbers are
converted back to ASCII.*/
#include <bits/stdc++.h>
using namespace std;
// Key values declared as global variables
int a;
int b;
int m = 26;
// This is the encryption function
string encryption(string message) {
string cipher;
/* Encrypted Message(x) = ( a * x + b ) mod m
modulus m: size of the alphabet, here 26 a and
b: key of the cipher. a and m should be coprime.*/
for (int i = 0; i < message.size(); ++i) {
// Spaces should not be encrypted
if (message[i] != ' ') {
// to make in range of ASCII "A" is added
cipher += (char)((a * (message[i] - 'A') + b) % 26 + 'A');
}
else {
cipher += message[i];
}
}
// Return the ciphertext
return cipher;
}
// This is the decryption function
string decryption(string cipher) {
string message;
int a_inv = 0;
for (int i = 0; i < 26; ++i) {
if ((a * i) % 26 == 1) {
a_inv = i;
break;
}
}
/* Decrypted Message(x) = a_inv ( x - b ) mod m
a_inv : modular multiplicative inverse of a modulo m
1 = (a * a_inv) mod m .*/
for (int i = 0; i < cipher.size(); ++i) {
// Spaces should not be encrpyted
if (cipher[i] != ' ') {
// to make it in range of ASCII "A" is added
message += (char)(a_inv * (cipher[i] + 'A' - b) % 26 + 'A');
}
else {
message += cipher[i];
}
}
// Return the underlying plaintext
return message;
}
int main() {
int choice;
cout << "----------Affine Cipher----------\n\n";
cout << "1. Encryption" << endl;
cout << "2. Decryption" << endl;
cout << "3. Exit" << endl;
cout << "\nEnter your choice: ";
cin >> choice;
// Encryption
if(choice == 1) {
cout << "\nEnter the key 'a': ";
cin >> a;
cout << "Enter the key 'b': ";
cin >> b;
string message;
cout << "Enter a message: ";
cin >> message;
transform(message.begin(), message.end(), message.begin(), ::toupper);
cout << "\nEncrypted message is: " << encryption(message) << endl;
}
// Decryption
else if(choice == 2) {
cout << "\nEnter the key 'a': ";
cin >> a;
cout << "Enter the key 'b': ";
cin >> b;
string ciphertext;
cout << "\nEnter a ciphertext: ";
cin >> ciphertext;
transform(ciphertext.begin(), ciphertext.end(), ciphertext.begin(), ::toupper);
cout << "\nDecrypted message is: " << decryption(ciphertext) << endl;
}
else if(choice == 3) {
cout << "\nExiting program." << endl;
exit(0);
}
else {
cout << "\nInvalid choice." << endl;
}
return 0;
}
/*
Sample I/O:
1)
----------Affine Cipher----------
1. Encryption
2. Decryption
3. Exit
Enter your choice: 1
Enter the key 'a': 17
Enter the key 'b': 21
Enter a message: NEOALGO
Encrypted message is: ILZVATZ
2)
----------Affine Cipher----------
1. Encryption
2. Decryption
3. Exit
Enter your choice: 2
Enter the key 'a': 17
Enter the key 'b': 21
Enter a ciphertext: ILZVATZ
Decrypted message is: NEOALGO
Time complexity - O(n)
Space complexity - O(1)
*/