-
Notifications
You must be signed in to change notification settings - Fork 0
/
Playfair.cs
309 lines (264 loc) · 10.8 KB
/
Playfair.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _17743_Mihajlo_Marjanovic_ZI_Projekat
{
class Playfair
{
private readonly char[,] _keyTable;
private readonly int _keyTableSize;
public Playfair(string key)
{
_keyTableSize = 5;
_keyTable = new char[_keyTableSize, _keyTableSize];
CreateKeyTable(key);
}
private void CreateKeyTable(string key)
{
// Create a set to store unique characters in the key
var keySet = new HashSet<char>();
// Add all unique characters in the key to the set
foreach (char c in key)
{
if (c != 'j' && !keySet.Contains(c))
{
keySet.Add(c);
}
}
// Add all remaining letters of the alphabet to the set
for (char c = 'a'; c <= 'z'; c++)
{
if (c != 'j' && !keySet.Contains(c))
{
keySet.Add(c);
}
}
// Add the characters in the set to the key table
int i = 0;
int j = 0;
foreach (char c in keySet)
{
_keyTable[i, j] = c;
j++;
if (j == _keyTableSize)
{
i++;
j = 0;
}
}
}
private (int, int) GetCharPos(char c)
{
if (c == 'j')
c = 'i';
for (int i = 0; i < _keyTableSize; i++)
{
for (int j = 0; j < _keyTableSize; j++)
{
if (_keyTable[i, j] == c)
{
return (i, j);
}
}
}
// Vracamo Tuple
return (-1, -1);
}
public string Encrypt(string plaintext)
{
// Remove spaces and non-alphabetic characters from the plaintext
plaintext = plaintext.ToLower().Replace(" ", "");
// Add an 'x' between consecutive identical characters
var modifiedPlaintext = new List<char>();
for (int i = 0; i < plaintext.Length; i++)
{
if (i < plaintext.Length - 1 && plaintext[i] == plaintext[i + 1])
{
modifiedPlaintext.Add(plaintext[i]);
modifiedPlaintext.Add('x');
}
else
{
modifiedPlaintext.Add(plaintext[i]);
}
}
// If the length of the modified plaintext is odd, add an 'x' at the end
if (modifiedPlaintext.Count % 2 == 1)
{
modifiedPlaintext.Add('x');
}
// Encrypt the modified plaintext
var ciphertext = new List<char>();
for (int i = 0; i < modifiedPlaintext.Count; i += 2)
{
var pos1 = GetCharPos(modifiedPlaintext[i]);
var pos2 = GetCharPos(modifiedPlaintext[i + 1]);
if (pos1.Item1 == pos2.Item1)
{
// Same row
ciphertext.Add(_keyTable[pos1.Item1, (pos1.Item2 + 1) % _keyTableSize]);
ciphertext.Add(_keyTable[pos2.Item1, (pos2.Item2 + 1) % _keyTableSize]);
}
else if (pos1.Item2 == pos2.Item2)
{
// Same column
ciphertext.Add(_keyTable[(pos1.Item1 + 1) % _keyTableSize, pos1.Item2]);
ciphertext.Add(_keyTable[(pos2.Item1 + 1) % _keyTableSize, pos2.Item2]);
}
else
{
// Different row and column
// Pravimo kvadrat - uzimamo suprotne koordinate jer su na suprotnoj dijagonali
ciphertext.Add(_keyTable[pos1.Item1, pos2.Item2]);
ciphertext.Add(_keyTable[pos2.Item1, pos1.Item2]);
}
}
return new string(ciphertext.ToArray());
}
public string Decrypt(string ciphertext)
{
// Decrypt the ciphertext
var plaintext = new List<char>();
for (int i = 0; i < ciphertext.Length; i += 2)
{
var pos1 = GetCharPos(ciphertext[i]);
var pos2 = GetCharPos(ciphertext[i + 1]);
if (pos1.Item1 == pos2.Item1)
{
// Same row
plaintext.Add(_keyTable[pos1.Item1, (pos1.Item2 - 1 + _keyTableSize) % _keyTableSize]);
plaintext.Add(_keyTable[pos2.Item1, (pos2.Item2 - 1 + _keyTableSize) % _keyTableSize]);
}
else if (pos1.Item2 == pos2.Item2)
{
// Same column
plaintext.Add(_keyTable[(pos1.Item1 - 1 + _keyTableSize) % _keyTableSize, pos1.Item2]);
plaintext.Add(_keyTable[(pos2.Item1 - 1 + _keyTableSize) % _keyTableSize, pos2.Item2]);
}
else
{
// Different row and column
plaintext.Add(_keyTable[pos1.Item1, pos2.Item2]);
plaintext.Add(_keyTable[pos2.Item1, pos1.Item2]);
}
}
// Remove any 'x' characters added during encryption
return new string(plaintext.ToArray()).Replace("x", "");
}
public void GenerateDemoInputFile()
{
string plaintext = "Ovo je demonstracija Playfair algoritma";
File.WriteAllText("playfair_demoInput.txt", plaintext);
}
public void EncryptFile(string inputFilePath, string outputFilePath)
{
// Read the plaintext from the input file
string plaintext = File.ReadAllText(inputFilePath);
// Encrypt the plaintext
string ciphertext = Encrypt(plaintext);
// Write the ciphertext to the output file
File.WriteAllText(outputFilePath, ciphertext);
}
public void DecryptFile(string inputFilePath, string outputFilePath)
{
// Read the ciphertext from the input file
string ciphertext = File.ReadAllText(inputFilePath);
// Decrypt the ciphertext
string plaintext = Decrypt(ciphertext);
// Write the plaintext to the output file
File.WriteAllText(outputFilePath, plaintext);
}
public void EncryptFileParallel(string inputFile, string outputFile, int numThreads)
{
// Read the input file and split it into chunks
string[] chunks = ReadAndSplitFile(inputFile, numThreads);
// Encrypt the chunks in parallel
object lockObject = new object();
var encryptedChunks = new List<string>();
Parallel.ForEach(chunks, new ParallelOptions { MaxDegreeOfParallelism = numThreads }, chunk =>
{
// Encrypt the plaintext
string ciphertext = Encrypt(chunk);
// Add the encrypted chunk to the list of encrypted chunks
lock (lockObject)
{
encryptedChunks.Add(string.Join("", ciphertext));
}
});
// Write the encrypted chunks to the output file
WriteToFile(outputFile, encryptedChunks);
}
private string[] ReadAndSplitFile(string file, int numChunks)
{
// Read the entire file into a string
string fileContent = File.ReadAllText(file);
// Calculate the size of each chunk
int chunkSize = fileContent.Length / numChunks;
// Split the file content into chunks
string[] chunks = new string[numChunks];
for (int i = 0; i < numChunks; i++)
{
int startIndex = i * chunkSize;
int endIndex = startIndex + chunkSize;
if (i == numChunks - 1)
{
// Last chunk may be smaller
endIndex = fileContent.Length;
}
chunks[i] = fileContent.Substring(startIndex, endIndex - startIndex);
}
return chunks;
}
private void WriteToFile(string file, List<string> chunks)
{
using (StreamWriter writer = new StreamWriter(file))
{
foreach (string chunk in chunks)
{
writer.Write(chunk);
}
}
}
}
}
//public byte[] ReadDataFromImage(string imageFilePath)
//{
// /*
// The method reads the contents of the image file into a byte array
// using the File.ReadAllBytes method.
// It then extracts the starting index of the pixel data in the byte array
// using the BitConverter.ToInt32 method. In a 24-bit BMP image,
// the starting index of the pixel data is stored at the 10th byte in the file.
// It also extracts the length of the pixel data using the BitConverter.ToInt32 method.
// In a 24-bit BMP image, the length of the pixel data is stored at the 34th byte in the file.
// It then uses the Array.Copy method to extract the pixel data from the byte array
// and store it in a new byte array.
// Finally, it returns the pixel data as a byte array.
// */
// // Read the image file into a byte array
// byte[] imageData = File.ReadAllBytes(imageFilePath);
// // Get the starting index of the pixel data in the byte array
// int pixelDataStartIndex = BitConverter.ToInt32(imageData, 10);
// // Get the length of the pixel data
// int pixelDataLength = BitConverter.ToInt32(imageData, 34);
// // Extract the pixel data from the byte array
// var pixelData = new byte[pixelDataLength];
// Array.Copy(imageData, pixelDataStartIndex, pixelData, 0, pixelDataLength);
// return pixelData;
//}
//public void CreateImageFromEncryptedData(string imageFilePath, byte[] encryptedData)
//{
// // Read the header and metadata from the original image file
// byte[] imageHeader = File.ReadAllBytes(imageFilePath).Take(54).ToArray();
// // Update the length of the pixel data in the header
// byte[] pixelDataLength = BitConverter.GetBytes(encryptedData.Length);
// Array.Copy(pixelDataLength, 0, imageHeader, 34, 4);
// // Concatenate the header, encrypted data, and padding to create the output image
// byte[] padding = new byte[encryptedData.Length % 4];
// byte[] outputImageData = imageHeader.Concat(encryptedData).Concat(padding).ToArray();
// // Write the output image data to a file
// File.WriteAllBytes(imageFilePath, outputImageData);
//}