-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayfair-cipher.cs
222 lines (185 loc) · 7.4 KB
/
playfair-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
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
using System;
namespace Cryptography_Algorithms
{
class PlayfairCipher
{
static void Main(string[] args)
{
Console.WriteLine("Do you want to Encrypt or Decrypt?\n1. Encrypt\n2. Decrypt\n");
int option = Convert.ToInt32(Console.ReadLine());
switch (option)
{
case 1:
Console.WriteLine("\nEnter message to encrypt: ");
string encryptMessage = Console.ReadLine();
encryptMessage = encryptMessage.ToLower();
encryptMessage = encryptMessage.Replace(" ", "");
Console.WriteLine("\nWhich letter do you want to cut from the alphabet? ");
char missedEncKey = Convert.ToChar(Console.ReadLine());
char [,] alphabetMatrixEnc = CreateAlphabetMatrix(missedEncKey);
string encryptedMessage = Encrypt(encryptMessage.ToCharArray(), alphabetMatrixEnc);
Console.WriteLine("\nEncrypted message: " + encryptedMessage);
break;
case 2:
Console.WriteLine("\nEnter message to decrypt: ");
string decryptMessage = Console.ReadLine();
decryptMessage = decryptMessage.ToLower();
decryptMessage = decryptMessage.Replace(" ", "");
Console.WriteLine("\nWhich letter do you want to cut from the alphabet? ");
char missedDecKey = Convert.ToChar(Console.ReadLine());
char[,] alphabetMatrixDec = CreateAlphabetMatrix(missedDecKey);
string decryptedMessage = Decrypt(decryptMessage.ToCharArray(), alphabetMatrixDec);
Console.WriteLine("\nDecrypted message: " + decryptedMessage);
break;
default:
Console.WriteLine("Enter a valid value.");
break;
}
Console.ReadLine();
}
static char[,] CreateAlphabetMatrix(char key)
{
char[,] alphabetMatrix = new char[5, 5];
int num = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if(Alphabet.alphabet[num] == key)
{
num++;
}
alphabetMatrix[i, j] = Alphabet.alphabet[num];
num++;
}
}
return alphabetMatrix;
}
static string Encrypt(char[] secretMessage, char[,] alphabet)
{
string cipherText = "";
int[] indexes = new int[4];
for (int i = 0; i < secretMessage.Length; i += 2)
{
if (secretMessage.Length % 2 == 1 && i == secretMessage.Length-1)
{
FindKey(secretMessage[i], 'x', alphabet, indexes);
cipherText += SolveEncrypt(secretMessage[i], 'x', alphabet, indexes);
}
else
{
FindKey(secretMessage[i], secretMessage[i + 1], alphabet, indexes);
cipherText += SolveEncrypt(secretMessage[i], secretMessage[i + 1], alphabet, indexes);
}
}
return cipherText;
}
static string Decrypt(char[] secretMessage, char[,] alphabet)
{
string cipherText = "";
int[] indexes = new int[4];
for (int i = 0; i < secretMessage.Length; i += 2)
{
if (secretMessage.Length % 2 == 1 && i == secretMessage.Length - 1)
{
FindKey(secretMessage[i], 'x', alphabet, indexes);
cipherText += SolveDecrypt(secretMessage[i], 'x', alphabet, indexes);
}
else
{
FindKey(secretMessage[i], secretMessage[i + 1], alphabet, indexes);
cipherText += SolveDecrypt(secretMessage[i], secretMessage[i + 1], alphabet, indexes);
}
}
return cipherText;
}
static int[] FindKey(char char1, char char2, char[,] alphabet, int[] indexes)
{
for (int i = 0, x = 0; i < 5; i++, x++)
{
for (int j = 0, y = 0; j < 5; j++, y++)
{
if (alphabet[i, j] == char1)
{
indexes[0] = i;
indexes[1] = j;
}
if (alphabet[x, y] == char2)
{
indexes[2] = x;
indexes[3] = y;
}
}
}
return indexes;
}
static string SolveEncrypt(char char1, char char2, char[,] alphabet, int[] indexes)
{
int char1i = indexes[0], char1j = indexes[1], char2i = indexes[2], char2j = indexes[3];
if (char1j == char2j)
{
if (char1i + 1 <= 4)
char1 = alphabet[char1i + 1, char1j];
else
char1 = alphabet[0, char1j];
if (char2i + 1 <= 4)
char2 = alphabet[char2i + 1, char2j];
else
char2 = alphabet[0, char2j];
}
else if (char1i == char2i)
{
if (char1j + 1 <= 4)
char1 = alphabet[char1i, char1j + 1];
else
char1 = alphabet[char1i, 0];
if (char2j + 1 <= 4)
char2 = alphabet[char2i, char2j + 1];
else
char2 = alphabet[char2i, 0];
}
else
{
char1 = alphabet[char1i, char2j];
char2 = alphabet[char2i, char1j];
}
return (char1.ToString() + char2.ToString());
}
static string SolveDecrypt(char char1, char char2, char[,] alphabet, int[] indexes)
{
int char1i = indexes[0], char1j = indexes[1], char2i = indexes[2], char2j = indexes[3];
if (char1j == char2j)
{
if (char1i - 1 >= 0)
char1 = alphabet[char1i - 1, char1j];
else
char1 = alphabet[4, char1j];
if (char2i - 1 >= 0)
char2 = alphabet[char2i - 1, char2j];
else
char2 = alphabet[4, char2j];
}
else if (char1i == char2i)
{
if (char1j - 1 >= 0)
char1 = alphabet[char1i, char1j - 1];
else
char1 = alphabet[char1i, 4];
if (char2j + 1 >= 0)
char2 = alphabet[char2i, char2j - 1];
else
char2 = alphabet[char2i, 4];
}
else
{
char1 = alphabet[char1i, char2j];
char2 = alphabet[char2i, char1j];
}
return (char1.ToString() + char2.ToString());
}
}
static class Alphabet
{
public static char[] alphabet = new char[] {'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' };
}
}