From d6578f1357f7ae10feb9e4a4fae1a1953739557a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 15 Mar 2015 16:22:13 -0400 Subject: if we disallow control characters, we get much more reasonable results --- data/6.out.txt | Bin 2876 -> 2876 bytes src/lib.rs | 3 +++ tests/lib.rs | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/data/6.out.txt b/data/6.out.txt index cd20bce..14283df 100644 Binary files a/data/6.out.txt and b/data/6.out.txt differ diff --git a/src/lib.rs b/src/lib.rs index e7f695d..9506289 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -191,6 +191,9 @@ fn crack_single_byte_xor_with_confidence (input: &[u8]) -> (u8, f64) { if !decrypted.is_ascii() { continue; } + if decrypted.iter().any(|&c| c != b'\n' && (c < 0x20 || c > 0x7E)) { + continue; + } let lowercase = decrypted.to_ascii_lowercase(); let mut frequencies = [0; 26]; let mut total_frequency = 0; diff --git a/tests/lib.rs b/tests/lib.rs index ac13fb2..b537bd0 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -36,7 +36,7 @@ fn problem_4 () { .lines() .map(|line| line.unwrap().from_hex().unwrap()) .collect::>>(); - assert_eq!(matasano::find_single_byte_xor_encrypted_string(&possibles[..]), b"nOW\0THAT\0THE\0PARTY\0IS\0JUMPING*"); + assert_eq!(matasano::find_single_byte_xor_encrypted_string(&possibles[..]), b"Now that the party is jumping\n"); } #[test] -- cgit v1.2.3-54-g00ecf