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
use common::alphabet::{self, Alphabet};
use common::cipher::Cipher;
use common::keygen::cyclic_keystream;
use common::substitute;
#[rustfmt::skip]
const SUBSTITUTION_TABLE: [[usize; 26]; 13] = [
[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 13, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 13, 14, 11, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 13, 14, 15, 10, 11, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[17, 18, 19, 20, 21, 22, 23, 24, 25, 13, 14, 15, 16, 9, 10, 11, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8],
[18, 19, 20, 21, 22, 23, 24, 25, 13, 14, 15, 16, 17, 8, 9, 10, 11, 12, 0, 1, 2, 3, 4, 5, 6, 7],
[19, 20, 21, 22, 23, 24, 25, 13, 14, 15, 16, 17, 18, 7, 8, 9, 10, 11, 12, 0, 1, 2, 3, 4, 5, 6],
[20, 21, 22, 23, 24, 25, 13, 14, 15, 16, 17, 18, 19, 6, 7, 8, 9, 10, 11, 12, 0, 1, 2, 3, 4, 5],
[21, 22, 23, 24, 25, 13, 14, 15, 16, 17, 18, 19, 20, 5, 6, 7, 8, 9, 10, 11, 12, 0, 1, 2, 3, 4],
[22, 23, 24, 25, 13, 14, 15, 16, 17, 18, 19, 20, 21, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 1, 2, 3],
[23, 24, 25, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 1, 2],
[24, 25, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 1],
[25, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0],
];
pub struct Porta {
key: String,
}
impl Cipher for Porta {
type Key = String;
type Algorithm = Porta;
fn new(key: String) -> Porta {
if key.is_empty() {
panic!("The key is empty.");
}
if !alphabet::STANDARD.is_valid(&key) {
panic!("The key contains a non-alphabetic symbol.");
}
Porta { key }
}
fn encrypt(&self, message: &str) -> Result<String, &'static str> {
Ok(substitute::key_substitution(
message,
&cyclic_keystream(&self.key, message),
|mi, ki| SUBSTITUTION_TABLE[ki / 2][mi],
))
}
fn decrypt(&self, ciphertext: &str) -> Result<String, &'static str> {
self.encrypt(ciphertext)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt() {
let message = "attackatdawn";
let porta = Porta::new("lemon".into());
assert_eq!(porta.encrypt(message).unwrap(), "seauvppaxtel");
}
#[test]
fn decrypt() {
let ciphertext = "seauvppaxtel";
let porta = Porta::new("lemon".into());
assert_eq!(porta.decrypt(ciphertext).unwrap(), "attackatdawn");
}
#[test]
fn mixed_case() {
let message = "Attack at Dawn!";
let porta = Porta::new("lemon".into());
let ciphertext = porta.encrypt(message).unwrap();
let decrypted = porta.decrypt(&ciphertext).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn with_utf8() {
let message = "Peace 🗡️ Freedom and Liberty!";
let porta = Porta::new("utfeightisfun".into());
let ciphertext = porta.encrypt(message).unwrap();
let decrypted = porta.decrypt(&ciphertext).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn valid_key() {
Porta::new("LeMon".into());
}
#[test]
#[should_panic]
fn key_with_symbols() {
Porta::new("!em@n".into());
}
#[test]
#[should_panic]
fn key_with_whitespace() {
Porta::new("wow this key is a real lemon".into());
}
}