59 lines
1.4 KiB
Rust
59 lines
1.4 KiB
Rust
mod lfsr;
|
|
mod period;
|
|
mod tea3;
|
|
|
|
use lfsr::Lfsr;
|
|
use period::{period_bruteforce, period_floyd, period_paper};
|
|
use tea3::Tea3;
|
|
|
|
fn main() {
|
|
let mut lfsr = Lfsr::new(4, vec![0, 3], vec![0x12, 0x34, 0x56, 0x78]);
|
|
|
|
for _ in 0..16 {
|
|
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);
|
|
|
|
println!(
|
|
"\nPaper period: {}",
|
|
// period_paper(vec![0xc2, 0xc2, 0xc2, 0xc2, 0xc2])
|
|
// period_paper(vec![0xa3, 0xe1, 0x6f, 0x58, 0x05])
|
|
period_paper(vec![0xe3, 0x61, 0x62, 0x00, 0x00])
|
|
);
|
|
}
|