forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere_cipher.cs
72 lines (61 loc) · 2.3 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
/* Vigenere Cipher in C# */
using System;
class Vigenere_Cipher
{
static String generateKey(String str, String key)
{
int x = str.Length;
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.Length == str.Length)
break;
key+=(key[i]);
}
return key;
}
static String cipherText(String str, String key)
{
String cipher_text="";
for (int i = 0; i < str.Length; i++)
{
int x = (str[i] + key[i]) %26;
x += 'A';
cipher_text+=(char)(x);
}
return cipher_text;
}
static String originalText(String cipher_text, String key)
{
String orig_text="";
for (int i = 0 ; i < cipher_text.Length && i < key.Length; i++)
{
int x = (cipher_text[i] - key[i] + 26) %26;
x += 'A';
orig_text+=(char)(x);
}
return orig_text;
}
public static void Main(String[] args)
{
String str;;
String word;
Console.WriteLine("Enter a string:");
str = Console.ReadLine();
Console.WriteLine("Enter a pattern word:");
word = Console.ReadLine();
String key = generateKey(str, word);
String cipher_text = cipherText(str, key);
Console.WriteLine("Ciphertext: " + cipher_text);
Console.WriteLine("Decrypted Text: " + originalText(cipher_text, key));
}
}
/* OUTPUT
Enter a string:
HACKINCODES
Enter a pattern word:
HACK
Ciphertext: OAEUCAWYULS
Decrypted Text: HACKINCODES
*/