Remove duplicated code and make base64 error handling better

This commit is contained in:
2022-08-14 09:12:36 -04:00
parent fbf26efa44
commit 5158c16d56
8 changed files with 132 additions and 88 deletions

View File

@@ -1,8 +1,8 @@
use crate::bytes::Bytes;
use crate::bytes_base64::BytesBase64;
use crate::cbc;
use crate::ecb;
use crate::parser;
use crate::utils;
use rand::Rng;
use std::collections::HashMap;
@@ -13,10 +13,6 @@ pub fn challenge9() {
}
pub fn challenge10() {
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
let iv = Bytes(vec![0; 16]);
let key = Bytes::from_utf8("YELLOW SUBMARINE");
@@ -24,7 +20,7 @@ pub fn challenge10() {
let ciphertext = cbc::encrypt(&key, &iv, &text);
let roundtrip = cbc::decrypt(&key, &iv, &ciphertext);
if text == roundtrip {
let ciphertext = read("data/10.txt");
let ciphertext = utils::read_base64("data/10.txt");
let cleartext = cbc::decrypt(&key, &iv, &ciphertext);
let output = cleartext.to_utf8()[..16].to_string();
println!("[okay] Challenge 10: {}", output);
@@ -104,16 +100,11 @@ pub fn challenge11() {
}
pub fn challenge12() {
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
fn encryption_oracle(key: &Bytes, Bytes(data): &Bytes) -> Bytes {
// Copy your oracle function to a new function that encrypts buffers under ECB mode using a consistent but unknown key
// Now take that same function and have it append to the plaintext, BEFORE ENCRYPTING, the following string (from 12.txt):
let mut data = data.to_vec();
let mut string = read("data/12.txt");
let mut string = utils::read_base64("data/12.txt");
data.append(&mut string.0);
let cipher = ecb::encrypt(&key, &Bytes(data));
cipher
@@ -184,7 +175,7 @@ pub fn challenge12() {
assert_eq!(get_block_size(&key), 16); // 1. discover block size
assert_eq!(is_encryption_ecb(&key), true); // 2. confirm oracle uses ecb
let roundtrip_text = decode(&key); // 3.-6.
let cleartext = read("data/12.txt");
let cleartext = utils::read_base64("data/12.txt");
// 138 (instead of 139); I think we get one additional byte because we guess
// the first padding byte. The right approach would be to remove the last
@@ -254,11 +245,6 @@ pub fn challenge13() {
}
pub fn challenge14() {
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
fn encryption_oracle(
Bytes(random_prefix): &Bytes,
random_key: &Bytes,
@@ -271,7 +257,7 @@ pub fn challenge14() {
// long it is and then adjust the decoding routine.
let mut plaintext = random_prefix.to_vec();
plaintext.append(&mut attacker_controlled.to_vec());
let mut target_bytes = read("data/12.txt").0;
let mut target_bytes = utils::read_base64("data/12.txt").0;
plaintext.append(&mut target_bytes);
let cipher = ecb::encrypt(&random_key, &Bytes(plaintext));
cipher
@@ -383,7 +369,7 @@ pub fn challenge14() {
let prefix_len = get_prefix_size(&prefix, &key);
assert_eq!(prefix.len(), prefix_len);
let roundtrip_text = decode(&prefix, &key);
let cleartext = read("data/12.txt");
let cleartext = utils::read_base64("data/12.txt");
assert_eq!(roundtrip_text, cleartext);
println!(
"[okay] Challenge 14: {}",