-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enigma.c
142 lines (129 loc) · 14 KB
/
Enigma.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MESSAGE_LENGTH 4
#define MAX_POSITION_LENGTH 3
#define RADIUS 256
#define BASE 256
// Assume rotor_inverser is a 2D array defined elsewhere
int rotor_inverser[8][256] = {
{106, 33, 209, 10, 109, 78, 45, 233, 203, 214, 37, 93, 114, 165, 197, 127, 14, 182, 58, 128, 194, 15, 241, 245, 162, 153, 96, 220, 140, 250, 227, 20, 208, 92, 21, 3, 196, 207, 57, 121, 50, 115, 221, 25, 5, 74, 102, 36, 63, 252, 190, 138, 249, 61, 9, 224, 120, 89, 231, 80, 236, 19, 51, 24, 167, 133, 87, 201, 23, 122, 123, 141, 13, 191, 31, 81, 85, 2, 200, 60, 8, 177, 222, 189, 82, 212, 132, 28, 247, 4, 79, 75, 6, 172, 170, 204, 49, 218, 136, 232, 94, 105, 217, 234, 35, 108, 169, 119, 195, 176, 38, 150, 147, 161, 103, 107, 206, 145, 193, 244, 237, 97, 157, 56, 41, 240, 173, 84, 90, 22, 112, 148, 86, 210, 76, 159, 27, 238, 198, 211, 67, 151, 239, 69, 53, 73, 251, 113, 253, 116, 71, 255, 48, 42, 104, 110, 168, 11, 126, 205, 39, 47, 68, 235, 17, 88, 188, 149, 55, 142, 44, 43, 52, 1, 202, 54, 184, 139, 146, 215, 246, 242, 137, 154, 65, 171, 64, 111, 40, 228, 166, 12, 254, 34, 185, 223, 129, 29, 230, 101, 16, 164, 18, 95, 248, 152, 46, 229, 158, 243, 66, 0, 175, 98, 7, 183, 100, 91, 181, 117, 187, 130, 144, 163, 180, 192, 186, 213, 62, 155, 156, 135, 26, 219, 125, 72, 131, 179, 124, 118, 178, 143, 59, 134, 199, 160, 225, 99, 77, 83, 32, 30, 226, 70, 174, 216},
{130, 110, 242, 232, 192, 222, 171, 75, 147, 120, 85, 159, 58, 132, 190, 24, 237, 200, 59, 100, 20, 52, 210, 19, 167, 223, 253, 74, 173, 55, 250, 234, 219, 102, 46, 207, 1, 128, 28, 64, 107, 2, 195, 104, 45, 186, 114, 98, 141, 240, 241, 153, 208, 23, 49, 166, 43, 151, 17, 198, 65, 188, 42, 187, 106, 10, 148, 230, 176, 80, 0, 47, 30, 181, 84, 149, 131, 40, 66, 118, 11, 37, 179, 228, 254, 26, 112, 4, 160, 14, 122, 137, 238, 236, 36, 116, 7, 95, 134, 87, 174, 97, 152, 76, 233, 229, 57, 78, 53, 108, 35, 88, 204, 252, 143, 251, 209, 191, 156, 154, 33, 201, 117, 34, 123, 150, 119, 39, 105, 3, 182, 18, 22, 133, 113, 81, 48, 12, 111, 180, 221, 163, 212, 72, 177, 79, 235, 202, 38, 29, 168, 215, 67, 246, 203, 6, 73, 142, 216, 165, 247, 184, 109, 199, 83, 135, 50, 15, 145, 197, 243, 239, 140, 32, 217, 144, 183, 249, 96, 138, 25, 245, 82, 164, 61, 8, 178, 136, 214, 70, 194, 206, 169, 139, 170, 27, 91, 68, 77, 90, 129, 231, 86, 62, 9, 227, 71, 115, 99, 121, 63, 157, 211, 224, 124, 93, 162, 60, 31, 175, 16, 13, 255, 56, 89, 205, 218, 101, 126, 127, 44, 172, 213, 5, 193, 161, 51, 21, 220, 189, 155, 244, 196, 158, 69, 185, 125, 248, 103, 54, 94, 41, 226, 225, 92, 146},
{204, 44, 252, 126, 239, 80, 99, 0, 41, 169, 37, 115, 189, 158, 9, 156, 114, 199, 220, 17, 47, 33, 72, 97, 19, 101, 166, 26, 81, 184, 14, 102, 109, 251, 5, 18, 213, 202, 180, 16, 227, 150, 235, 217, 154, 224, 133, 20, 58, 242, 57, 182, 234, 211, 196, 30, 88, 122, 76, 247, 78, 100, 134, 83, 77, 91, 241, 23, 67, 255, 61, 69, 42, 43, 34, 231, 70, 116, 192, 13, 188, 52, 248, 55, 230, 51, 147, 145, 125, 93, 74, 36, 236, 141, 161, 82, 226, 98, 159, 10, 48, 35, 246, 143, 144, 32, 1, 11, 96, 2, 238, 137, 118, 222, 163, 89, 181, 244, 148, 45, 38, 127, 46, 174, 22, 193, 112, 56, 249, 225, 8, 254, 135, 62, 223, 123, 24, 39, 120, 208, 86, 113, 29, 173, 59, 95, 124, 15, 110, 28, 54, 210, 131, 53, 119, 152, 228, 12, 176, 84, 130, 194, 167, 157, 107, 151, 203, 73, 200, 177, 139, 149, 4, 71, 7, 155, 138, 146, 168, 66, 160, 205, 170, 171, 140, 187, 179, 207, 117, 240, 75, 191, 218, 90, 164, 85, 175, 60, 185, 245, 197, 212, 142, 50, 183, 21, 106, 165, 195, 232, 63, 190, 250, 92, 215, 132, 172, 108, 178, 68, 219, 243, 253, 129, 128, 221, 162, 65, 103, 3, 153, 105, 111, 40, 64, 233, 87, 27, 216, 94, 237, 209, 206, 214, 79, 31, 25, 198, 229, 136, 186, 49, 201, 6, 104, 121},
{21, 32, 181, 185, 31, 172, 117, 150, 241, 61, 167, 140, 99, 6, 178, 12, 42, 168, 131, 148, 194, 206, 231, 154, 0, 100, 27, 141, 35, 55, 146, 17, 73, 30, 9, 98, 219, 197, 103, 128, 5, 248, 118, 72, 221, 166, 228, 149, 119, 182, 247, 84, 233, 162, 209, 210, 237, 20, 18, 83, 176, 123, 109, 3, 46, 36, 52, 112, 86, 79, 28, 157, 179, 220, 155, 163, 2, 59, 236, 184, 88, 77, 199, 50, 201, 196, 191, 44, 33, 66, 8, 156, 107, 39, 142, 158, 238, 215, 25, 49, 14, 48, 10, 242, 24, 116, 136, 68, 229, 255, 250, 208, 81, 67, 111, 254, 173, 224, 230, 133, 93, 54, 22, 47, 78, 37, 226, 45, 217, 145, 183, 105, 63, 104, 189, 92, 113, 23, 58, 153, 97, 89, 60, 213, 243, 152, 244, 102, 192, 187, 223, 75, 161, 234, 51, 198, 90, 110, 127, 239, 95, 126, 71, 11, 38, 85, 175, 4, 106, 80, 143, 203, 134, 253, 57, 135, 82, 138, 186, 16, 188, 144, 15, 129, 121, 180, 40, 41, 147, 246, 76, 53, 252, 87, 62, 171, 70, 200, 130, 151, 169, 165, 114, 7, 122, 177, 34, 170, 94, 132, 1, 91, 204, 74, 227, 65, 69, 202, 56, 212, 125, 137, 240, 43, 160, 214, 249, 207, 96, 101, 13, 26, 235, 216, 218, 139, 174, 251, 195, 245, 211, 124, 115, 193, 159, 108, 205, 64, 225, 222, 232, 164, 190, 120, 29, 19},
{147, 22, 133, 196, 55, 47, 4, 140, 8, 123, 234, 178, 174, 206, 96, 177, 173, 20, 181, 220, 54, 131, 104, 162, 95, 109, 219, 207, 106, 221, 148, 56, 201, 153, 186, 73, 216, 62, 187, 128, 6, 108, 67, 36, 204, 176, 247, 71, 210, 171, 191, 246, 46, 35, 39, 251, 125, 17, 29, 33, 44, 172, 65, 200, 85, 40, 7, 223, 137, 118, 21, 107, 48, 228, 122, 57, 167, 170, 136, 135, 142, 232, 45, 126, 215, 243, 18, 113, 94, 10, 52, 159, 77, 80, 66, 103, 58, 130, 152, 254, 34, 72, 1, 230, 175, 3, 60, 2, 188, 203, 236, 138, 124, 183, 237, 83, 11, 86, 15, 76, 222, 238, 158, 253, 157, 98, 12, 184, 64, 205, 116, 93, 110, 242, 145, 160, 88, 146, 100, 49, 212, 31, 141, 199, 129, 211, 143, 5, 37, 115, 195, 105, 74, 117, 164, 119, 168, 23, 41, 155, 9, 53, 213, 19, 239, 235, 192, 16, 224, 89, 90, 121, 70, 69, 111, 92, 14, 226, 180, 0, 208, 163, 165, 32, 150, 249, 244, 26, 82, 169, 68, 27, 120, 99, 233, 179, 28, 241, 156, 30, 198, 250, 194, 190, 79, 202, 144, 50, 182, 151, 161, 87, 185, 134, 197, 209, 149, 154, 227, 25, 255, 218, 252, 63, 97, 139, 225, 43, 51, 81, 84, 24, 127, 101, 214, 245, 75, 229, 193, 114, 59, 42, 132, 217, 13, 38, 248, 231, 102, 91, 240, 61, 166, 78, 189, 112},
{47, 225, 190, 213, 176, 87, 28, 0, 93, 219, 184, 226, 224, 230, 52, 68, 194, 112, 228, 105, 97, 156, 48, 111, 36, 107, 185, 2, 15, 63, 182, 20, 120, 237, 86, 8, 129, 152, 143, 13, 173, 35, 123, 33, 67, 26, 59, 222, 183, 251, 4, 232, 165, 170, 164, 192, 242, 181, 145, 53, 197, 243, 206, 191, 24, 199, 240, 234, 161, 77, 61, 109, 94, 238, 64, 79, 212, 76, 45, 74, 108, 11, 153, 16, 55, 128, 22, 250, 116, 10, 37, 217, 75, 88, 49, 198, 85, 205, 56, 62, 169, 255, 157, 101, 210, 227, 98, 50, 46, 82, 144, 148, 17, 252, 96, 155, 60, 7, 172, 221, 248, 127, 39, 209, 195, 214, 204, 40, 158, 103, 189, 38, 57, 14, 83, 65, 249, 171, 71, 44, 218, 208, 99, 121, 136, 110, 216, 162, 81, 9, 180, 89, 233, 200, 231, 175, 30, 130, 188, 254, 235, 207, 51, 203, 92, 19, 193, 6, 42, 115, 244, 31, 32, 229, 84, 54, 168, 125, 113, 3, 43, 135, 66, 131, 174, 90, 12, 246, 1, 91, 202, 137, 69, 138, 21, 124, 220, 187, 73, 178, 34, 72, 141, 25, 102, 149, 166, 211, 78, 146, 159, 239, 139, 201, 117, 118, 134, 106, 215, 119, 177, 114, 133, 151, 150, 132, 29, 95, 196, 70, 122, 100, 41, 245, 163, 223, 186, 23, 167, 160, 147, 27, 241, 140, 104, 154, 253, 179, 236, 18, 142, 58, 247, 80, 5, 126},
{150, 121, 211, 128, 145, 25, 254, 242, 225, 215, 123, 49, 187, 226, 78, 252, 202, 233, 55, 140, 167, 133, 13, 255, 29, 231, 19, 171, 40, 179, 160, 229, 129, 190, 64, 142, 21, 223, 213, 99, 164, 90, 210, 27, 3, 112, 208, 163, 139, 74, 4, 91, 79, 220, 245, 50, 238, 147, 100, 26, 114, 138, 52, 193, 206, 117, 148, 75, 181, 168, 188, 88, 1, 81, 156, 216, 152, 60, 22, 240, 132, 250, 28, 6, 36, 124, 191, 54, 108, 72, 180, 111, 243, 169, 110, 201, 39, 33, 207, 159, 14, 199, 212, 236, 194, 70, 0, 94, 58, 42, 7, 46, 65, 158, 198, 53, 8, 122, 137, 9, 104, 170, 115, 234, 182, 157, 18, 80, 47, 5, 20, 77, 45, 102, 126, 61, 76, 89, 31, 59, 224, 186, 44, 155, 116, 227, 185, 92, 109, 228, 32, 69, 184, 232, 219, 73, 120, 71, 68, 43, 125, 95, 98, 165, 24, 149, 176, 62, 237, 246, 189, 141, 244, 239, 30, 101, 113, 166, 218, 214, 34, 17, 11, 10, 105, 235, 197, 248, 241, 217, 127, 51, 106, 247, 97, 253, 86, 192, 119, 154, 177, 205, 12, 118, 153, 48, 103, 251, 82, 23, 230, 83, 203, 144, 249, 66, 85, 178, 162, 63, 204, 41, 175, 135, 57, 15, 130, 200, 16, 195, 136, 134, 107, 35, 84, 37, 67, 38, 173, 56, 209, 221, 196, 183, 151, 143, 174, 93, 161, 131, 222, 96, 2, 146, 87, 172},
{163, 227, 14, 82, 158, 241, 96, 169, 251, 119, 13, 160, 187, 211, 164, 65, 217, 10, 244, 39, 216, 148, 237, 27, 86, 103, 56, 195, 208, 182, 104, 247, 88, 147, 206, 87, 127, 42, 189, 83, 194, 145, 50, 20, 48, 120, 186, 131, 4, 125, 63, 151, 202, 30, 123, 220, 33, 108, 34, 100, 172, 161, 153, 110, 25, 59, 71, 134, 179, 37, 155, 170, 180, 254, 84, 219, 94, 154, 92, 28, 140, 67, 222, 32, 111, 149, 38, 168, 49, 2, 212, 133, 117, 141, 159, 228, 156, 3, 209, 128, 137, 122, 11, 236, 74, 107, 64, 12, 248, 47, 235, 238, 40, 55, 114, 184, 233, 60, 229, 69, 231, 15, 22, 46, 192, 43, 24, 250, 135, 93, 79, 8, 26, 98, 23, 215, 99, 176, 190, 243, 97, 62, 53, 19, 252, 85, 124, 183, 91, 203, 223, 45, 31, 16, 221, 144, 44, 113, 109, 115, 224, 143, 41, 57, 17, 68, 193, 204, 157, 214, 152, 240, 9, 102, 52, 18, 199, 146, 239, 6, 175, 188, 90, 77, 35, 81, 106, 185, 139, 136, 167, 178, 78, 5, 101, 166, 205, 181, 225, 54, 234, 197, 196, 21, 201, 73, 230, 171, 249, 138, 58, 36, 200, 177, 162, 0, 1, 150, 89, 255, 207, 116, 105, 112, 213, 121, 198, 80, 210, 218, 174, 165, 61, 129, 76, 118, 173, 132, 130, 253, 75, 7, 232, 72, 126, 242, 191, 246, 95, 70, 29, 51, 66, 245, 142, 226}};
int *addition_long(int number_in_base[MAX_POSITION_LENGTH])
{
// Add one to number_in_base
//printf("Number_in_base = [%d, %d, %d]\n", number_in_base[0], number_in_base[1], number_in_base[2]);
number_in_base[0] += 1;
// Handle carry
int remainder = 0;
for (int i = 0; i < MAX_POSITION_LENGTH; i++)
{
int s = number_in_base[i] + remainder;
number_in_base[i] = s % BASE;
remainder = s / BASE;
}
return number_in_base;
}
int decode_enigma(int rotor_num[MAX_POSITION_LENGTH], int decode_position[MAX_POSITION_LENGTH], int char_value)
{
/* printf("rotor_num= [%d, %d, %d]\n", rotor_num[0], rotor_num[1], rotor_num[2]);
printf("decode_position= [%d, %d, %d]\n", decode_position[0], decode_position[1], decode_position[2]); */
if (char_value < 0)
{
char_value += BASE;
}
for (int i = MAX_POSITION_LENGTH - 1; i >= 0; i--)
{
char_value = (char_value + decode_position[i]) % BASE;
if (char_value < 0)
{
char_value += BASE;
}
char_value = rotor_inverser[rotor_num[i]][char_value];
char_value = (char_value - decode_position[i]) % BASE;
if (char_value < 0)
{
char_value += BASE;
}
}
return char_value;
// Assume addition_long is a function that increments decode_position
}
void brute_force_enigma(int *message, int **rotor_num, int **position, int attempts)
{
int position_copy[MAX_POSITION_LENGTH] = {0};
printf("Starting on rotor_num [%d, %d, %d]\n", *rotor_num[0], *rotor_num[1], *rotor_num[2]);
int rotor_num_test[] = {*rotor_num[0], *rotor_num[1], *rotor_num[2]};
for (int i = 0; i < attempts; i++)
{
// Assume decode_enigma is a function that modifies local_message_test
for (int j = 0; j < MAX_POSITION_LENGTH; j++){position_copy[j] = *position[j];}
if (decode_enigma(rotor_num_test, position_copy, message[0]) == 74)
{
addition_long(position_copy);
if (decode_enigma(rotor_num_test, position_copy, message[1]) == 111)
{
addition_long(position_copy);
if (decode_enigma(rotor_num_test, position_copy, message[2]) == 235)
{
addition_long(position_copy);
if (decode_enigma(rotor_num_test, position_copy, message[3]) == 108)
{
printf("\"Joël\" found in the message with position = [%d, %d, %d] and rotor_num = [%d, %d, %d]\n",*position[0], *position[1], *position[2], *rotor_num[0], *rotor_num[1], *rotor_num[2]);
//*rotor_num_test = rotor_num;
//*position_test = position;
return;
}
}
}
}
//printf("Position: [%d, %d, %d]\n", *position[0], *position[1], *position[2]);
for (int j = 0; j < MAX_POSITION_LENGTH; j++){position_copy[j] = *position[j];}
addition_long(position_copy);
//position = addition_long((int[]){*position[0], *position[1], *position[2]});
for (int j = 0; j < MAX_POSITION_LENGTH; j++){*position[j] = position_copy[j];}
// printf("Position: [%d, %d, %d]\n", *position[0], *position[1], *position[2]);
if (*position[0] == 0 && *position[1] == 0 && *position[2] == 0)
{
printf("Done with rotor [%d, %d, %d]\n", *rotor_num[0], *rotor_num[1], *rotor_num[2]);
int *no_result = malloc(sizeof(int));
if (no_result == NULL)
{
printf("Memory allocation failed\n");
return;
}
*no_result = -1;
*rotor_num = no_result;
no_result = malloc(sizeof(int));
if (no_result == NULL)
{
printf("Memory allocation failed\n");
return;
}
*no_result = -1;
*position = no_result;
return;
}
}
}
int main()
{
int *rotor_num[] = {(int[]){5}, (int[]){1}, (int[]){7}};
int *positions[] = {(int[]){0}, (int[]){0}, (int[]){0}};
brute_force_enigma((int[]){32, 192, 129, 176}, rotor_num, positions, 16777216);
int *rotor_num_bis[] = {(int[]){4}, (int[]){1}, (int[]){7}};
int *positions_bis[] = {(int[]){0}, (int[]){0}, (int[]){0}};
brute_force_enigma((int[]){32, 192, 129, 176}, rotor_num_bis, positions_bis, 16777216);
/* int positions[] = {0, 0, 0};
for (int i = 0; i < 500; i++)
{
addition_long(positions, 1);
printf("Positions: [%d, %d, %d]\n", positions[0], positions[1], positions[2]);
} */
}