forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModifiedCaesarCipher.java
449 lines (420 loc) · 17.6 KB
/
ModifiedCaesarCipher.java
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import java.util.Scanner;
/**
* <h1>Modified Caesar Cipher</h1>
* <p>
* To Implement Modified Version of Caesar Cipher
* </p>
* <b>Description</b>
* <p>
* class ModifiedCaesarCipher is used to encrypt plainText to cipherText using Modified Caesar Algorithm and decrypt
* cipherText to plainText using the respective algorithm, in Modified Caesar Cipher the encryption takes place by
* increasing or decreasing the key-value to the character based on its index number from A-Z series, instead of just
* increasing the key to the character which was done in Caesar Cipher, which provides more enhancement in security and
* decreases the chances of attack.
* </p>
* <b>Requirements</b>
* <p>
* A string plain text and an integer key are required for encryption and for decryption the cipher text which is
* calculated from the given key and the key itself.
* </p>
*
* <b>Reference for understanding the algorithm</b>
* <a href="https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.402.8295&rep=rep1&type=pdf"> Modified CaesarCipher
* for Better Security Enhancement </a>
*
*/
public class ModifiedCaesarCipher
{
/**
* <p>
* This is a public method used to encrypt plainText using integer key using Modified Caesar Cipher algorithm.
* </p>
* <b>Process</b>
* <ul>
* <li>a string plain text and integer key are taken as input</li>
* <li>iteratively each character of plain text is encrypted
* <ul>
* <li>The character index of the current character is calculated, if the character is whitespace then it is not
* calculated</li>
* <li>If the character index is even then the character value is incremented by the key-value, else decremented</li>
* <li>If the sum of the character value and key-value extends or descends within the letter group range (A-Z,a-z,0-9),
* then the values are circulated and encrypted unless and until their encrypted value comes it the range.</li>
* <li>The encrypted value is returned</li>
* </ul>
* </li>
* </ul>
*
* @param plainText This is a String parameter to encrypt method, this signifies plain text which is passed.
* @param key This is an Integer parameter to encrypt method, this signifies key which is passed to encrypt
* plainText.
*
* @return String This returns the cipherText.
*/
public String encrypt(String plainText, Integer key)
{
/*
* StringBuilder is used because, it makes a mutable sequence of characters, due to appending of each encrypted
* character in cipherText, if we use String then it could cause creation of many immutable objects, so to make the code
* more efficient, StringBuilder is used.
*/
StringBuilder cipherText = new StringBuilder("");
// Iterating through each character in plainText
for (int i = 0; i < plainText.length(); i++)
{
// Checking for the character index and storing its value in variable 'characterIndex'
Integer characterIndex = 0;
// Withing range a-z
if (plainText.charAt(i) >= 'a' && plainText.charAt(i) <= 'z')
{
characterIndex = plainText.charAt(i) - 'a';
}
// Withing range A-Z
else if (plainText.charAt(i) >= 'A' && plainText.charAt(i) <= 'Z')
{
characterIndex = plainText.charAt(i) - 'A';
}
// Withing range 0-9
else if (plainText.charAt(i) >= '0' && plainText.charAt(i) <= '9')
{
characterIndex = plainText.charAt(i) - '0' + 1;
}
// Special Characters
else
{
cipherText.append(plainText.charAt(i));
continue;
}
/*
* If there's an occurrence of whitespace observed in the plainText then it is passed as it is to the cipherText without
* encrypting it.
*/
if (plainText.charAt(i) == ' ')
{
cipherText.append(' ');
continue;
}
int encryptedChar = plainText.charAt(i);
// If the value characterIndex is even then the value of the character is incremented
if (characterIndex % 2 == 0)
{
encryptedChar += key;
}
// If the value characterIndex is odd then the value of the character is decremented
else
{
encryptedChar -= key;
}
/*
* Flag used to know that if the encrypted character is withing its group range or not
*
* exit = false signifies that the character is within the range
*
* exit = true signifies that the character was not within its range and needs one more iteration for circulation
*/
boolean exit = false;
do
{
if (plainText.charAt(i) >= 'a' && plainText.charAt(i) <= 'z')
{
exit = false;
if (encryptedChar < 'a' || encryptedChar > 'z')
{
if (encryptedChar < 'a')
{
encryptedChar += ('z' - 'a' + 1);
}
else if (encryptedChar > 'z')
{
encryptedChar -= ('z' - 'a' + 1);
}
exit = true;
}
}
else if (plainText.charAt(i) >= 'A' && plainText.charAt(i) <= 'Z')
{
exit = false;
if (encryptedChar < 'A' || encryptedChar > 'Z')
{
if (encryptedChar < 'A')
{
encryptedChar += ('Z' - 'A' + 1);
}
else if (encryptedChar > 'Z')
{
encryptedChar -= ('Z' - 'A' + 1);
}
exit = true;
}
}
else if (plainText.charAt(i) >= '0' && plainText.charAt(i) <= '9')
{
exit = false;
if (encryptedChar < '0' || encryptedChar > '9')
{
if (encryptedChar < '0')
{
encryptedChar += ('9' - '0' + 1);
}
else if (encryptedChar > '9')
{
encryptedChar -= ('9' - '0' + 1);
}
exit = true;
}
}
}
while (exit);
/*
* At the end of for loop(which was used to iterate through each character in plainText), the encrypted character
* 'encryptedChar' is appended to cipherText
*/
cipherText.append((char) encryptedChar);
}
// cipher text is retured
return cipherText.toString();
}
/**
* <p>
* This is a public method used to decrypt cipherText using integer key using Modified Caesar Cipher algorithm.
* </p>
* <b>Process</b>
* <ul>
* <li>a string cipher text and integer key are taken as input</li>
* <li>iteratively each character of cipher text is decrypted
* <ul>
* <li>The character index of the current character is calculated, if the character is whitespace then it is not
* calculated</li>
* <li>If the key is even then
* <ul>
* <li>If the character index is even then the character value is decremented by the key-value</li>
* <li>If the character index is odd then the character value is incremented by the key-value</li>
* </ul>
* </li>
* <li>If the key is odd then
* <ul>
* <li>If the character index is even then the character value is incremented by the key-value</li>
* <li>If the character index is odd then the character value is decremented by the key-value</li>
* </ul>
* </li>
* <li>If the sum of the character value and key-value extends or descends within the letter group range (A-Z,a-z,0-9),
* then the values are circulated and decrepted unless and until their decrypted value comes it the range.</li>
* <li>The decrypted value is returned</li>
* </ul>
* </li>
* </ul>
*
* @param cipherText This is a String parameter to decrypt method, this signifies cipher text which is passed.
* @param key This is an Integer parameter to decrypt method, this signifies key which is passed to decrypt
* cipherText.
*
* @return String This returns the plainText.
*/
public String decrypt(String cipherText, Integer key)
{
/*
* StringBuilder is used because, it makes a mutable sequence of characters, due to appending of each decrypted
* character in plainText, if we use String then it could cause creation of many immutable objects, so to make the code
* more efficient, StringBuilder is used.
*/
StringBuilder plainText = new StringBuilder("");
// Iterating through each character in cipherText
for (int i = 0; i < cipherText.length(); i++)
{
// Checking for the character index and storing its value in variable 'characterIndex'
Integer characterIndex = 0;
// Withing range a-z
if (cipherText.charAt(i) >= 'a' && cipherText.charAt(i) <= 'z')
{
characterIndex = cipherText.charAt(i) - 'a';
}
// Withing range A-Z
else if (cipherText.charAt(i) >= 'A' && cipherText.charAt(i) <= 'Z')
{
characterIndex = cipherText.charAt(i) - 'A';
}
// Withing range 0-9
else if (cipherText.charAt(i) >= '0' && cipherText.charAt(i) <= '9')
{
characterIndex = cipherText.charAt(i) - '0' + 1;
}
// Special Characters
else
{
plainText.append(cipherText.charAt(i));
continue;
}
/*
* If there's an occurrence of whitespace observed in the cipherText then it is passed as it is to the plainText without
* decrypting it.
*/
if (cipherText.charAt(i) == ' ')
{
plainText.append(' ');
continue;
}
int decryptedChar = cipherText.charAt(i);
/*
* AND logic is used to determine increment or decrement in the cipherText
*
* 0 X 1 = 0
*
* 1 X 0 = 0
*
* 1 X 1 = 1
*
* 0 X 0 = 0
*
* Where 0 determines odd number and 1 determines even in this case
*/
// If the value key is even
if (key % 2 == 0)
{
// If the value characterIndex is even then the value of the character is decremented
if (characterIndex % 2 == 0)
{
decryptedChar -= key;
}
// If the value characterIndex is odd then the value of the character is incremented
else
{
decryptedChar += key;
}
}
// If the value key is odd
else
{
// If the value characterIndex is even then the value of the character is incremented
if (characterIndex % 2 == 0)
{
decryptedChar += key;
}
// If the value characterIndex is odd then the value of the character is decremented
else
{
decryptedChar -= key;
}
}
/*
* Flag used to know that if the decrypted character is withing its group range or not
*
* exit = false signifies that the character is within the range
*
* exit = true signifies that the character was not within its range and needs one more iteration for circulation
*/
boolean exit = false;
do
{
if (cipherText.charAt(i) >= 'a' && cipherText.charAt(i) <= 'z')
{
exit = false;
if (decryptedChar < 'a' || decryptedChar > 'z')
{
if (decryptedChar < 'a')
{
decryptedChar += ('z' - 'a' + 1);
}
else if (decryptedChar > 'z')
{
decryptedChar -= ('z' - 'a' + 1);
}
exit = true;
}
}
else if (cipherText.charAt(i) >= 'A' && cipherText.charAt(i) <= 'Z')
{
exit = false;
if (decryptedChar < 'A' || decryptedChar > 'Z')
{
if (decryptedChar < 'A')
{
decryptedChar += ('Z' - 'A' + 1);
}
else if (decryptedChar > 'Z')
{
decryptedChar -= ('Z' - 'A' + 1);
}
exit = true;
}
}
else if (cipherText.charAt(i) >= '0' && cipherText.charAt(i) <= '9')
{
exit = false;
if (decryptedChar < '0' || decryptedChar > '9')
{
if (decryptedChar < '0')
{
decryptedChar += ('9' - '0' + 1);
}
else if (decryptedChar > '9')
{
decryptedChar -= ('9' - '0' + 1);
}
exit = true;
}
}
}
while (exit);
/*
* At the end of for loop(which was used to iterate through each character in cipherText), the decrypted character
* 'decryptedChar' is appended to plainText
*/
plainText.append((char) decryptedChar);
}
// plain text is retured
return plainText.toString();
}
/**
* This is the main method which makes use of the following methods:
* <ul>
* <li>encrypt</li>
* <li>decrypt</li>
* </ul>
*
* And uses the following objects of types:
* <ul>
* <li>java.util.Scanner</li>
* <li>java.lang.Integer</li>
* <li>ModifiedCaesarCipher</li>
* </ul>
*
* @param args not used
*/
public static void main(String[] args)
{
Integer key; // Created an object of Integer class for storing key to be entered by the user
String plainText; // Create an object of String class for storing plain text to be entered by the user
Scanner scan = new Scanner(System.in); // Created an object of Scanner class for asking for user input.
ModifiedCaesarCipher modifiedCaeserCipher = new ModifiedCaesarCipher();// Created an object of ModifiedCaesarCipher for accessing it's method for encryption and decryption
// Asking user to enter plain text
System.out.println("Please Enter Plain Text:");
plainText = scan.nextLine();
// Asking user to enter integer key
System.out.println("Please Enter Key:");
key = scan.nextInt();
// Accessing method 'encrypt' of ModifiedCaesarCipher class to encrypt plainText by key
String cipherText = modifiedCaeserCipher.encrypt(plainText, key);
// Printing cipher text
System.out.println("Cipher Text: " + cipherText);
// Accessing method 'decrypt' of ModifiedCaesarCipher class to decrypt cipherText by key
String decryptedText = modifiedCaeserCipher.decrypt(cipherText, key);
// Printing decrypted text
System.out.println("Decrypted Text: " + decryptedText);
// Closing the Scanner object scan
scan.close();
}
}
/*
* OUTPUT:
*
* Please Enter Plain Text:
*
* Attack Alice's troops at 5:00 PM today
*
* Please Enter Key:
*
* 3
*
* Cipher Text: Dqqdfn Dilfh'v qorrmv dq 8:77 MP qradb
*
* Decrypted Text: Attack Alice's troops at 5:00 PM today
*/