This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRottor.cc
273 lines (256 loc) · 6.95 KB
/
Rottor.cc
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
Rottor::Rottor()
{
Key=0;
TempCount=0;
Count=0;
cout<<"Enter the Rottor Number "<<endl;
cin>>RottorNo;
char a[26]={ 'w','r','y','i','p','k','h','f','s','z','c','b','m','q', 'n','e','v','t','x','u','a','o','d','l','g','j' };
AlphaArray=new char[strlen(a)]; //setting the address of the AlphaArray
strcpy(AlphaArray,a); //copying the data from a to TempArray
TempArray=new char[strlen(a)]; //creating a new memory with length same as AlphaArray is
strcpy(TempArray,a); //copying the data from a to TempArray
SetRottor(); //calling SetRottor function to set TempArray according to the Key
}
Rottor::Rottor(int k,char* ta,int RNo, int FFF)
{
RottorNo=RNo;
Count=0;
TempCount=0;
Key=k; //setting the Key
char a[26]={'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'};
char p[26]={ 'w','r','y','i','p','k','h','f','s','z','c','b','m','q', 'n','e','v','t','x','u','a','o','d','l','g','j' };
AlphaArray=new char[strlen(a)]; //setting the address of the AlphaArray
strcpy(AlphaArray,a); //copying the data from a to TempArray
if( FFF == 1 )
{
TempArray=new char[strlen(ta)]; //creating a new memory with length same as AlphaArray is
strcpy(TempArray,a); //copying the data from a to TempArray
}
else
{
TempArray=new char[strlen(p)]; //creating a new memory with length same as AlphaArray is
strcpy(TempArray,p);
}
SetRottor(); //calling SetRottor function to set TempArray according to the Key
}
void Rottor::Rotate(int time,bool Dir)
{
int Size=strlen(TempArray);
if(Dir==true) //if true rotate Clock Wise
{
//rotating clock wise for time 'time'
for(int i=0;i<time;i++)
{
char temp=TempArray[0];
for(int i=0;i<Size;i++)
{
TempArray[i]=TempArray[i+1];
}
TempArray[25]=temp;
}
}
if(Dir==false)//rotating Anti-ClockWise
{
for(int i=0;i<time;i++)
{
char temp=TempArray[25];
for(int i=Size;i>=0;i--)
{
TempArray[i]=TempArray[i-1];
}
TempArray[0]=temp;
}
}
}
char Rottor::Encrypt(char ch)
{
if(RottorNo==1)// 1st Rottor rotates every time it Encrypt
{
if(TempCount==0)//first half cycle of encryption
{
TempCount++;
Rotate(1,true); //rotating one time clock wise
return GetCharT(GetPositionA(ch));
}
else if(TempCount==1)
{
TempCount--;//in secod half cycle we doses not rotate the rottor
return GetCharA(GetPositionT(ch));
}
}
if(RottorNo==2)
{
if(TempCount==0)
{
if(Count==26)
{
Rotate(1,true);
Count=0;
}
Count++;
TempCount++;
return GetCharT(GetPositionA(ch));
}
else if(TempCount==1)
{
TempCount--;
return GetCharA(GetPositionT(ch));
}
}
if(RottorNo==3)
{
if(TempCount==0)
{
if(Count==52)
{
Rotate(1,true);
Count=0;
}
Count++;
TempCount++;
return GetCharT(GetPositionA(ch));
}
else if(TempCount==1)
{
TempCount--;
return GetCharA(GetPositionT(ch));
}
}
}
/*
char Rottor::DeCrypt(char ch)
{
if(RottorNo==1)// 1st Rottor rotates every time it Encrypt
{
if(TempCount==0)//first half cycle of encryption
{
TempCount++;
Rotate(1,false); //rotating one time clock wise
return GetCharT(GetPositionA(ch));
}
else if(TempCount==1)
{
TempCount--;
return GetCharA(GetPositionT(ch));
}
}
if(RottorNo==2)
{
if(TempCount==0)
{
if(Count==26)
{
Rotate(1,false);
Count=0;
}
Count++;
TempCount++;
return GetCharT(GetPositionA(ch));
}
else if(TempCount==1)
{
TempCount--;
return GetCharA(GetPositionT(ch));
}
}
if(RottorNo==3)
{
if(TempCount==0)
{
if(Count==52)
{
Rotate(1,true);
Count=0;
}
Count++;
TempCount++;
return GetCharA(GetPositionT(ch));
}
else if(TempCount==1)
{
TempCount--;
return GetCharT(GetPositionA(ch));
}
}
*/
int Rottor::GetPositionT(char ch) //getting the present position of char ah in TempArray
{
for(int i=0;i<26;i++)
{
if(TempArray[i]==ch) //matching
{
return i; //returning the address of the matched char with ch
}
}
}
int Rottor::GetPositionA(char ch) //getting the present position of char ah in TempArray
{
for(int i=0;i<26;i++)
{
if(AlphaArray[i]==ch) //matching
{
return i; //returning the address of the matched char with ch
}
}
}
char Rottor::GetCharA(int no)
{
return AlphaArray[no]; //returning the char present at the index no in AlphaArray
}
char Rottor::GetCharT(int no)
{
return TempArray[no]; //returning the char present at the index no in AlphaArray
}
void Rottor::SetRottor()
{
int temp=GetPositionT('a'); //getting the present position of a char in the TempArray
/*
There appear two cases when Key>25 & Key<=25
In 1st case Key>25:
Than we have to calculate the position. As we have only available positions are 26 in Range (0 to 25) and we have to convert it
Think about a numbering system with base 26 which will have numbers only in Range (0 to 25) so we have to convert Key into 26-Base
system and the remainder will be the new index or Key. And repeating the process of case 1.
In 2nd case Key<=25:
in this case there are further two cases char 'a' can be at required position or not
When char is present at required position:
than we have to do nothing because Rottor is already in required Position
When char is not present at required position:
So if Key<temp this means that we have to rotate TempArray in Clock-wise direction by (temp-Key) times
So if Key>temp this means that we have to rotate TempArray in AntiClock-wise direction by (Key-temp) times
*/
if(Key>25) //case1
{
int TempKey=Key%26; //calculating the position according to the Key
if(temp!=Key)
{
if(TempKey<temp)
{
Rotate(temp-TempKey,true); //rotating Clock_wise by "temp-Key" time
}
else if(TempKey>temp)
{
Rotate(TempKey-temp,false);//rotating AntiClock_wise by "Key-temp" time
}
}
}
if(Key<=25) //case 2
{
if(temp!=Key) //position is not matching
{
if(Key<temp) //present at behind the required position
{
Rotate(temp-Key,true); //rotating Clock_wise by "temp-Key" time
}
else if(Key>temp)//present ahead from the the required position
{
Rotate(Key-temp,false);//rotating AntiClock_wise by "Key-temp" time
}
}
}
}
Rottor::~Rottor()
{
delete[] TempArray;
delete[] AlphaArray;
}