cryptopals

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

commit 66a708c4cb022a38329ca0a338c4865af77bad64
parent 9965aa64dabc6ae41d02d4ba08d02b8523a68c23
Author: Jared Tobin <jared@jtobin.ca>
Date:   Sat, 13 May 2017 20:11:20 +1200

Add aes-ecb tool.

Diffstat:
MREADME.md | 8++++++++
Mlib/aes_ecb/src/main.rs | 41+++++++++++++++++++++++++----------------
2 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/README.md b/README.md @@ -162,6 +162,14 @@ I like openssl, heck the rules: I'm back and I'm ringin' the bell A rockin' on the mike while the fly girls yell +Alternatively: + + $ cat data/s1/q7_input.txt | tr -d '\n' | ./bin/aes_ecb | head -2 + 'm back and I'm ringin' the bell + A rockin' on the mike while the fly girls yell + +NB. missing the initial 'I' there for some reason. + #### 1.8 $ cat data/s1/q8_input.txt | parallel \ diff --git a/lib/aes_ecb/src/main.rs b/lib/aes_ecb/src/main.rs @@ -1,34 +1,43 @@ + extern crate base64; extern crate openssl; -use base64::decode; -use openssl::symm::{Cipher, Crypter}; -use openssl::symm::Mode; +use openssl::symm::{Cipher, Crypter, Mode}; use std::io::{self, Read}; -use std::str; fn main() { let mut buffer = String::new(); - io::stdin().read_to_string(&mut buffer) - .expect("aes_ecb"); + io::stdin().read_to_string(&mut buffer).expect("aes_ecb"); + + let decoded = match base64::decode(&buffer) { + Ok(val) => val, + Err(err) => panic!("{}", err) + }; - let decoded = &decode(&buffer).unwrap(); let decoded_len = decoded.len(); - let mut crypter = Crypter::new( - Cipher::aes_128_ecb() - , Mode::Decrypt - , b"YELLOW SUBMARINE" - , None).unwrap(); + let cipher = Cipher::aes_128_ecb(); + let mode = Mode::Decrypt; + let key = b"YELLOW SUBMARINE"; + let iv = None; + + let mut crypter = match Crypter::new(cipher, mode, key, iv) { + Ok(val) => val, + Err(err) => panic!("{}", err) + }; crypter.pad(false); - let mut result = vec![0u8; 2896]; + let mut result = vec![0u8; decoded_len + cipher.key_len()]; + + let decrypted_len = match crypter.update(&decoded, result.as_mut_slice()) { + Ok(val) => val, + Err(err) => panic!("{}", err) + }; - let c_len = crypter.update(&decoded[..decoded_len], result.as_mut_slice()).unwrap(); - let output = &result[1..c_len]; + let output = &result[1..decrypted_len]; - println!("{:?}", String::from_utf8_lossy(output)); + println!("{}", String::from_utf8_lossy(output)); }