encrypt decrypt test
This commit is contained in:
+38
@@ -2,6 +2,7 @@ mod lfsr;
|
||||
mod tea3;
|
||||
|
||||
use lfsr::Lfsr;
|
||||
use tea3::Tea3;
|
||||
|
||||
fn main() {
|
||||
let mut lfsr = Lfsr::new(4, vec![0, 3], vec![0x12, 0x34, 0x56, 0x78]);
|
||||
@@ -10,4 +11,41 @@ fn main() {
|
||||
let byte = lfsr.next();
|
||||
println!("{:02x} | state: {:?}", byte, lfsr.state());
|
||||
}
|
||||
|
||||
let key = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
let state = vec![0; 8];
|
||||
|
||||
let mut cipher = Tea3::new(key.clone(), state.clone());
|
||||
cipher.init();
|
||||
|
||||
println!("\nKeystream:");
|
||||
for _ in 0..10 {
|
||||
let byte = cipher.next_byte();
|
||||
println!("{:#04x}", byte);
|
||||
}
|
||||
|
||||
let plaintext = b"hello world";
|
||||
|
||||
let mut cipher_enc = Tea3::new(key.clone(), state.clone());
|
||||
cipher_enc.init();
|
||||
|
||||
let ciphertext: Vec<u8> = plaintext
|
||||
.iter()
|
||||
.map(|&b| b ^ cipher_enc.next_byte())
|
||||
.collect();
|
||||
|
||||
println!("\nPlaintext : {:?}", plaintext);
|
||||
println!("Ciphertext: {:?}", ciphertext);
|
||||
|
||||
let mut cipher_dec = Tea3::new(key, state);
|
||||
cipher_dec.init();
|
||||
|
||||
let decrypted: Vec<u8> = ciphertext
|
||||
.iter()
|
||||
.map(|&b| b ^ cipher_dec.next_byte())
|
||||
.collect();
|
||||
|
||||
println!("Decrypted : {:?}", decrypted);
|
||||
|
||||
assert_eq!(plaintext.to_vec(), decrypted);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user