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
use common::cipher::Cipher;
use common::{alphabet, keygen, morse};
use common::alphabet::Alphabet;
const TRIGRAPH_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> {
if key.len() < 1 || !alphabet::STANDARD.is_valid(&key) {
return Err("Invalid key. Keys cannot contain non-alphabetic symbols.");
}
let keyed_alphabet = keygen::keyed_alphabet(&key, alphabet::STANDARD, true)?;
Ok(FractionatedMorse {
keyed_alphabet: keyed_alphabet,
})
}
fn encrypt(&self, message: &str) -> Result<String, &'static str> {
let mut morse = FractionatedMorse::to_morse(message)?;
FractionatedMorse::pad(&mut morse);
FractionatedMorse::encrypt_sequence(&self.keyed_alphabet, &morse)
}
fn decrypt(&self, cipher_text: &str) -> Result<String, &'static str> {
let seq = FractionatedMorse::to_trigraphs(&self.keyed_alphabet, cipher_text)?;
FractionatedMorse::decrypt_sequence(&seq)
}
}
impl FractionatedMorse {
fn to_morse(message: &str) -> Result<String, &'static str> {
let mut morse = String::new();
for c in message.chars() {
match morse::encode_character(c) {
Some(sequence) => {
morse.push_str(sequence);
morse.push('|');
}
None => return Err("Unsupported character detected."),
}
}
morse.push('|');
Ok(morse)
}
fn encrypt_sequence(key: &str, morse: &str) -> Result<String, &'static str> {
let mut ciphertext = String::new();
for trigraph in morse.as_bytes().chunks(3) {
match TRIGRAPH_ALPHABET
.iter()
.position(|&t| t.as_bytes() == trigraph)
{
Some(pos) => ciphertext.push(key.chars().nth(pos).unwrap()),
None => return Err("Unknown trigraph sequence within the morse code."),
}
}
Ok(ciphertext)
}
fn to_trigraphs(key: &str, ciphertext: &str) -> Result<String, &'static str> {
let mut sequence = String::new();
for c in ciphertext.to_uppercase().chars() {
match key.chars().position(|k| k == c) {
Some(pos) => sequence.push_str(TRIGRAPH_ALPHABET[pos]),
None => return Err("Ciphertext cannot contain non-alphabetic symbols."),
}
}
Ok(sequence)
}
fn decrypt_sequence(sequence: &str) -> Result<String, &'static str> {
let mut plaintext = String::new();
let mut trigraphs = String::from(sequence);
while trigraphs.starts_with('|') {
trigraphs.remove(0);
}
for morse_seq in trigraphs.split('|') {
if morse_seq == "" {
break;
}
match morse::decode_sequence(morse_seq) {
Some(c) => plaintext.push_str(c),
None => return Err("Unknown morsecode sequence in trigraphs"),
}
}
Ok(plaintext)
}
fn pad(morse_sequence: &mut String) {
while morse_sequence.len() % 3 != 0 {
morse_sequence.push('.');
}
}
}
#[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 m = "Testingpunctuation!Willitwork?";
let f = FractionatedMorse::new(String::from("Punctuation")).unwrap();
assert_eq!(m.to_uppercase(), f.decrypt(&f.encrypt(m).unwrap()).unwrap());
}
#[test]
fn encrypt_no_key() {
assert!(FractionatedMorse::new(String::from("")).is_err());
}
#[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_decrypt() {
let m = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.,:\'\"!?@-;()=";
let f = FractionatedMorse::new(String::from("exhaustive")).unwrap();
assert_eq!(m.to_uppercase(), f.decrypt(&f.encrypt(m).unwrap()).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("test")).unwrap();
assert!(f.encrypt(message).is_err());
}
#[test]
fn decrypt_bad_message() {
let message = "badmessagefordecryption";
let f = FractionatedMorse::new(String::from("test")).unwrap();
assert!(f.decrypt(message).is_err());
}
}