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
use common::cipher::Cipher;
pub struct Scytale {
height: usize,
}
impl Cipher for Scytale {
type Key = usize;
type Algorithm = Scytale;
fn new(key: usize) -> Result<Scytale, &'static str> {
if key == 0 {
Err("Invalid key, height cannot be zero.")
} else {
Ok(Scytale { height: key })
}
}
fn encrypt(&self, message: &str) -> Result<String, &'static str> {
if self.height >= message.chars().count() || self.height == 1 {
return Ok(message.to_string());
}
let width = (message.chars().count() as f64 / self.height as f64).ceil() as usize;
let mut table = vec![vec![' '; width]; self.height];
for (pos, element) in message.chars().enumerate() {
let col = pos % self.height;
let row = pos / self.height;
table[col][row] = element;
}
let mut ciphertext = String::new();
for col in 0..self.height {
for row in 0..width {
ciphertext.push(table[col][row]);
}
}
Ok(ciphertext.trim_right().to_string())
}
fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str> {
if self.height >= ciphertext.chars().count() || self.height == 1 {
return Ok(ciphertext.to_string());
}
let width = (ciphertext.chars().count() as f64 / self.height as f64).ceil() as usize;
let mut table = vec![vec![' '; width]; self.height];
for (pos, element) in ciphertext.chars().enumerate() {
let col = pos / width;
let row = pos % width;
table[col][row] = element;
}
let mut plaintext = String::new();
for row in 0..width {
for col in 0..self.height {
plaintext.push(table[col][row]);
}
}
Ok(plaintext.trim_right().to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_encrypt() {
let s = Scytale::new(6).unwrap();
assert_eq!("aatttdaacwkn", s.encrypt("attackatdawn").unwrap());
}
#[test]
fn simple_decrypt() {
let s = Scytale::new(6).unwrap();
assert_eq!("attackatdawn", s.decrypt("aatttdaacwkn").unwrap());
}
#[test]
fn padding_required() {
let s = Scytale::new(5).unwrap();
let m = "attackatdawn";
assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
}
#[test]
fn invalid_height() {
assert!(Scytale::new(0).is_err());
}
#[test]
fn with_utf8() {
let s = Scytale::new(5).unwrap();
let m = "Attack 🗡️ at once.";
assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
}
#[test]
fn with_spaces() {
let s = Scytale::new(5).unwrap();
let m = "Attack At Dawn comrades! ";
assert_eq!(
"Attack At Dawn comrades!",
s.decrypt(&s.encrypt(m).unwrap()).unwrap()
);
}
#[test]
fn longer_height() {
let s = Scytale::new(20).unwrap();
let m = "attackatdawn";
assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
}
#[test]
fn longer_msg() {
let s = Scytale::new(7).unwrap();
let m = concat!(
"We attack at dawn, not later when it is light, ",
"or at some strange time of the clock. Only at dawn. ",
"Why do we like to attack at dawn, actually, I don\'t ",
"get it. I hate getting up that early, it puts me in ",
"a bad mood. Can\'t we do it a bit later, say nine-thirty?"
);
assert_eq!(m, s.decrypt(&s.encrypt(m).unwrap()).unwrap());
}
}