-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt_text.rs
40 lines (31 loc) · 1.2 KB
/
encrypt_text.rs
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
/// Text encryption sample
pub fn encrypt_text() {
use cipher_magma::{CipherMode, MagmaStream};
let key = [0xab; 32];
println!("Key:\n{:x?}\n", key);
let mut magma = MagmaStream::new(key, CipherMode::CFB);
let source = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
Aenean ac sem leo. Morbi pretium neque eget felis finibus convallis. \
Praesent tristique rutrum odio at rhoncus. Duis non ligula ut diam tristique commodo. \
Phasellus vel ex nec leo pretium efficitur. Aliquam malesuada vestibulum magna. \
Quisque iaculis est et est volutpat posuere.";
println!("Source:\n{}\n", String::from_utf8(source.to_vec()).unwrap());
let encrypted = magma.encrypt(source);
println!("Encrypted:\n{:02x?}\n", encrypted);
let mut decrypted = magma.decrypt(&encrypted);
if magma.get_mode().has_padding() {
// remove padding bytes
decrypted.truncate(source.len());
}
assert_eq!(decrypted, source);
println!("Decrypted:\n{}\n", String::from_utf8(decrypted).unwrap());
println!("Completed.");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt_text_test() {
encrypt_text();
}
}