From d80ba0567c3a0ed16d01fd3a0b72d9c4b54db38e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 15 Mar 2015 06:41:22 -0400 Subject: problem 5 --- src/lib.rs | 12 ++++++++++++ tests/lib.rs | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index cd5fda7..0371b54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,18 @@ pub fn fixed_xor (bytes1: &[u8], bytes2: &[u8]) -> Vec { .collect(); } +pub fn repeating_key_xor (plaintext: &[u8], key: &[u8]) -> Vec { + return fixed_xor( + plaintext, + &key + .iter() + .cycle() + .take(plaintext.len()) + .map(|c| *c) + .collect::>()[..] + ); +} + pub fn crack_single_byte_xor (input: &[u8]) -> Vec { let (decrypted, _) = crack_single_byte_xor_with_confidence(input); return decrypted; diff --git a/tests/lib.rs b/tests/lib.rs index 231be41..80342e3 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -37,3 +37,11 @@ fn problem_4 () { .collect::>>(); assert_eq!(matasano::find_single_byte_xor_encrypted_string(&possibles[..]), b"nOW\0THAT\0THE\0PARTY\0IS\0JUMPING*"); } + +#[test] +fn problem_5 () { + let plaintext = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"; + let key = b"ICE"; + let ciphertext = "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f".from_hex().unwrap(); + assert_eq!(matasano::repeating_key_xor(plaintext, key), ciphertext); +} -- cgit v1.2.3-54-g00ecf