-
Notifications
You must be signed in to change notification settings - Fork 1
/
hill_cipher.c
371 lines (310 loc) · 9.56 KB
/
hill_cipher.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include<stdio.h>
#include<math.h>
#include<string.h>
char plain[20],plain2[20],encrypted[20],x[1],y[1],z[1];
char characters[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'}, ciphertext[3], plaintext[3];
int i,j,key2[3][3],k,w,key[2][2],key3[2][2];
int number[3], cipher[3];
int choice,choice2;
int determinant;
char temp;
void encryption2x2(char ch1[], char ch2[])
{
char plain[3]= {};
plain[2] = '\0';
strcat(plain,ch1);
strcat(plain,ch2);
printf("\n-----Two-pair character text is: ");
puts(plain);
number[0]=0;
number[1]=0;
cipher[0]=0;
cipher[1]=0;
ciphertext[0] = 0;
ciphertext[1] = 0;
for(i=0; i<=1; i++)
{
for(j=0; j<=25; j++)
{
if( plain[i] == characters[j])
{
number[i] = j;
}
}
}
printf("\nAlphabetical ordered number text will be-\n");
for(k=0; k <= 1; k++)
printf("%d ", number[k]);
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
cipher[i] = cipher[i] + ( key[i][j] * number[j] );
}
cipher[i] = cipher[i]%26;
for(k=0; k<=25; k++)
{
if(cipher[i] == k)
ciphertext[i] = characters[k];
}
}
printf("\nGenerated Cipher text in alphabetical ordered number text will be-\n");
for(i=0; i<=1; i++)
printf("%d ", cipher[i]);
printf("\n-----Cipher text of the pair will be: ");
puts(ciphertext);
}
void key_inverse(int key3[2][2])
{
printf("\nDeterminant of entered key matrix is: ");
determinant = ( key3[1][1]*key3[0][0] ) - ( key3[0][1]*key3[1][0] ) ;
determinant = determinant%26;
printf("%d", determinant);
printf("\n\nAdjoint of the entered key matrix will be- \n");
key3[0][1] = -key3[0][1] + 26;
key3[1][0] = -key3[1][0] + 26;
temp = key3[0][0];
key3[0][0] = key3[1][1];
key3[1][1] = temp;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d ",key3[i][j]);
}
printf("\n");
}
int d;
int x=0;
for(x=1; x<25; x++){
if( (x*determinant)%26 == 1)
d = x;
}
printf("\n D-inverse will be: %d", d);
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
key3[i][j] = (key3[i][j] * d)%26;
}
}
printf("\nInverse of Key matrix will be- \n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d ",key3[i][j]);
}
printf("\n");
}
}
void decryption2x2(char ch1[], char ch2[])
{
char encrypt[3]= {};
encrypt[2] = '\0';
strcat(encrypt,ch1);
strcat(encrypt,ch2);
printf("\n-----Two-pair encrypted character text is: ");
puts(encrypt);
number[0]=0;
number[1]=0;
cipher[0]=0;
cipher[1]=0;
ciphertext[0] = 0;
ciphertext[1] = 0;
for(i=0; i<=1; i++)
{
for(j=0; j<=25; j++)
{
if( encrypt[i] == characters[j])
{
number[i] = j;
}
}
}
printf("\nAlphabetical ordered number text will be-\n");
for(k=0; k <= 1; k++)
printf("%d ", number[k]);
for(i=0; i<=1; i++)
{
for(j=0; j<=1; j++)
{
cipher[i] = cipher[i] + ( key3[i][j] * number[j] );
}
cipher[i] = cipher[i]%26;
for(k=0; k<=25; k++)
{
if(cipher[i] == k)
plaintext[i] = characters[k];
}
}
printf("\nGenerated Plain text in alphabetical ordered number text will be-\n");
for(i=0; i<=1; i++)
printf("%d ", cipher[i]);
printf("\n-----Plain text of the pair will be: ");
puts(plaintext);
}
void encryption3x3(char ch1[], char ch2[], char ch3[])
{
char plain[4]= {};
plain[3] = '\0';
strcat(plain,ch1);
strcat(plain,ch2);
strcat(plain, ch3);
printf("\n-----Three-pair character text is:-----\n");
puts(plain);
number[0]=0;
number[1]=0;
number[2]=0;
cipher[0]=0;
cipher[1]=0;
cipher[2]=0;
ciphertext[0] = 0;
ciphertext[1] = 0;
ciphertext[2] = 0;
for(i=0; i<=2; i++)
{
for(j=0; j<=25; j++)
{
if( plain[i] == characters[j])
{
number[i] = j;
}
}
}
printf("\nAlphabetical ordered number text will be-\n");
for(k=0; k <= 2; k++)
printf("%d ", number[k]);
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
cipher[i] = cipher[i] + ( key2[i][j] * number[j] );
}
cipher[i] = cipher[i]%26;
for(k=0; k<=25; k++)
{
if(cipher[i] == k)
ciphertext[i] = characters[k];
}
}
printf("\nGenerated Cipher text in alphabetical ordered number text will be-\n");
for(i=0; i<=2; i++)
printf("%d ", cipher[i]);
printf("\nCipher text of the pair will be: ");
puts(ciphertext);
}
void main()
{
printf("Hill Cipher");
do{
printf("\nMAIN MENU");
printf("\n1. 2x2 encryption/decryption \t2. 3x3 encryption \t3. EXIT");
printf("\nEnter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: do{
printf("\n--------2x2 Hill Cipher-----------");
printf("\n1. Encryption\t2. Decryption\t3. EXIT");
printf("\nEnter your choice: ");
scanf("%d", &choice2);
switch(choice2)
{
case 1: printf("\nEnter the plain text: ");
scanf("%s", &plain);
printf("\nEnter the 2*2 Key Matrix-");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&key[i][j]);
}
}
printf("\nKey is:\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d ",key[i][j]);
}
printf("\n");
}
printf("\n-------------------Encrypting each two-character pair of plain text-----------------------\n\n");
for(w=0; plain[w] != '\0'; ){
x[0] = plain[w];
x[1] = '\0';
y[0] = plain[w+1];
y[1] = '\0';
encryption2x2(x, y);
w = w + 2;
}
break;
case 2: printf("\nEnter the cipher text: ");
scanf("%s", &encrypted);
printf("\nEnter the 2*2 Key Matrix-");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&key3[i][j]);
}
}
printf("\nEntered Key is:\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d ",key3[i][j]);
}
printf("\n");
}
key_inverse(key3);
printf("\n-------------------Decrypting each two-character pair of plain text-----------------------\n\n");
for(w=0; encrypted[w] != '\0'; ){
x[0] = encrypted[w];
x[1] = '\0';
y[0] = encrypted[w+1];
y[1] = '\0';
decryption2x2(x, y);
w = w + 2;
}
break;
}
}while( choice2 != 3);
break;
case 2: printf("\n--------3x3 Hill Cipher-----------");
printf("\nEnter the plain text: ");
scanf("%s", &plain2);
printf("\nEnter the 3*3 Key Matrix-");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&key2[i][j]);
}
}
printf("\nKey is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",key2[i][j]);
}
printf("\n");
}
printf("\n-------------------Encrypting each three-character pair of plain text-----------------------\n\n");
for(w=0; plain2[w] != '\0'; ){
x[0] = plain2[w];
x[1] = '\0';
y[0] = plain2[w+1];
y[1] = '\0';
z[0] = plain2[w+2];
z[1] = '\0';
encryption3x3(x, y, z);
w = w + 3;
}
break;
}
}while(choice != 3);
}