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
use common::cipher::Cipher;
use common::alphabet;
use common::keygen;
use common::morse_alphabet;
const FRAC_MORSE_ALPHABET: [&str; 26] = ["...", "..-", "..|", ".-.", ".--", ".-|", ".|.", ".|-",
".||", "-..", "-.-", "-.|", "--.", "---", "--|", "-|.", "-|-", "-||", "|..", "|.-", "|.|", "|-.",
"|--", "|-|", "||.", "||-"];
pub struct FractionatedMorse {
keyed_alphabet: String,
}
impl Cipher for FractionatedMorse {
type Key = String;
type Algorithm = FractionatedMorse;
fn new(key: String) -> Result<FractionatedMorse, &'static str> {
for c in key.chars() {
if alphabet::find_position(c).is_none() {
return Err("Invalid key. Fractionated Morse keys cannot contain non-alphabetic symbols.");
}
}
let keyed_alphabet = keygen::keyed_alphabet(&key, false)?;
Ok(FractionatedMorse { keyed_alphabet: keyed_alphabet })
}
fn encrypt(&self, message: &str) -> Result<String, &'static str> {
let morse = FractionatedMorse::to_morse(message.to_string())?;
let ciphertext = FractionatedMorse::to_ciphertext(&self.keyed_alphabet, morse)?;
Ok(ciphertext)
}
fn decrypt(&self, cipher_text: &str) -> Result<String, &'static str> {
let frac_morse = FractionatedMorse::to_fractionated_morse(&self.keyed_alphabet, cipher_text.to_string())?;
let plaintext = FractionatedMorse::to_plaintext(frac_morse)?;
Ok(plaintext)
}
}
impl FractionatedMorse {
fn to_morse(message: String) -> Result<String, &'static str> {
let mut morse = String::new();
for c in message.chars() {
if let Some(m) = morse_alphabet::to_morse(c) {
morse.extend(m.chars());
morse.push('|');
} else {
return Err("Invalid message. Please strip any whitespace or unsupported symbols.")
}
}
morse.push('|');
Ok(morse)
}
fn to_plaintext(mut morse: String) -> Result<String, &'static str> {
let mut plaintext = String::new();
while morse.starts_with('|') {
morse.remove(0);
}
for morse_chr in morse.split('|') {
if morse_chr == "" {
break;
}
if let Some(c) = morse_alphabet::to_plaintext(morse_chr) {
plaintext.push(c);
} else {
return Err("Invalid Fractionated Morse message. Unknown Morse character found.")
}
}
Ok(plaintext)
}
fn to_fractionated_morse(key: &String, message: String) -> Result<String, &'static str> {
let mut frac_morse = String::new();
for c in message.to_lowercase().chars() {
if let Some(pos) = key.chars().position(|a| a == c) {
frac_morse.extend(FRAC_MORSE_ALPHABET[pos].chars());
} else {
return Err("Invalid message. Please strip any whitespace or non-alphabetic symbols.")
}
}
Ok(frac_morse)
}
fn to_ciphertext(key: &str, morse: String) -> Result<String, &'static str> {
let mut ciphertext = String::new();
let fractionated_morse = FractionatedMorse::pad_morse_message(morse);
for trigraph in fractionated_morse.as_bytes().chunks(3) {
if let Some(pos) = FRAC_MORSE_ALPHABET.iter().position(|&t| t.as_bytes() == trigraph) {
ciphertext.push(key.chars().nth(pos).unwrap());
} else {
return Err("Unknown Fractionated Morse trigraph found.")
}
}
Ok(ciphertext)
}
fn pad_morse_message(morse: String) -> String {
let mut fractionated_morse = morse;
while fractionated_morse.len() % 3 != 0 {
fractionated_morse.push('.');
}
fractionated_morse
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt_test() {
let message = "attackatdawn";
let f = FractionatedMorse::new(String::from("key")).unwrap();
assert_eq!("cpsujiswhsspg", f.encrypt(message).unwrap());
}
#[test]
fn decrypt_test() {
let message = "cpsujiswhsspg";
let f = FractionatedMorse::new(String::from("key")).unwrap();
assert_eq!("attackatdawn", f.decrypt(message).unwrap());
}
#[test]
fn encrypt_mixed_case() {
let message = "AttackAtDawn";
let f = FractionatedMorse::new(String::from("OranGE")).unwrap();
assert_eq!("eptvihtxfttpd", f.encrypt(message).unwrap());
}
#[test]
fn decrypt_mixed_case() {
let message = "EPtvihtXFttPD";
let f = FractionatedMorse::new(String::from("OranGE")).unwrap();
assert_eq!("attackatdawn", f.decrypt(message).unwrap());
}
#[test]
fn encrypt_punctuation() {
let message = "Testingpunctuation!Willitwork?";
let f = FractionatedMorse::new(String::from("Punctuation")).unwrap();
assert_eq!("kqoqvwigqlocxurxnhvvekjncidqxtwkfeqgb", f.encrypt(message).unwrap());
}
#[test]
fn encrypt_no_key() {
let message = "defendtheeastwall";
let f = FractionatedMorse::new(String::from("")).unwrap();
assert_eq!("jubgvvhscgtshtppjtcs", f.encrypt(message).unwrap());
}
#[test]
fn encrypt_long_key() {
let message = "defendtheeastwall";
let f = FractionatedMorse::new(String::from("nnhhyqzabguuxwdrvvctspefmjoklii")).unwrap();
assert_eq!("xmhbjjgeybfegfttxfye", f.encrypt(message).unwrap());
}
#[test]
fn exhaustive_encrypt() {
let message = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.,:\'\"!?@-;()=";
let encrypted = "sbiaqtndfnhhulsailijuicothksekjblurhsbiaqtndfnhhulsailijuicot\
hksekjblurhujxjejesehbhfhghgdgjacrxlfhufoxiajxbociescaqfqflem";
let f = FractionatedMorse::new(String::from("exhaustive")).unwrap();
assert_eq!(encrypted, f.encrypt(message).unwrap());
}
#[test]
fn exhaustive_decrypt() {
let encrypted = "sbiaqtndfnhhulsailijuicothksekjblurhsbiaqtndfnhhulsailijuicot\
hksekjblurhujxjejesehbhfhghgdgjacrxlfhufoxiajxbociescaqfqflem";
let decrypted = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890.,:\'\"!?@-;()=";
let f = FractionatedMorse::new(String::from("exhaustive")).unwrap();
assert_eq!(decrypted, f.decrypt(encrypted).unwrap());
}
#[test]
fn bad_key() {
assert!(FractionatedMorse::new(String::from("bad key")).is_err());
}
#[test]
fn encrypt_bad_message() {
let message = "Spaces are not supported.";
let f = FractionatedMorse::new(String::from("")).unwrap();
assert!(f.encrypt(message).is_err());
}
#[test]
fn decrypt_bad_message() {
let message = "badmessagefordecryption";
let f = FractionatedMorse::new(String::from("")).unwrap();
assert!(f.decrypt(message).is_err());
}
}