Implement encryption oracle for challenge 11.

This commit is contained in:
2022-04-22 18:56:39 -04:00
parent 9f3eb1356c
commit b757f359ff
5 changed files with 74 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
#![allow(dead_code)]
use crate::bytes::Bytes;
use crate::bytes_base64::BytesBase64;
use openssl::symm::{decrypt, Cipher};
use crate::ecb;
use std::io::{BufRead, BufReader, Write};
use std::str;
@@ -160,15 +160,16 @@ pub fn challenge7() {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
let ciphertext = &read("data/7.txt").0;
let key = b"YELLOW SUBMARINE";
let cipher = Cipher::aes_128_ecb();
let data = decrypt(cipher, key, None, ciphertext);
let result = match data {
Ok(data) => Bytes(data).to_utf8(),
Err(err) => err.to_string(),
};
let partial_msg = result[..20].to_string();
let text = Bytes::from_utf8("We meet at burger king!");
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let ciphertext = ecb::encrypt(&key, &text);
let roundtrip = ecb::decrypt(&key, &ciphertext);
if text != roundtrip {
panic!("ECB roundtrip not working.");
}
let ciphertext = read("data/7.txt");
let data = ecb::decrypt(&key, &ciphertext);
let partial_msg = data.to_utf8()[..20].to_string();
println!("[okay] Challenge 7: {}...", partial_msg);
}
@@ -198,6 +199,9 @@ pub fn challenge8() {
let index = ratings.iter().position(|e| e == min_rating).unwrap();
let average = ratings.iter().sum::<u32>() as f32 / ratings.len() as f32;
let partial_cipher = bytes_vector[index].to_hex()[..10].to_string();
if index != 132 {
panic!("Regression in challenge 8.");
}
println!(
"[okay] Challenge 8: Cipher {} [{}...] with rating {} (average = {}) is the solution.",
index, partial_cipher, min_rating, average