-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaffine_decrypt.c
More file actions
71 lines (50 loc) · 1.28 KB
/
affine_decrypt.c
File metadata and controls
71 lines (50 loc) · 1.28 KB
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
// Author - Michael Ibeh
// License - Apache Version 2.0
#include <stdio.h>
#include <string.h>
#include "CipherSolve.h"
int mod_inverse(int a, int m){
int i;
a = a % m;
for(i = 1; i < m; i++)
if( (a * i) % m == 1)
return i;
}
void affine_decrypt(void){
int i, j, k, temp, len, a, b, a_inverse;
int avals[] = {3,5,7,9,11,15,17,19,21,23,25};
char buffer[1024];
char plaintext[1024];
char ciphertext[1024];
printf("What is the ciphertext you want to decrypt?\n");
scanf("%s", buffer);
strcpy(ciphertext, buffer);
len = strlen(ciphertext);
printf("Possible Decryptions: \n");
// Loops for each of the possible values coprime with 26
for(i = 0; i < 11; i++){
for(j = 0; j < 26; j++){
a = avals[i];
a_inverse = mod_inverse(a, 26);
b = j;
for(k = 0; k < len; k++){
// Create a new string for manipulation
plaintext[k] = ciphertext[k];
// Convert ASCII values to 0-25
plaintext[k] -= 'a';
// Perform encryption algorithm in reverse
temp = (int)plaintext[k];
temp -= b;
temp *= a_inverse;
while(temp < 0){
temp += 26;
}
temp = temp % 26;
plaintext[k] = 'a' + temp;
}
// Clean up output
plaintext[k] = '\0';
printf("Keys a = %d b = %d \tMessage: %s\n", a, b, plaintext);
}
}
}