-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathvernam.rs
More file actions
244 lines (215 loc) · 6.57 KB
/
vernam.rs
File metadata and controls
244 lines (215 loc) · 6.57 KB
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
//! Vernam Cipher
//!
//! The Vernam cipher is a symmetric stream cipher where plaintext is combined
//! with a random or pseudorandom stream of data (the key) of the same length.
//! This implementation uses the alphabet A-Z with modular arithmetic.
//!
//! # Algorithm
//!
//! For encryption: C = (P + K) mod 26
//! For decryption: P = (C - K) mod 26
//!
//! Where P is plaintext, K is key, and C is ciphertext (all converted to 0-25 range)
/// Encrypts a plaintext string using the Vernam cipher.
///
/// The function converts all input to uppercase and works only with letters A-Z.
/// The key is repeated cyclically if it's shorter than the plaintext.
///
/// # Arguments
///
/// * `plaintext` - The text to encrypt (will be converted to uppercase)
/// * `key` - The encryption key (will be converted to uppercase, must not be empty)
///
/// # Returns
///
/// The encrypted ciphertext as an uppercase string
///
/// # Panics
///
/// Panics if the key is empty
///
/// # Example
///
/// ```
/// use the_algorithms_rust::ciphers::vernam_encrypt;
///
/// let ciphertext = vernam_encrypt("HELLO", "KEY");
/// assert_eq!(ciphertext, "RIJVS");
/// ```
pub fn vernam_encrypt(plaintext: &str, key: &str) -> String {
assert!(!key.is_empty(), "Key cannot be empty");
let plaintext = plaintext.to_uppercase();
let key = key.to_uppercase();
let plaintext_bytes: Vec<u8> = plaintext
.chars()
.filter(|c| c.is_ascii_alphabetic())
.map(|c| (c as u8) - b'A')
.collect();
let key_bytes: Vec<u8> = key
.chars()
.filter(|c| c.is_ascii_alphabetic())
.map(|c| (c as u8) - b'A')
.collect();
assert!(
!key_bytes.is_empty(),
"Key must contain at least one letter"
);
plaintext_bytes
.iter()
.enumerate()
.map(|(i, &p)| {
let k = key_bytes[i % key_bytes.len()];
let encrypted = (p + k) % 26;
(encrypted + b'A') as char
})
.collect()
}
/// Decrypts a ciphertext string using the Vernam cipher.
///
/// The function converts all input to uppercase and works only with letters A-Z.
/// The key is repeated cyclically if it's shorter than the ciphertext.
///
/// # Arguments
///
/// * `ciphertext` - The text to decrypt (will be converted to uppercase)
/// * `key` - The decryption key (will be converted to uppercase, must not be empty)
///
/// # Returns
///
/// The decrypted plaintext as an uppercase string
///
/// # Panics
///
/// Panics if the key is empty
///
/// # Example
///
/// ```
/// use the_algorithms_rust::ciphers::vernam_decrypt;
///
/// let plaintext = vernam_decrypt("RIJVS", "KEY");
/// assert_eq!(plaintext, "HELLO");
/// ```
pub fn vernam_decrypt(ciphertext: &str, key: &str) -> String {
assert!(!key.is_empty(), "Key cannot be empty");
let ciphertext = ciphertext.to_uppercase();
let key = key.to_uppercase();
let ciphertext_bytes: Vec<u8> = ciphertext
.chars()
.filter(|c| c.is_ascii_alphabetic())
.map(|c| (c as u8) - b'A')
.collect();
let key_bytes: Vec<u8> = key
.chars()
.filter(|c| c.is_ascii_alphabetic())
.map(|c| (c as u8) - b'A')
.collect();
assert!(
!key_bytes.is_empty(),
"Key must contain at least one letter"
);
ciphertext_bytes
.iter()
.enumerate()
.map(|(i, &c)| {
let k = key_bytes[i % key_bytes.len()];
// Add 26 before modulo to handle negative numbers properly
let decrypted = (c + 26 - k) % 26;
(decrypted + b'A') as char
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encrypt_basic() {
assert_eq!(vernam_encrypt("HELLO", "KEY"), "RIJVS");
}
#[test]
fn test_decrypt_basic() {
assert_eq!(vernam_decrypt("RIJVS", "KEY"), "HELLO");
}
#[test]
fn test_encrypt_decrypt_roundtrip() {
let plaintext = "HELLO";
let key = "KEY";
let encrypted = vernam_encrypt(plaintext, key);
let decrypted = vernam_decrypt(&encrypted, key);
assert_eq!(decrypted, plaintext);
}
#[test]
fn test_encrypt_decrypt_long_text() {
let plaintext = "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG";
let key = "SECRET";
let encrypted = vernam_encrypt(plaintext, key);
let decrypted = vernam_decrypt(&encrypted, key);
assert_eq!(decrypted, plaintext);
}
#[test]
fn test_lowercase_input() {
// Should convert to uppercase
assert_eq!(vernam_encrypt("hello", "key"), "RIJVS");
assert_eq!(vernam_decrypt("rijvs", "key"), "HELLO");
}
#[test]
fn test_mixed_case_input() {
assert_eq!(vernam_encrypt("HeLLo", "KeY"), "RIJVS");
assert_eq!(vernam_decrypt("RiJvS", "kEy"), "HELLO");
}
#[test]
fn test_single_character() {
assert_eq!(vernam_encrypt("A", "B"), "B");
assert_eq!(vernam_decrypt("B", "B"), "A");
}
#[test]
fn test_key_wrapping() {
// Key shorter than plaintext, should wrap around
let encrypted = vernam_encrypt("AAAA", "BC");
assert_eq!(encrypted, "BCBC");
let decrypted = vernam_decrypt(&encrypted, "BC");
assert_eq!(decrypted, "AAAA");
}
#[test]
fn test_alphabet_boundary() {
// Test wrapping at alphabet boundaries
assert_eq!(vernam_encrypt("Z", "B"), "A"); // 25 + 1 = 26 -> 0
assert_eq!(vernam_decrypt("A", "B"), "Z"); // 0 - 1 = -1 -> 25
}
#[test]
fn test_same_key_as_plaintext() {
let text = "HELLO";
let encrypted = vernam_encrypt(text, text);
assert_eq!(encrypted, "OIWWC");
}
#[test]
fn test_with_spaces_and_numbers() {
// Non-alphabetic characters should be filtered out
let encrypted = vernam_encrypt("HELLO 123 WORLD", "KEY");
let expected = vernam_encrypt("HELLOWORLD", "KEY");
assert_eq!(encrypted, expected);
}
#[test]
#[should_panic(expected = "Key cannot be empty")]
fn test_empty_key_encrypt() {
vernam_encrypt("HELLO", "");
}
#[test]
#[should_panic(expected = "Key cannot be empty")]
fn test_empty_key_decrypt() {
vernam_decrypt("HELLO", "");
}
#[test]
#[should_panic(expected = "Key must contain at least one letter")]
fn test_key_with_only_numbers() {
vernam_encrypt("HELLO", "12345");
}
#[test]
fn test_empty_plaintext() {
assert_eq!(vernam_encrypt("", "KEY"), "");
}
#[test]
fn test_plaintext_with_only_numbers() {
assert_eq!(vernam_encrypt("12345", "KEY"), "");
}
}