false-bottom/examples/export.rs

48 lines
1.2 KiB
Rust

use false_bottom::{FB128, FBKey, FBAlgo, Encode};
fn main() {
// Cipher Initialization
let mut fb = FB128::init(2, 2).unwrap();
// Encryption
let msg1 = "This is a message".as_bytes();
let key1 = fb.add(msg1);
let msg2 = "This is another message".as_bytes();
let key2 = fb.add(msg2);
// Export as base64
let (cipher, keybase) = fb.export(); // Careful with the order
let key1_exp = key1.export();
// Or as raw bytes
let key2_exp = key2.to_bytes();
let inp_len = msg1.len() + msg2.len();
let out_len = fb.to_bytes().0.len();
let extra = out_len - inp_len;
let percent = extra as f32/out_len as f32 * 100_f32;
println!("Input: {inp_len}B \nOutput: {out_len}B");
println!("Extra: {extra}B ({percent}%)");
// Import from base64
let fb_new = FB128::import(&cipher, &keybase).unwrap();
let key1_imp = FBKey::import(&key1_exp).unwrap();
// Or as raw bytes
let key2_imp = FBKey::from_bytes(&key2_exp).unwrap();
// Decryption
let decr1 = fb_new.decrypt(&key1_imp).unwrap();
let decr2 = fb_new.decrypt(&key2_imp).unwrap();
// Display
println!("
CipherText: \n{cipher}\n
KeyBase: \n{keybase}\n
Key 1: {key1_exp}
Key 2: {key2_exp:?}
");
assert_eq!(msg1, decr1);
assert_eq!(msg2, decr2);
}