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
use common::cipher::Cipher;
use common::{alphabet, substitute};
use common::alphabet::Alphabet;
pub struct Autokey {
key: String,
}
impl Cipher for Autokey {
type Key = String;
type Algorithm = Autokey;
fn new(key: String) -> Result<Autokey, &'static str> {
if key.len() < 1 {
return Err("Invalid key. It must have at least one character.");
} else if !alphabet::STANDARD.is_valid(&key) {
return Err("Invalid key. Autokey keys cannot contain non-alphabetic symbols.");
}
Ok(Autokey { key: key })
}
fn encrypt(&self, message: &str) -> Result<String, &'static str> {
substitute::key_substitution(message, &mut self.encrypt_keystream(message), |mi, ki| {
alphabet::STANDARD.modulo((mi + ki) as isize)
})
}
fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str> {
self.autokey_decrypt(ciphertext)
}
}
impl Autokey {
fn autokey_decrypt(&self, ciphertext: &str) -> Result<String, &'static str> {
let mut plaintext = String::new();
let mut keystream: Vec<char> = self.key.clone().chars().collect();
for cc in ciphertext.chars() {
let pos = alphabet::STANDARD.find_position(cc);
match pos {
Some(ci) => {
if keystream.len() < 1 {
return Err(
"Keystream is not large enough for full substitution of message",
);
}
let kc = keystream[0];
if let Some(ki) = alphabet::STANDARD.find_position(kc) {
let si = alphabet::STANDARD.modulo(ci as isize - ki as isize);
let s = alphabet::STANDARD
.get_letter(si, cc.is_uppercase())
.unwrap();
plaintext.push(s);
keystream.push(s);
keystream.remove(0);
} else {
return Err("Keystream contains a non-alphabetic symbol.");
}
}
None => plaintext.push(cc),
}
}
Ok(plaintext)
}
fn encrypt_keystream(&self, message: &str) -> Vec<char> {
let scrubbed_msg = alphabet::STANDARD.scrub(message);
if self.key.len() >= scrubbed_msg.len() {
return self.key[0..scrubbed_msg.len()].chars().collect();
}
let mut keystream = self.key.clone();
keystream.push_str(&scrubbed_msg);
keystream[0..scrubbed_msg.len()].chars().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn with_utf8() {
let m = "Attack 🗡️ the east wall";
let a = Autokey::new(String::from("fort")).unwrap();
assert_eq!(m, a.decrypt(&a.encrypt(m).unwrap()).unwrap());
}
#[test]
fn simple_encrypt_decrypt_test() {
let message = "defend the east wall of the castle";
let v = Autokey::new(String::from("fortification")).unwrap();
let c_text = v.encrypt(message).unwrap();
let p_text = v.decrypt(&c_text).unwrap();
assert_eq!(message, p_text);
}
#[test]
fn decrypt_test() {
let ciphertext = "lxfopktmdcgn";
let v = Autokey::new(String::from("lemon")).unwrap();
assert_eq!("attackatdawn", v.decrypt(ciphertext).unwrap());
}
#[test]
fn larger_base_key() {
let message = "Hello";
let v = Autokey::new(String::from("fortification")).unwrap();
assert_eq!(vec!['f', 'o', 'r', 't', 'i'], v.encrypt_keystream(message));
}
#[test]
fn smaller_base_key() {
let message = "We are under seige";
let v = Autokey::new(String::from("lemon")).unwrap();
assert_eq!(
vec![
'l', 'e', 'm', 'o', 'n', 'W', 'e', 'a', 'r', 'e', 'u', 'n', 'd', 'e', 'r'
],
v.encrypt_keystream(message)
);
}
#[test]
fn valid_key() {
assert!(Autokey::new(String::from("LeMon")).is_ok());
}
#[test]
fn key_with_symbols() {
assert!(Autokey::new(String::from("!em@n")).is_err());
}
#[test]
fn key_with_whitespace() {
assert!(Autokey::new(String::from("wow this key is a real lemon")).is_err());
}
}