From 32737f5bef09270da4f28c94a434f57cb6e01ae7 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 8 Apr 2018 09:27:09 -0700 Subject: [PATCH] Continue WIP on s1c4 --- tests/cryptopals.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/cryptopals.rs b/tests/cryptopals.rs index ca84879..21e4284 100644 --- a/tests/cryptopals.rs +++ b/tests/cryptopals.rs @@ -65,9 +65,39 @@ fn s1c4() { Err(e) => panic!("failed to open the strings file: {}", e), }; let reader = BufReader::new(&f); + + let mut best_overall_key = 0u8; + let mut best_overall_score = f32::INFINITY; + let mut best_overall_output: Option = None; + for line in reader.lines() { + // Decode the string. let line = line.unwrap(); println!("{:?}", line); - let bytes = line.chars().hex_decoded(); + let decoded = line.chars().hex_decoded().collect::>(); + + // Do the decryption across all printable ASCII characters + let mut best_key = 0u8; + let mut best_score = f32::INFINITY; + let mut best_output: Option = None; + for key in 32u8..127 { + let decrypted = decoded.iter().byte_xor(key).map(char::from).collect::(); + let score = decrypted.chi2_freqs("en"); + if !score.is_nan() && score < best_score { + best_score = score; + best_output = Some(decrypted); + best_key = key; + println!("\tnew best {} -> {:7.3} {:?}", key, score, best_output); + } + } + + if best_output.is_some() && best_score < best_overall_score { + best_overall_key = best_key; + best_overall_score = best_score; + best_overall_output = best_output; + println!("-> new overall best: {}: {:?} -> {}", best_overall_key, best_overall_output, best_overall_score); + } } + + println!("Overall Best: {} {:7.3} {:?}", best_overall_key, best_overall_score, best_overall_output); }