Fix embarrassing naming error and still don't know what I am doing wrong.

This commit is contained in:
2022-04-25 20:21:59 -04:00
parent 4bbfc0e49f
commit f0cfefd352
3 changed files with 38 additions and 26 deletions

View File

@@ -1,6 +1,6 @@
mod bytes;
mod bytes_base64;
mod cbs;
mod cbc;
mod ecb;
mod set1;
mod set2;
@@ -13,7 +13,7 @@ fn main() {
// set1::challenge5();
// set1::challenge6();
// set1::challenge7();
// set1::challenge8();
set1::challenge8();
set2::challenge9();
set2::challenge10();
set2::challenge11();

View File

@@ -1,6 +1,6 @@
use crate::bytes::Bytes;
use crate::bytes_base64::BytesBase64;
use crate::cbs;
use crate::cbc;
use crate::ecb;
use rand::Rng;
@@ -19,11 +19,11 @@ pub fn challenge10() {
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let text = Bytes::from_utf8("aaaabbbbccccddddeeeeffffgggghhhh");
let ciphertext = cbs::encrypt(&key, &iv, &text);
let roundtrip = cbs::decrypt(&key, &iv, &ciphertext);
let ciphertext = cbc::encrypt(&key, &iv, &text);
let roundtrip = cbc::decrypt(&key, &iv, &ciphertext);
if text == roundtrip {
let ciphertext = read("data/10.txt");
let cleartext = cbs::decrypt(&key, &iv, &ciphertext);
let cleartext = cbc::decrypt(&key, &iv, &ciphertext);
let output = cleartext.to_utf8()[..10].to_string();
println!("[okay] Challenge 10: {}", output);
} else {
@@ -34,7 +34,7 @@ pub fn challenge10() {
pub fn challenge11() {
#[derive(Debug, PartialEq)]
enum EncryptionType {
CBS,
CBC,
ECB,
}
fn random_bytes(count: usize) -> Vec<u8> {
@@ -44,8 +44,6 @@ pub fn challenge11() {
}
fn pad_data(mut v: Vec<u8>) -> Bytes {
// Under the hood, have the function append 5-10 bytes (count chosen randomly)
// before the plaintext and 5-10 bytes after the plaintext.
let pre_count: usize = rand::thread_rng().gen_range(5..10);
let post_count: usize = rand::thread_rng().gen_range(5..10);
let mut pre_pad = random_bytes(pre_count);
@@ -58,23 +56,33 @@ pub fn challenge11() {
fn encryption_oracle(Bytes(data): &Bytes) -> (Bytes, EncryptionType) {
// Write a function that encrypts data under an unknown key --- that is, a
// function that generates a random key and encrypts under it.
let zero_or_one: u32 = rand::thread_rng().gen_range(0..2);
let padded_data = pad_data(data.to_vec());
let key = Bytes(random_bytes(16));
// let key = Bytes::from_utf8("YELLOW SUBMARINE");
// println!("{:?}", key);
// Under the hood, have the function append 5-10 bytes (count chosen randomly)
// before the plaintext and 5-10 bytes after the plaintext.
let _padded_data = pad_data(data.to_vec());
let padded_data = Bytes(data.to_vec());
// Now, have the function choose to encrypt under ECB 1/2 the time, and under CBC
// the other half (just use random IVs each time for CBC). Use rand(2) to decide
// which to use.
let zero_or_one: u32 = rand::thread_rng().gen_range(0..2);
let (data, encryption_type) = if zero_or_one == 1 {
(ecb::encrypt(&key, &padded_data), EncryptionType::ECB)
} else {
let iv = Bytes(random_bytes(16));
(cbs::encrypt(&key, &iv, &padded_data), EncryptionType::CBS)
// let iv = Bytes(random_bytes(16));
// (cbc::encrypt(&key, &iv, &padded_data), EncryptionType::CBC)
(Bytes(random_bytes(2876)), EncryptionType::CBC)
};
(data, encryption_type)
}
fn cbsecb_detection_oracle(Bytes(_data): &Bytes) -> EncryptionType {
fn cbcecb_detection_oracle(Bytes(_data): &Bytes) -> EncryptionType {
// Detect the block cipher mode the function is using each time. You should end up
// with a piece of code that, pointed at a block box that might be encrypting ECB
// or CBC, tells you which one is happening.
EncryptionType::CBS
EncryptionType::CBC
}
fn rate(Bytes(v): &Bytes, size: usize) -> u32 {
@@ -90,28 +98,32 @@ pub fn challenge11() {
rating / count
}
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
fn read_text() -> Bytes {
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let ciphertext = read("data/7.txt");
let text = ecb::decrypt(&key, &ciphertext);
text
}
fn run_oracle(count: usize) {
// let text = Bytes::from_utf8("On the 9th. William Findley and David Redick--deputed by the Committee of Safety (as it is designated) which met on the 2d. of this month at Parkinson Ferry arrived in Camp with the Resolutions of the said Committee; and to give information of the State of things in the four Western Counties of Pennsylvania to wit--Washington Fayette Westd. & Alligany in order to see if it would prevent the March of the Army into them.");
let text = read_text(); // get long enough text so that detection oracle works maybe?
let mut correct: usize = 0;
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let ciphertext = read("data/7.txt");
let text = ecb::decrypt(&key, &ciphertext);
println!("{}", text.0.len());
for _ in 0..count {
let (ciphertext, encryption_type) = encryption_oracle(&text);
let guessed_encryption_type = cbsecb_detection_oracle(&ciphertext);
let (ciphertext, et) = encryption_oracle(&text);
let get = cbcecb_detection_oracle(&ciphertext);
let rating = rate(&ciphertext, 16);
if encryption_type == guessed_encryption_type {
if et == get {
correct += 1;
}
println!("actual: {:?}; rating: {} ", encryption_type, rating);
println!("{:?}[{}] - rating: {} ", et, text.0.len(), rating);
}
println!("[open] Challenge 11: [{} / {}]", correct, count);
}
run_oracle(10);
// println!("[open] Challenge 11: workingonit");
}