· 4 years ago · Jan 23, 2021, 11:18 AM
1using System;
2using System.Linq;
3using System.Text.RegularExpressions;
4namespace Playfair
5{
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 string SECRET_KEY = "KGYLOPNU";
11 Console.WriteLine("Wprowadź tekst do zaszyfrowania:");
12 Console.Write("\n");
13 string sPlainText = Console.ReadLine().ToLower();
14 Console.Write("\n");
15 string sCipherText = PlayfairEncryption(sPlainText, SECRET_KEY);
16 Console.WriteLine("Zaszyfrowany tekst: " + sCipherText);
17 Console.Write("\n");
18 string sDecryptedPlainText = PlayfairDecryption(sCipherText, SECRET_KEY);
19 Console.WriteLine("Rozszyfrowany tekst: " + sDecryptedPlainText);
20 Console.Read();
21 }
22 static string PlayfairEncryption(string sInput, string sKey)
23 {
24 string sEncryptedText = string.Empty;
25 if ((sKey != "") && (sInput != ""))
26 {
27 sKey = sKey.ToLower();
28 string sGrid = null;
29 string sAlpha = "abcdefghiklmnopqrstuvwxyz";
30 sInput = sInput.ToLower();
31 string sOutput = "";
32 Regex rgx = new Regex("[^a-z-]");
33 sKey = rgx.Replace(sKey, "");
34 sKey = sKey.Replace('j', 'i');
35 for (int i = 0; i < sKey.Length; i++)
36 {
37 if ((sGrid == null) || (!sGrid.Contains(sKey[i])))
38 {
39 sGrid += sKey[i];
40 }
41 }
42
43 for (int i = 0; i < sAlpha.Length; i++)
44 {
45 if (!sGrid.Contains(sAlpha[i]))
46 {
47 sGrid += sAlpha[i];
48 }
49 }
50 sInput = rgx.Replace(sInput, "");
51 sInput = sInput.Replace('j', 'i');
52 for (int i = 0; i < sInput.Length; i += 2)
53 {
54 if (((i + 1) < sInput.Length) && (sInput[i] == sInput[i + 1]))
55 {
56 sInput = sInput.Insert(i + 1, "x");
57 }
58 }
59 if ((sInput.Length % 2) > 0)
60 {
61 sInput += "x";
62 }
63 int iTemp = 0;
64 do
65 {
66 int iPosA = sGrid.IndexOf(sInput[iTemp]);
67 int iPosB = sGrid.IndexOf(sInput[iTemp + 1]);
68 int iRowA = iPosA / 5;
69 int iColA = iPosA % 5;
70 int iRowB = iPosB / 5;
71 int iColB = iPosB % 5;
72
73 if (iColA == iColB)
74 {
75 iPosA += 5;
76 iPosB += 5;
77 }
78 else
79 {
80 if (iRowA == iRowB)
81 {
82 if (iColA == 4)
83 {
84 iPosA -= 4;
85 }
86 else
87 {
88 iPosA += 1;
89 }
90 if (iColB == 4)
91 {
92 iPosB -= 4;
93 }
94
95 else
96 {
97 iPosB += 1;
98 }
99 }
100 else
101 {
102 if (iRowA < iRowB)
103 {
104 iPosA -= iColA - iColB;
105 iPosB += iColA - iColB;
106 }
107 else
108 {
109 iPosA += iColB - iColA;
110 iPosB -= iColB - iColA;
111 }
112 }
113 }
114 if (iPosA >= sGrid.Length)
115 {
116 iPosA = 0 + (iPosA - sGrid.Length);
117 }
118 if (iPosB >= sGrid.Length)
119 {
120 iPosB = 0 + (iPosB - sGrid.Length);
121 }
122 if (iPosA < 0)
123 {
124 iPosA = sGrid.Length + iPosA;
125 }
126 if (iPosB < 0)
127 {
128 iPosB = sGrid.Length + iPosB;
129 }
130 sOutput += sGrid[iPosA].ToString() + sGrid[iPosB].ToString();
131 iTemp += 2;
132 } while (iTemp < sInput.Length);
133 sEncryptedText = sOutput;
134
135 }
136 return sEncryptedText;
137 }
138 static string PlayfairDecryption(string sCipherText, string sKey)
139 {
140 sKey = sKey.ToLower();
141 string sGrid = null;
142 string sAlpha = "abcdefghiklmnopqrstuvwxyz";
143 string sInput = sCipherText.ToLower();
144 string sOutput = "";
145 sKey = sKey.Replace('j', 'i');
146 for (int i = 0; i < sKey.Length; i++)
147 {
148 if ((sGrid == null) || (!sGrid.Contains(sKey[i])))
149 {
150 sGrid += sKey[i];
151 }
152 }
153 for (int i = 0; i < sAlpha.Length; i++)
154 {
155 if (!sGrid.Contains(sAlpha[i]))
156 {
157 sGrid += sAlpha[i];
158 }
159 }
160 int iTemp = 0;
161 do
162 {
163 int iPosA = sGrid.IndexOf(sInput[iTemp]);
164 int iPosB = sGrid.IndexOf(sInput[iTemp + 1]);
165 int iRowA = iPosA / 5;
166 int iColA = iPosA % 5;
167 int iRowB = iPosB / 5;
168 int iColB = iPosB % 5;
169 if (iColA == iColB)
170 {
171 iPosA -= 5;
172 iPosB -= 5;
173 }
174 else
175 {
176 if (iRowA == iRowB)
177 {
178 if (iColA == 0)
179 {
180 iPosA += 4;
181 }
182 else
183 {
184 iPosA -= 1;
185 }
186 if (iColB == 0)
187 {
188 iPosB += 4;
189 }
190 else
191 {
192 iPosB -= 1;
193 }
194 }
195 else
196 {
197 if (iRowA < iRowB)
198 {
199 iPosA -= iColA - iColB;
200 iPosB += iColA - iColB;
201 }
202 else
203 {
204 iPosA += iColB - iColA;
205 iPosB -= iColB - iColA;
206 }
207 }
208 }
209 if (iPosA > sGrid.Length)
210 {
211 iPosA = 0 + (iPosA - sGrid.Length);
212 }
213 if (iPosB > sGrid.Length)
214 {
215 iPosB = 0 + (iPosB - sGrid.Length);
216 }
217 if (iPosA < 0)
218 {
219 iPosA = sGrid.Length + iPosA;
220 }
221 if (iPosB < 0)
222 {
223 iPosB = sGrid.Length + iPosB;
224 }
225 sOutput += sGrid[iPosA].ToString() + sGrid[iPosB].ToString();
226 iTemp += 2;
227 } while (iTemp < sInput.Length);
228 return sOutput;
229 }
230 }
231}