cryptopals

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

commit b290dc26ffc3b8fcffdb7f370838fa7a198159a4
parent cc79208a8ce2242a74b4e00ac2841520c5b044d3
Author: Jared Tobin <jared@jtobin.ca>
Date:   Sat, 17 Dec 2016 13:22:24 +1300

S1C5.

Diffstat:
Asrc/s1c5.rs | 40++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+), 0 deletions(-)

diff --git a/src/s1c5.rs b/src/s1c5.rs @@ -0,0 +1,40 @@ +extern crate rustc_serialize as serialize; + +use serialize::hex::{ToHex}; +use std::vec::Vec; + +const STRING: &'static str = + "Burning 'em, if you ain't quick and nimble\n\ + I go crazy when I hear a cymbal"; + +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() { + println!("{}", STRING); + println!("{}", repeating_key_xor(STRING, "ICE")); +} + +// to read from stdin: +// +// use std::io::{self, Read}; +// +// let mut buffer = String::new(); +// +// io::stdin().read_to_string(&mut buffer) +// .expect("Couldn't read."); +// +// println!("{}", &buffer); +// println!("{}", repeating_key_xor(&buffer, "ICE")); +