cryptopals

Matasano's cryptopals challenges (cryptopals.com).
Log | Files | Refs | README | LICENSE

commit 91d0db9973f11b6c292a2c9c99e67b68cb84d22a
parent c34984041c5a79bb1f85041dae20742db55f49e2
Author: Jared Tobin <jared@jtobin.ca>
Date:   Thu, 22 Dec 2016 22:42:51 +1300

Add repeating-key bin.

Diffstat:
Aetc/data/s1c5.txt | 2++
Alib/repeating_key_xor/Cargo.lock | 14++++++++++++++
Alib/repeating_key_xor/Cargo.toml | 8++++++++
Alib/repeating_key_xor/src/main.rs | 46++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/etc/data/s1c5.txt b/etc/data/s1c5.txt @@ -0,0 +1,2 @@ +Burning 'em, if you ain't quick and nimble +I go crazy when I hear a cymbal diff --git a/lib/repeating_key_xor/Cargo.lock b/lib/repeating_key_xor/Cargo.lock @@ -0,0 +1,14 @@ +[root] +name = "repeating_key_xor" +version = "0.1.0" +dependencies = [ + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b" diff --git a/lib/repeating_key_xor/Cargo.toml b/lib/repeating_key_xor/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "repeating_key_xor" +version = "0.1.0" +authors = ["Jared Tobin <jared@jtobin.ca>"] + +[dependencies] + +rustc-serialize = "0.3.0" diff --git a/lib/repeating_key_xor/src/main.rs b/lib/repeating_key_xor/src/main.rs @@ -0,0 +1,46 @@ +extern crate rustc_serialize as serialize; + +use serialize::hex::{ToHex}; +use std::env; +use std::io::{self, Read}; +use std::string::String; +use std::vec::Vec; + +fn repeating_key_xor(text: &str, key: &str) -> String { + let text_bytes = text.as_bytes(); + let key_bytes = key.as_bytes(); + + let mut xored: Vec<u8> = vec![0; text_bytes.len()]; + + for (idx, val) in text_bytes.iter().enumerate() { + let byte_idx = idx % key_bytes.len(); + xored[idx] = val ^ key_bytes[byte_idx]; + } + + xored.to_hex() +} + +fn main() { + // deal with args + let args: Vec<String> = env::args().collect(); + + if args.len() != 2 { + println!("USAGE: echo FOO | ./repeating_key_xor KEY"); + return () + } + + let supplied_key = &args[1]; + + // deal with stdin + let mut buffer = String::new(); + + io::stdin().read_to_string(&mut buffer) + .expect("repeating_key_xor: bad input"); + + let xored = repeating_key_xor(&buffer, &supplied_key); + + println!("original: \n{}", &buffer); + println!("xored with: {}", supplied_key); + println!("result: \n{}", xored); + +}