Skip to content

Commit

Permalink
Solution for 'Implement repeating-key XOR' challenge
Browse files Browse the repository at this point in the history
http://cryptopals.com/sets/1/challenges/5/

Problem Statement:

Here is the opening stanza of an important work of the English language:

Burning 'em, if you ain't quick and nimble
I go crazy when I hear a cymbal

Encrypt it, under the key "ICE", using repeating-key XOR.

In repeating-key XOR, you'll sequentially apply each byte of the key; the first
byte of plaintext will be XOR'd against I, the next C, the next E, then I again
for the 4th byte, and so on.

It should come out to:
0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272
a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f

Encrypt a bunch of stuff using your repeating-key XOR function. Encrypt your
mail. Encrypt your password file. Your .sig file. Get a feel for it. I promise,
we aren't wasting your time with this.

Usage:

> cat in
Burning 'em, if you ain't quick and nimble
I go crazy when I hear a cymbal

> ./target/debug/xor "ICE" < in
  • Loading branch information
jakerr committed Sep 16, 2015
1 parent 7823c40 commit 61ef3e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
name = "cryptopals"
version = "0.1.0"
authors = ["Jake Kerr <[email protected]>"]

[[bin]]
name = "xor"
path = "src/challenges/05_repeating_key_xor.rs"
21 changes: 21 additions & 0 deletions src/challenges/05_repeating_key_xor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate cryptopals;

use std::env;
use std::io::{stdin, stdout, Read, Write};
use cryptopals::conversions::hex_to_string;

fn main() {
let args: Vec<_> = env::args().collect();
if args.len() != 2 {
panic!("Requires one argument which is the key with which to do repeating key xor \
over stdin's bytes");
}
let key = args.get(1).unwrap().bytes().cycle();
let stdin = stdin().bytes().map(|b| b.unwrap());
let mut stdout = stdout();
for (a, b) in stdin.zip(key) {
if a == 10 { continue } // Line feed
write!(&mut stdout, "{}", hex_to_string(&[a ^ b]));
}
write!(&mut stdout, "\n");
}

0 comments on commit 61ef3e7

Please sign in to comment.