diff --git a/src/algo/feistel0.rs b/src/algo/feistel0.rs index 615e7b9..aa90f03 100644 --- a/src/algo/feistel0.rs +++ b/src/algo/feistel0.rs @@ -86,7 +86,7 @@ pub fn inner(input: u16, key: u16, verbose: bool) -> u16 { } /// Boilerplate KSA, returns the same given values everytime. -pub fn key_scheduler(key: u32) -> Vec { +pub fn key_scheduler(_key: u32) -> Vec { return vec![0xdead, 0xc0ff, 0xee5a] } @@ -130,37 +130,45 @@ fn test_decrypt() { let plaintext = 0xb00b00; let ciphertext = encrypt(plaintext, keys.clone(), true); let deciphertext = decrypt(ciphertext, keys.clone(), true); - assert_eq!(ciphertext, deciphertext); + assert_eq!(plaintext, deciphertext); } /// decrypt a given plaintext with a given key vec pub fn decrypt(ciphertext: u32, mut keys: Vec, verbose: bool) -> u32 { assert_eq!(keys.len(), ROUNDS as usize); - let mut lef: u16 = ((ciphertext & 0xffff0000) >> 16).try_into().expect("boom"); - let mut rig: u16 = ((ciphertext & 0x0000ffff) >> 0).try_into().expect("boom"); - if verbose { - println!("input:\n{:08x}\n{:04x}\n {:04x}", ciphertext, lef, rig); - } // swap lef rig + let mut lef: u16 = ((ciphertext & 0xffff0000) >> 16).try_into().expect("boom"); + let mut rig: u16 = ((ciphertext & 0x0000ffff) >> 0).try_into().expect("boom"); let tmp = rig; rig = lef; lef = tmp; + let mut ciphertext: u32 = 0; + ciphertext |= (lef as u32) << 16; + ciphertext |= rig as u32; + if verbose { + println!("input:\n{:08x}\n{:04x}\n {:04x}", ciphertext, lef, rig); + } // reverse keys keys.reverse(); // do the thing - encrypt(ciphertext, keys, verbose); + ciphertext = encrypt(ciphertext, keys, verbose); // swap lef rig back + let mut lef: u16 = ((ciphertext & 0xffff0000) >> 16).try_into().expect("boom"); + let mut rig: u16 = ((ciphertext & 0x0000ffff) >> 0).try_into().expect("boom"); let tmp = rig; rig = lef; lef = tmp; - let mut plaintext: u32 = 0; plaintext |= (lef as u32) << 16; plaintext |= rig as u32; + if verbose { + println!("input:\n{:08x}\n{:04x}\n {:04x}", plaintext, lef, rig); + } + if verbose { println!("returning plaintext:\n{:08x}", plaintext); } diff --git a/src/main.rs b/src/main.rs index 7246fb9..a5124c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -136,9 +136,11 @@ struct Feistel0SBOXArgs { #[derive(Args, Clone, Debug, PartialEq, Eq)] struct Feistel0Args{ #[clap(value_parser=maybe_hex::)] - plaintext: u32, + input: u32, #[clap(value_parser=maybe_hex::)] key: u32, + #[arg(short, long, default_value_t = false)] + decrypt: bool, } /*************************************************************************************************/ @@ -215,7 +217,13 @@ pub fn main() { } AlgoActions::Feistel0(alg_fe0_args) => { let keys = algo::feistel0::key_scheduler(alg_fe0_args.key); - let result: u32 = algo::feistel0::encrypt(alg_fe0_args.plaintext, keys, args.verbose); + let result: u32; + if alg_fe0_args.decrypt { + result = algo::feistel0::decrypt(alg_fe0_args.input, keys, args.verbose); + } + else { + result = algo::feistel0::encrypt(alg_fe0_args.input, keys, args.verbose); + } if args.machine { println!("{}", result) }