-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path10_Hill_cipher.cpp
260 lines (220 loc) · 6.53 KB
/
10_Hill_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <cmath>
#include <cstring>
// #include "./returnName.h"
using namespace std;
void getKeyMatrix(const char *, int[][4], int);
void printKeyMatrix(int[][4], int);
int modInverse(int, int);
int determinant(int[][4]);
void adjugate(int[][4], int[][4]);
void encrypt(const char *, int[][4], int, char *);
void decrypt(const char *, int[][4], int, char *);
int main()
{
// generateHeader("Program to demonstrate Hill Cipher");
while (true)
{
cout << "Menu:" << endl;
cout << "1. Encrypt Text" << endl;
cout << "2. Decrypt Text" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
if (choice == 1)
{
char message[100], key[16];
cout << "Enter the message (uppercase letters): ";
cin >> message;
cout << "Enter the key matrix (e.g., for a 2x2 matrix, enter 4 uppercase letters): ";
cin >> key;
int n = sqrt(strlen(key));
if (n * n != strlen(key))
{
cout << "Invalid key matrix size. Please enter a square key matrix." << endl;
continue;
}
int keyMatrix[4][4];
getKeyMatrix(key, keyMatrix, n);
cout << "Key Matrix:" << endl;
printKeyMatrix(keyMatrix, n);
char ciphertext[100];
encrypt(message, keyMatrix, n, ciphertext);
cout << "Encrypted ciphertext: " << ciphertext << endl;
}
else if (choice == 2)
{
char ciphertext[100], key[16];
cout << "Enter the ciphertext (uppercase letters): ";
cin >> ciphertext;
cout << "Enter the key matrix (e.g., for a 2x2 matrix, enter 4 uppercase letters): ";
cin >> key;
int n = sqrt(strlen(key));
if (n * n != strlen(key))
{
cout << "Invalid key matrix size. Please enter a square key matrix." << endl;
continue;
}
int keyMatrix[4][4];
getKeyMatrix(key, keyMatrix, n);
cout << "Key Matrix:" << endl;
printKeyMatrix(keyMatrix, n);
char plaintext[100];
decrypt(ciphertext, keyMatrix, n, plaintext);
cout << "Decrypted plaintext: " << plaintext << endl;
}
else if (choice == 3)
break;
else
cout << "Invalid choice. Please enter a valid option." << endl;
}
cin.get();
return 0;
}
// Function to get the key matrix from the key string
void getKeyMatrix(const char *key, int keyMatrix[][4], int n)
{
int k = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
keyMatrix[i][j] = (key[k++] - 'A') % 26;
}
}
}
// Function to print the key matrix
void printKeyMatrix(int keyMatrix[][4], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << keyMatrix[i][j] << " ";
}
cout << endl;
}
}
// Function to calculate the modular inverse of a number 'a' modulo 'm'
int modInverse(int a, int m)
{
a = a % m;
for (int x = 1; x < m; x++)
{
if ((a * x) % m == 1)
{
return x;
}
}
return -1; // Modular inverse does not exist
}
// Function to compute the determinant of a square matrix
int determinant(int matrix[][4])
{
int det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
return det;
}
// Function to find the adjugate of a matrix
void adjugate(int matrix[][4], int adjMatrix[][4])
{
adjMatrix[0][0] = matrix[1][1];
adjMatrix[0][1] = -matrix[0][1];
adjMatrix[1][0] = -matrix[1][0];
adjMatrix[1][1] = matrix[0][0];
}
// Function to encrypt the message
void encrypt(const char *message, int keyMatrix[][4], int n, char *ciphertext)
{
int m = 26; // Modulo 26 for English alphabets
int len = strlen(message);
int k = 0;
for (int i = 0; i < len; i += n)
{
int messageVector[4] = {0};
int cipherVector[4] = {0};
// Generate vector for the message
for (int j = 0; j < n; j++)
{
if (i + j < len)
{
messageVector[j] = (message[i + j] - 'A') % m;
}
}
// Perform Hill Cipher encryption
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
cipherVector[j] += keyMatrix[j][k] * messageVector[k];
}
cipherVector[j] %= m;
}
// Generate the encrypted text
for (int j = 0; j < n; j++)
{
if (i + j < len)
{
ciphertext[k++] = static_cast<char>(cipherVector[j] + 'A');
}
}
}
ciphertext[k] = '\0';
}
// Function to decrypt the message
void decrypt(const char *ciphertext, int keyMatrix[][4], int n, char *plaintext)
{
int m = 26; // Modulo 26 for English alphabets
int len = strlen(ciphertext);
int adjMatrix[4][4], detInverse;
adjugate(keyMatrix, adjMatrix);
int det = determinant(keyMatrix);
detInverse = modInverse(det, m);
if (detInverse == -1)
{
cout << "Modular inverse of the determinant does not exist. Decryption is not possible." << endl;
return;
}
int keyInverse[4][4] = {0};
// Calculate the key inverse
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
keyInverse[i][j] = (adjMatrix[i][j] * detInverse) % m;
keyInverse[i][j] = (keyInverse[i][j] + m) % m; // Ensure non-negative values
}
}
int k = 0;
for (int i = 0; i < len; i += n)
{
int cipherVector[4] = {0};
int messageVector[4] = {0};
// Generate vector for the ciphertext
for (int j = 0; j < n; j++)
{
if (i + j < len)
{
cipherVector[j] = (ciphertext[i + j] - 'A') % m;
}
}
// Perform Hill Cipher decryption
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
messageVector[j] += keyInverse[j][k] * cipherVector[k];
}
messageVector[j] %= m;
}
// Generate the decrypted text
for (int j = 0; j < n; j++)
{
if (i + j < len)
{
plaintext[k++] = static_cast<char>(messageVector[j] + 'A');
}
}
}
plaintext[k] = '\0';
}