-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenigma101.c
More file actions
209 lines (197 loc) · 5.34 KB
/
enigma101.c
File metadata and controls
209 lines (197 loc) · 5.34 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
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
#include <stdio.h>
#include <string.h>
void rotate(int arr[26])
{
int temp=arr[0];
int i=0;
for(i=0;i<25;i++)
{
arr[i]=arr[i+1];
}
arr[25]=temp;
}
int main(void) {
// your code goes here
///////////////////////////// defining the rotors //////////////////////////////////////////////////
int fast_rotor_array2[26] = {16,3,5,7,9,11,13,4,17,19,21,25,23,1,15,26,6,10,24,8,2,12,14,22,20,18};
int medium_rotor_array2[26] = {23,12,9,2,18,6,22,15,3,5,14,21,11,4,20,16,25,19,26,8,24,7,10,13,17,1};
int slow_rotor_array2[26] = {17,5,11,6,14,16,25,15,12,10,7,18,26,19,9,20,1,22,2,4,23,3,24,21,8,13};
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////// defining the starting point of each rotor ////////////////////////////////
int stp_fast = 14;
int stp_medium = 15;
int stp_slow = 16;
///////////////////////////////////////////////////////////////////////////////////////////////////
char confirm = 'Y';
while(confirm == 'Y')
{
//the initial user choices displayed
printf("***************************************************\n");
printf("enter 1 - to enter the starting rotor letters\n");
printf("enter 2 - to input a word to encrypt\n");
printf("***************************************************\n");
int choice=0;
scanf("%d",&choice);
switch(choice)
{
case 1: ;
//Enter the initial rotor Configurations
char r1;
char r2;
char r3;
printf("enter the rotor configurations : ( fast medium slow)\n");
scanf(" %c %c %c",&r1,&r2,&r3);
int n1=r1-65;
int n2=r2-65;
int n3=r3-65;
printf("%d %d %d\n",n1,n2,n3);
while(fast_rotor_array2[0]!=(n1+1))
{
rotate(fast_rotor_array2);
if(fast_rotor_array2[0]==stp_fast)
rotate(medium_rotor_array2);
}
while(medium_rotor_array2[0]!=(n2+1))
{
rotate(medium_rotor_array2);
if(medium_rotor_array2[0]==stp_medium)
rotate(slow_rotor_array2);
}
while(slow_rotor_array2[0]!=(n3+1))
rotate(slow_rotor_array2);
break;
case 2: ;
printf("Enter the word to be encrypted :\n");
char str[100];
scanf("%s",str);
char newstr[strlen(str)];
int n=1;
while(n<=strlen(str))
{
char ch1 = str[n-1];
int i=0;
int index = ch1-65;
int temp = fast_rotor_array2[0];
if(fast_rotor_array2[0]==stp_fast)
rotate(medium_rotor_array2);
if(medium_rotor_array2[0]==stp_medium)
rotate(slow_rotor_array2);
for(i=0;i<25;i++)
{
fast_rotor_array2[i]= fast_rotor_array2[i+1];
}
fast_rotor_array2[25]=temp;
int output1 = fast_rotor_array2[index];
int output2 = medium_rotor_array2[output1-1];
int output3 = slow_rotor_array2[output2-1];
int newoutput2=0;
int newoutput1=0;
int newoutput=0;
//reflector
switch (output3)
{
case 1:
output3 = 9;
break;
case 2:
output3 =24;
break;
case 3:
output3 =21;
break;
case 4:
output3 = 8;
break;
case 5:
output3 = 6;
break;
case 6:
output3 =5;
break;
case 7:
output3 =26;
break;
case 8:
output3 = 4;
break;
case 9:
output3 = 1;
break;
case 10:
output3 = 15;
break;
case 11:
output3 = 13;
break;
case 12:
output3 = 20;
break;
case 13:
output3 = 11;
break;
case 14:
output3 = 17;
break;
case 15:
output3 = 10;
break;
case 16:
output3 = 23;
break;
case 17:
output3 = 14;
break;
case 18:
output3 = 19;
break;
case 19:
output3 = 18;
break;
case 20:
output3 = 12;
break;
case 21:
output3 = 3;
break;
case 22:
output3 = 25;
break;
case 23:
output3 = 16;
break;
case 24:
output3 = 2;
break;
case 25:
output3 = 22;
break;
case 26:
output3 = 7;
break;
}
for(i=0;i<26;i++)
{
if(output3==slow_rotor_array2[i])
newoutput2=i+1;
}
for(i=0;i<26;i++)
{
if(newoutput2==medium_rotor_array2[i])
newoutput1=i+1;
}
for(i=0;i<26;i++)
{
if(newoutput1==fast_rotor_array2[i])
newoutput=i;
}
newstr[n-1]=(char)(65+newoutput);
n++;
}
printf("The encrypted word :-\n");
printf("%s\n",newstr);
}
printf("Do you want to continue Y/N ?\n");
scanf(" %c",&confirm);
}
return 0;
}