summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-03-15 06:41:22 -0400
committerJesse Luehrs <doy@tozt.net>2015-03-15 06:41:22 -0400
commitd80ba0567c3a0ed16d01fd3a0b72d9c4b54db38e (patch)
tree688ca9c953e4c19cb04b806ed0d40db1e04bff6b
parente297730510998b5c355e52ea3c4f14e4727d7d86 (diff)
downloadmatasano-d80ba0567c3a0ed16d01fd3a0b72d9c4b54db38e.tar.gz
matasano-d80ba0567c3a0ed16d01fd3a0b72d9c4b54db38e.zip
problem 5
-rw-r--r--src/lib.rs12
-rw-r--r--tests/lib.rs8
2 files changed, 20 insertions, 0 deletions
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<u8> {
.collect();
}
+pub fn repeating_key_xor (plaintext: &[u8], key: &[u8]) -> Vec<u8> {
+ return fixed_xor(
+ plaintext,
+ &key
+ .iter()
+ .cycle()
+ .take(plaintext.len())
+ .map(|c| *c)
+ .collect::<Vec<u8>>()[..]
+ );
+}
+
pub fn crack_single_byte_xor (input: &[u8]) -> Vec<u8> {
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::<Vec<Vec<u8>>>();
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);
+}