solve Challenge 3 of Set 1
This commit is contained in:
parent
a5467c7293
commit
5b5c47fe15
107
set1/xor_cipher/Cargo.lock
generated
Normal file
107
set1/xor_cipher/Cargo.lock
generated
Normal file
|
@ -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"
|
||||
|
9
set1/xor_cipher/Cargo.toml
Normal file
9
set1/xor_cipher/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "xor_cipher"
|
||||
version = "0.1.0"
|
||||
authors = ["Thomas Lindner <thomas.lindner@fau.de>"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.3.6"
|
||||
env_logger = "0.3.4"
|
||||
rustc-serialize = "0.3"
|
49
set1/xor_cipher/src/main.rs
Normal file
49
set1/xor_cipher/src/main.rs
Normal file
|
@ -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<u8>) -> 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<u8> = vec![];
|
||||
|
||||
for key in 0..0xff {
|
||||
let plaintext: Vec<u8> = 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue