-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
648 lines (534 loc) · 13.6 KB
/
Copy pathProgram.cs
File metadata and controls
648 lines (534 loc) · 13.6 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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#region Configuration Variables
// Used to control the version number
const string version = "0.1";
#endregion
#region Variables
// Integers
int offset = -1;
// Strings
string encryptedMessage;
string decryptedMessage;
// Chars
char[] specialCharArray = {
'`',
'~',
'+',
'='
};
// Dictionaries
Dictionary<int, char> alphabetIndexLetter = new Dictionary<int, char>()
{
{ 0, 'a' },
{ 1, 'b' },
{ 2, 'c' },
{ 3, 'd' },
{ 4, 'e' },
{ 5, 'f' },
{ 6, 'g' },
{ 7, 'h' },
{ 8, 'i' },
{ 9, 'j' },
{ 10, 'k' },
{ 11, 'l' },
{ 12, 'm' },
{ 13, 'n' },
{ 14, 'o' },
{ 15, 'p' },
{ 16, 'q' },
{ 17, 'r' },
{ 18, 's' },
{ 19, 't' },
{ 20, 'u' },
{ 21, 'v' },
{ 22, 'w' },
{ 23, 'x' },
{ 24, 'y' },
{ 25, 'z' },
{ 26, ',' },
{ 27, '.' },
{ 28, '<' },
{ 29, '>' },
{ 30, '?' },
{ 31, '/' },
{ 32, ':' },
{ 33, ';' },
{ 34, '"' },
{ 35, '\'' },
{ 36, '{' },
{ 37, '[' },
{ 38, '}' },
{ 39, ']' },
{ 40, '|' },
{ 41, '\\' },
{ 42, 'Z' },
{ 43, 'Y' },
{ 44, 'X' },
{ 45, 'W' },
{ 46, 'V' },
{ 47, 'U' },
{ 48, 'T' },
{ 49, 'S' },
{ 50, 'R' },
{ 51, 'Q' },
{ 52, 'P' },
{ 53, 'O' },
{ 54, 'N' },
{ 55, 'M' },
{ 56, 'L' },
{ 57, 'K' },
{ 58, 'J' },
{ 59, 'I' },
{ 60, 'H' },
{ 61, 'G' },
{ 62, 'F' },
{ 63, 'E' },
{ 64, 'D' },
{ 65, 'C' },
{ 66, 'B' },
{ 67, 'A' },
{ 68, '!' },
{ 69, '1' },
{ 70, '@' },
{ 71, '2' },
{ 72, '#' },
{ 73, '3' },
{ 74, '$' },
{ 75, '4' },
{ 76, '%' },
{ 77, '5' },
{ 78, '^' },
{ 79, '6' },
{ 80, '&' },
{ 81, '7' },
{ 82, '*' },
{ 83, '8' },
{ 84, '(' },
{ 85, '9' },
{ 86, ')' },
{ 87, '0' },
{ 88, '_' },
{ 89, '-' }
};
Dictionary<char, int> alphabetLetterIndex = new Dictionary<char, int>()
{
{ 'a', 0 },
{ 'b', 1 },
{ 'c', 2 },
{ 'd', 3 },
{ 'e', 4 },
{ 'f', 5 },
{ 'g', 6 },
{ 'h', 7 },
{ 'i', 8 },
{ 'j', 9 },
{ 'k', 10 },
{ 'l', 11 },
{ 'm', 12 },
{ 'n', 13 },
{ 'o', 14 },
{ 'p', 15 },
{ 'q', 16 },
{ 'r', 17 },
{ 's', 18 },
{ 't', 19 },
{ 'u', 20 },
{ 'v', 21 },
{ 'w', 22 },
{ 'x', 23 },
{ 'y', 24 },
{ 'z', 25 },
{ ',', 26 },
{ '.', 27 },
{ '<', 28 },
{ '>', 29 },
{ '?', 30 },
{ '/', 31 },
{ ':', 32 },
{ ';', 33 },
{ '"', 34 },
{ '\'', 35 },
{ '{', 36 },
{ '[', 37 },
{ '}', 38 },
{ ']', 39 },
{ '|', 40 },
{ '\\', 41 },
{ 'Z', 42 },
{ 'Y', 43 },
{ 'X', 44 },
{ 'W', 45 },
{ 'V', 46 },
{ 'U', 47 },
{ 'T', 48 },
{ 'S', 49 },
{ 'R', 50 },
{ 'Q', 51 },
{ 'P', 52 },
{ 'O', 53 },
{ 'N', 54 },
{ 'M', 55 },
{ 'L', 56 },
{ 'K', 57 },
{ 'J', 58 },
{ 'I', 59 },
{ 'H', 60 },
{ 'G', 61 },
{ 'F', 62 },
{ 'E', 63 },
{ 'D', 64 },
{ 'C', 65 },
{ 'B', 66 },
{ 'A', 67 },
{ '!', 68 },
{ '1', 69 },
{ '@', 70 },
{ '2', 71 },
{ '#', 72 },
{ '3', 73 },
{ '$', 74 },
{ '4', 75 },
{ '%', 76 },
{ '5', 77 },
{ '^', 78 },
{ '6', 79 },
{ '&', 80 },
{ '7', 81 },
{ '*', 82 },
{ '8', 83 },
{ '(', 84 },
{ '9', 85 },
{ ')', 86 },
{ '0', 87 },
{ '_', 88 },
{ '-', 89 }
};
#endregion
#region Main
MenuSelection();
#endregion
#region Methods
/*
*
* DisplayMenuBanner()
*
* Used to show the banner for the
* main menu.
*
*/
void DisplayMenuBanner()
{
Clear();
WriteLine("");
WriteLine("***********************************************");
WriteLine("* *");
WriteLine("* _/_/_/ _/_/_/ *");
WriteLine("* _/ _/ *");
WriteLine("* _/ _/ *");
WriteLine("* _/ _/ *");
WriteLine("* _/_/_/ _/_/_/ *");
WriteLine("* *");
WriteLine("***********************************************");
WriteLine("* CAESAR CIPHER *");
WriteLine("***********************************************");
WriteLine($"* VERSION: {version} *");
WriteLine("***********************************************");
}
/*
*
* MenuSelection()
*
* Displays the root menu selections and takes the
* users input.
*
*/
void MenuSelection()
{
// Variables
string userSelection = "";
string intermittentMessage = "";
bool isIncorrectSelection = true;
bool intermittentMessageExists = false;
// User needs to make a correct selection, otherwise the application
// reiterates through this while loop
while (isIncorrectSelection)
{
// Display the menu options
DisplayMenuBanner();
// Display the intermittent message
if (intermittentMessageExists)
{
WriteLine($"\n{userSelection} is not a correct choice!\n");
}
else
{
WriteLine("");
}
WriteLine("Please make a selection from one of the following options:\n");
WriteLine(" 1) Create a new message");
WriteLine(" 2) Decrypt an existing message");
WriteLine(" 3) Quit Caesar Cipher\n");
Write("Your selection >> ");
userSelection = ReadLine();
// Switch through the selections and create a default selection
switch (userSelection)
{
// 1: Create a new message
case "1":
isIncorrectSelection = false;
CreateMessage();
break;
// 2: Decrypt a message
case "2":
isIncorrectSelection = false;
DecryptMessage();
break;
// 3: Quit the app
case "3":
Clear();
Environment.Exit(0);
break;
// Default: Incorrect Choice
default:
intermittentMessageExists = true;
break;
}
}
}
/*
*
* CreateMessage()
*
* Begins the process of creating an encrypted message.
*
*/
void CreateMessage()
{
// Variables
bool isNotAnInt = true;
bool successfullyConverted = false;
bool intermittentMessageExists = false;
// User needs to enter an offset (integer). If the user
// input is not an integer, reiterate.
while (isNotAnInt)
{
// Display the banner
DisplayMenuBanner();
// Display the intermittent message
if (intermittentMessageExists)
{
WriteLine($"\nNot a correct offset!\n");
}
else
{
WriteLine("");
}
Write("Please enter a cipher offset (0 - 89) >> ");
successfullyConverted = Int32.TryParse(ReadLine(), out offset);
// If it is an integer ? continue : restart the while loop.
if (successfullyConverted)
{
// Test to see if the integer is between 0 - 89 ? continue : restart the while loop.
if (offset >= 0 && offset <= 89)
{
isNotAnInt = false;
encryptedMessage = CreateEncryptedMessage(offset);
DisplayMenuBanner();
WriteLine("\nYour encrypted message:\n");
WriteLine($" {encryptedMessage}");
Write("\nPress any key to continue...");
ReadLine();
MenuSelection();
}
else
{
// Show the intermittent message
intermittentMessageExists = true;
}
}
else
{
// Show the intermittent message
intermittentMessageExists = true;
}
}
}
/*
*
* DecryptMessage()
*
* Begins the process of decrypting an encrypted message.
*
*/
void DecryptMessage()
{
// Variables
bool isNotAnInt = true;
bool successfullyConverted = false;
bool intermittentMessageExists = false;
// User needs to enter an offset (integer). If the user
// input is not an integer, reiterate.
while (isNotAnInt)
{
// Display the banner
DisplayMenuBanner();
// Display the intermittent message
if (intermittentMessageExists)
{
WriteLine($"\nNot a correct offset!\n");
}
else
{
WriteLine("");
}
Write("Please enter a cipher offset (0 - 89) >> ");
successfullyConverted = Int32.TryParse(ReadLine(), out offset);
// If it is an integer ? continue : restart the while loop.
if (successfullyConverted)
{
// Test to see if the integer is between 0 - 89 ? continue : restart the while loop.
if (offset >= 0 && offset <= 89)
{
decryptedMessage = DecryptEncryptedMessage(offset);
DisplayMenuBanner();
WriteLine("\nYour decrypted message:\n");
WriteLine($" {decryptedMessage}");
Write("\nPress any key to continue...");
ReadLine();
MenuSelection();
}
else
{
// Show the intermittent message
intermittentMessageExists = true;
}
}
else
{
// Show the intermittent message
intermittentMessageExists = true;
}
}
}
/*
*
* CreateEncryptedMessage(int userOffset)
*
* This method is the method that creates an encrypted
* Caesar Cipher message. Uses the alpabet and an offset
* that is chosen by the user.
*
*/
string CreateEncryptedMessage(int userOffset)
{
// Variables
StringBuilder hashedString = new StringBuilder();
string userPlaintextString = "";
int tempIndex = 0;
// Get the user's plaintext message
DisplayMenuBanner();
Write("\nPlease enter the message you want to encrypt >> ");
userPlaintextString = ReadLine();
// Iterate though the userPlaintextString and encrypt it.
foreach (char userChar in userPlaintextString)
{
// Check for a space char
if (userChar == ' ')
{
// Run GenerateSpaceChar() appened it to the stringbuilder
hashedString.Append(GenerateSpaceChar(specialCharArray));
}
else
{
// Get the index from the user char
tempIndex = alphabetLetterIndex[userChar];
// Add the offset to the tempIndex
tempIndex = tempIndex + userOffset;
// If the tempIndex is greater than 89
if (tempIndex > 89)
{
// Subtract 26 from tempIndex to loop back to the
// beginning of the alphabet
tempIndex = tempIndex - 90;
// Get the letter that corresponds with the new index
// and append it to the stringbuilder
hashedString.Append(alphabetIndexLetter[tempIndex]);
}
else
{
// Get the letter that corresponds with the new index
// and append it to the stringbuilder
hashedString.Append(alphabetIndexLetter[tempIndex]);
}
}
}
return hashedString.ToString();
}
/*
*
* DecryptEncryptedMessage(int userOffset)
*
* This method decrypts an encrypted Caesar Cipher message. Method
* takes a user offset and decrypts the encrypted message.
*
*/
string DecryptEncryptedMessage(int userOffset)
{
// Variables
StringBuilder plaintextString = new StringBuilder();
string userEncryptedString = "";
int tempIndex = 0;
// Get the user's encrypted message
DisplayMenuBanner();
Write("\nPlease enter the message you want to decrypt >> ");
userEncryptedString = ReadLine();
// Iterate though the userEncryptedString and decrypt it.
foreach (char userChar in userEncryptedString)
{
// Check for a special char
if (specialCharArray.Contains(userChar))
{
// Append a space
plaintextString.Append(' ');
}
else
{
// Get the index from the user char
tempIndex = alphabetLetterIndex[userChar];
// Subtract the offset from the tempIndex
tempIndex = tempIndex - userOffset;
// If the tempIndex is less than zero
if (tempIndex < 0)
{
// Add 26 to tempIndex to loop back to the
// end of the alphabet
tempIndex = tempIndex + 90;
// Get the letter that corresponds with the new index
// and append it to the stringbuilder
plaintextString.Append(alphabetIndexLetter[tempIndex]);
}
else
{
// Get the letter that corresponds with the new index
// and append it to the stringbuilder
plaintextString.Append(alphabetIndexLetter[tempIndex]);
}
}
}
return plaintextString.ToString();
}
/*
*
* GenerateSpaceChar(char[] array)
*
* Generates a random special character to take
* place of a space in the user's plaintext
* string.
*
*/
char GenerateSpaceChar(char[] array)
{
// Variables
Random random = new Random();
return array[random.Next(array.Length)];
}
#endregion