-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.cs
More file actions
331 lines (303 loc) · 7.55 KB
/
Encoder.cs
File metadata and controls
331 lines (303 loc) · 7.55 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System.Security.Cryptography;
namespace MuEncode;
class Encoder : IDisposable
{
private ErrorStream _errorStream;
/// <summary>
/// Dictionary with alphanumeric characters mapped to morse code
/// </summary>
private static Dictionary<char, string> _toMorse = new();
/// <summary>
/// Dictionary with morse code mapped to alphanumeric characters.
/// Reverse of <c>MorseF</c>
/// </summary>
private static Dictionary<string, char> _fromMorse = new();
public Encoder(ErrorStream e)
{
_errorStream = e;
}
static Encoder()
{
InitializeMorse();
}
public void Dispose() { }
/// <summary>
/// Adds data to the <c>MorseT</c> and <c>MorseF</c> dictionaries
/// </summary>
///
private static void InitializeMorse()
{
// Fill Morse dictionary
// SPACE
_toMorse.Add(' ', "/");
// LETTERS
_toMorse.Add('a', ".-");
_toMorse.Add('b', "-...");
_toMorse.Add('c', "-.-.");
_toMorse.Add('d', "-..");
_toMorse.Add('e', ".");
_toMorse.Add('f', "..-.");
_toMorse.Add('g', "--.");
_toMorse.Add('h', "....");
_toMorse.Add('i', "..");
_toMorse.Add('j', ".---");
_toMorse.Add('k', "-.-");
_toMorse.Add('l', ".-..");
_toMorse.Add('m', "--");
_toMorse.Add('n', "-.");
_toMorse.Add('o', "---");
_toMorse.Add('p', ".--.");
_toMorse.Add('q', "--.-");
_toMorse.Add('r', ".-.");
_toMorse.Add('s', "...");
_toMorse.Add('t', "-");
_toMorse.Add('u', "..-");
_toMorse.Add('v', "...-");
_toMorse.Add('w', ".--");
_toMorse.Add('x', "-..-");
_toMorse.Add('y', "-.--");
_toMorse.Add('z', "--..");
// NUMBERS
_toMorse.Add('1', ".----");
_toMorse.Add('2', "..---");
_toMorse.Add('3', "...--");
_toMorse.Add('4', "....-");
_toMorse.Add('5', ".....");
_toMorse.Add('6', "-....");
_toMorse.Add('7', "--...");
_toMorse.Add('8', "---..");
_toMorse.Add('9', "----.");
_toMorse.Add('0', "-----");
// PUNCTUATION
_toMorse.Add('.', ".-.-.-");
_toMorse.Add(',', "--..--");
_toMorse.Add('?', "..--..");
_toMorse.Add('\'', ".----.");
_toMorse.Add('!', "-.-.--");
_toMorse.Add('/', "-..-.");
_toMorse.Add('(', "-.--.");
_toMorse.Add(')', "-.--.-");
_toMorse.Add('&', ".-...");
_toMorse.Add(':', "---...");
_toMorse.Add(';', "-.-.-.");
_toMorse.Add('=', "-...-");
_toMorse.Add('+', ".-.-.");
_toMorse.Add('-', "-....-");
_toMorse.Add('_', "..--.-");
_toMorse.Add('\"', ".-..-.");
_toMorse.Add('$', "...-..-");
_toMorse.Add('@', ".--.-.");
// Fill reverse dictionary (MorseT) and AvailableChars list
// Add opposite key-value pair for each existing in MorseF
// Add encodable keys to list of total available keys
foreach (KeyValuePair<char, string> kvp in _toMorse)
{
_fromMorse.Add(kvp.Value, kvp.Key);
}
}
#region AES Methods
/// <summary>
/// Encrypts a string with AES encryption, given a key and IV.
/// </summary>
/// <param name="inStr">String to be encrypted</param>
/// <param name="key">Encryption Key</param>
/// <param name="IV">Initialization Vector</param>
/// <returns>A cipher represenetd as a byte[]</returns>
private static byte[] AesEncrypt(string inStr, byte[] key, byte[] IV)
{
using Aes a = Aes.Create();
a.Key = key;
a.IV = IV;
ICryptoTransform encryptor = a.CreateEncryptor(a.Key, a.IV);
using MemoryStream msEncrypt = new();
using CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write);
using (StreamWriter swEncrypt = new(csEncrypt))
swEncrypt.Write(inStr);
return msEncrypt.ToArray();
}
/// <summary>
/// Decrypts a string with AES encryption, given a key and IV.
/// </summary>
/// <param name="inText">Cipher to be decrypted</param>
/// <param name="key">Encryption Key</param>
/// <param name="IV">Initialization Vector</param>
/// <returns>A string of decrypted text</returns>
private static string AesDecrypt(byte[] inText, byte[] key, byte[] IV)
{
using Aes a = Aes.Create();
a.Key = key;
a.IV = IV;
ICryptoTransform decryptor = a.CreateDecryptor(a.Key, a.IV);
string output;
using MemoryStream msDecrypt = new(inText);
using CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read);
using (StreamReader srDecrypt = new(csDecrypt))
output = srDecrypt.ReadToEnd();
return output;
}
public string AES(string inStr, byte[] key, byte[] IV, bool encode)
{
if (encode)
{
return Convert.ToBase64String(AesEncrypt(inStr, key, IV));
}
else
{
return AesDecrypt(Convert.FromBase64String(inStr), key, IV);
}
}
#endregion
#region Morse Code Methods
public string CharShift(string inStr, bool encode)
{
char[] input = inStr.ToLower().ToCharArray();
int charIndex;
int s;
if (encode)
{
Random rand = new();
s = rand.Next(1, _toMorse.Count - 1);
}
else
{
try
{
s = int.Parse(inStr[0..2]) * -1;
}
catch
{
s = 0;
}
}
List<char> keys = new(_toMorse.Keys);
for (int i = 0; i < input.Length; i++)
{
if (keys.Contains(input[i]))
{
charIndex = keys.IndexOf(input[i]) + s;
if (keys.IndexOf(input[i]) + s >= keys.Count)
charIndex -= keys.Count;
if (keys.IndexOf(input[i]) + s < 0)
charIndex += keys.Count;
input[i] = keys[charIndex];
}
}
inStr = "";
foreach (char c in input)
{
inStr += c;
}
if (encode)
{
if (s >= 10)
return inStr.Insert(0, "" + s);
else
return inStr.Insert(0, "0" + s);
}
else
{
return inStr[2..];
}
}
public string MultiShift(string inStr, bool encode)
{
for (int i = 0; i < 5; i++)
{
inStr = CharShift(inStr, encode);
}
return inStr;
}
/// <summary>
/// Converts between Morse Code and plain text.
/// </summary>
/// <param name="encIn">Input</param>
/// <param name="encode">Determines the mode of the encoder. True encodes, false decodes</param>
/// <param name="error">Indicades whether or not an error has occured</param>
/// <returns></returns>
public string MorseCode(string encIn, bool encode, out bool error)
{
List<string> illegalChars = new();
encIn = encIn.ToLower();
string encOut = "";
error = false;
// ENCODE
if (encode)
{
for (int i = 0; i < encIn.Length; i++)
{
try
{
encOut += _toMorse[encIn[i]] + " ";
}
catch (KeyNotFoundException)
{
if (encIn[i] == '\n' && encIn[i - 1] == '\r')
{
encOut += "\r\n";
}
else if (encIn[i] != '\r')
{
// bypass this character, don't decode it
encOut += encIn[i] + " ";
illegalChars.Add("" + encIn[i]);
// let Main know that an error has occured
error = true;
}
}
}
}
// DECODE
else
{
encIn = encIn.Trim() + " ";
for (int i = 1; i < encIn.Length; i++)
{
if (encIn[i] == '\r' && encIn[i - 1] != ' ')
{
encIn = encIn.Insert(i, " ");
i++;
}
}
while (encIn.Length > 1)
{
// current morse code sequence, one character long.
string currentChar;
if (encIn[0] != '\r')
{
currentChar = encIn[..encIn.IndexOf(' ')];
try
{
encOut += _fromMorse[currentChar];
}
catch (KeyNotFoundException)
{
if (currentChar == "\r\n")
{
encOut += "\r\n";
}
else
{
encOut += currentChar + " ";
illegalChars.Add(currentChar);
// bypass this character, don't decode it
error = true;
}
}
encIn = encIn.Remove(0, encIn.IndexOf(' ') + 1);
}
else
{
encOut += "\r\n";
encIn = encIn.Remove(0, encIn.IndexOf('\n') + 1);
}
}
}
if (error == true) _errorStream.Write(new IllegalCharacterError(illegalChars));
return encOut;
}
public string MorseCode(string encIn, bool encode)
{
return MorseCode(encIn, encode, out _);
}
#endregion
}