From 5b5c47fe15d312e89ebe71ff5329c6fc9fd1b9d4 Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sat, 10 Sep 2016 15:59:40 +0200 Subject: [PATCH] solve Challenge 3 of Set 1 --- set1/xor_cipher/Cargo.lock | 107 ++++++++++++++++++++++++++++++++++++ set1/xor_cipher/Cargo.toml | 9 +++ set1/xor_cipher/src/main.rs | 49 +++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 set1/xor_cipher/Cargo.lock create mode 100644 set1/xor_cipher/Cargo.toml create mode 100644 set1/xor_cipher/src/main.rs diff --git a/set1/xor_cipher/Cargo.lock b/set1/xor_cipher/Cargo.lock new file mode 100644 index 0000000..a95b77c --- /dev/null +++ b/set1/xor_cipher/Cargo.lock @@ -0,0 +1,107 @@ +[root] +name = "xor_cipher" +version = "0.1.0" +dependencies = [ + "env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "env_logger" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.75 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rustc-serialize" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/set1/xor_cipher/Cargo.toml b/set1/xor_cipher/Cargo.toml new file mode 100644 index 0000000..a9e7fc3 --- /dev/null +++ b/set1/xor_cipher/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "xor_cipher" +version = "0.1.0" +authors = ["Thomas Lindner "] + +[dependencies] +log = "0.3.6" +env_logger = "0.3.4" +rustc-serialize = "0.3" diff --git a/set1/xor_cipher/src/main.rs b/set1/xor_cipher/src/main.rs new file mode 100644 index 0000000..eb9e1f7 --- /dev/null +++ b/set1/xor_cipher/src/main.rs @@ -0,0 +1,49 @@ +#[macro_use] +extern crate log; +extern crate env_logger; +extern crate rustc_serialize; + +use rustc_serialize::hex::FromHex; +use std::io::{BufRead, stdin}; +use std::iter::repeat; + +fn score(plaintext: &Vec) -> u32 { + let ranking = [24, 7, 15, 17, 26, 11, 10, 19, 22, 4, 5, 16, 13, + 21, 23, 8, 2, 18, 20, 25, 14, 6, 12, 3, 9, 1]; + let mut score = 0; + + for ch in plaintext.iter() { + if ch >= &97 && ch <= &122 { + score += ranking[(ch - 97) as usize]; + } + } + score +} + +fn main() { + env_logger::init().unwrap(); + let stdin = stdin(); + + for line in stdin.lock().lines().filter_map(|x| x.ok()) { + match line.from_hex() { + Ok(ciphertext) => { + let mut maxscore = 0; + let mut decrypted: Vec = vec![]; + + for key in 0..0xff { + let plaintext: Vec = ciphertext.iter().zip(repeat(key)).map(|(c, k)| c ^ k).collect(); + let s = score(&plaintext); + if s > maxscore { + maxscore = s; + decrypted = plaintext; + } + } + match String::from_utf8(decrypted) { + Ok(s) => println!("{}", s), + Err(e) => error!("{}", e) + } + } + Err(e) => error!("{}", e) + } + } +}