forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere_cipher.cs
87 lines (71 loc) · 2.55 KB
/
vigenere_cipher.cs
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
using System;
using System.Text.RegularExpressions;
namespace vigenere_cipher
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Encrypt/Decrypt? [e/d]: ");
char option = Console.ReadKey().KeyChar;
// processes the user choice
if (option != 'e' && option != 'd')
{
Console.WriteLine("\nInvalid choice. Use 'e' or 'd'");
return;
}
Console.WriteLine("\n\nEnter MESSAGE: ");
string message = Console.ReadLine();
Console.WriteLine("\nEnter KEY: ");
string key = Console.ReadLine().ToUpper();
// makes sure the key contains only letters
if (!Regex.IsMatch(key, @"^[a-zA-Z]+$"))
{
Console.WriteLine("Illegal Character in KEY, use only alphabetic characters");
return;
}
Console.WriteLine("\n******************\n");
// calls the method Cipher depending on the user choice
switch (option)
{
case 'e':
Cipher(message, key, false);
break;
case 'd':
Cipher(message, key, true);
break;
}
}
static void Cipher(string msg, string key, bool reverse)
{
int skip = 0;
string crypted = "";
for (int i = 0; i < msg.Length; i++)
{
// if the character not are letter then skips it and
// don't en/dec it.
if (!char.IsLetter(msg[i]))
{
skip++;
crypted += msg[i];
}
else
{
// computes the shift
int shift = key[(i - skip) % key.Length] - 'A';
// modifying the shift depending on the option 'reverse'
shift = reverse ? -shift : shift;
// shifts the letter of the message
int c = msg[i] + shift;
char letterA = char.IsUpper(msg[i]) ? 'A' : 'a';
char letterZ = char.IsUpper(msg[i]) ? 'Z' : 'z';
// makes sure the en/dec character is a letter.
if (c < letterA) c += 26;
if (c > letterZ) c -= 26;
crypted += (char)c;
}
}
Console.WriteLine(crypted);
}
}
}