Not sure how to solve challenge 11 yet my approach should be okay.
This commit is contained in:
50
src/set2.rs
50
src/set2.rs
@@ -32,6 +32,7 @@ pub fn challenge10() {
|
||||
}
|
||||
|
||||
pub fn challenge11() {
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum EncryptionType {
|
||||
CBS,
|
||||
ECB,
|
||||
@@ -43,6 +44,8 @@ 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);
|
||||
@@ -53,6 +56,8 @@ 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));
|
||||
@@ -64,4 +69,49 @@ pub fn challenge11() {
|
||||
};
|
||||
(data, encryption_type)
|
||||
}
|
||||
|
||||
fn cbsecb_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
|
||||
}
|
||||
|
||||
fn rate(Bytes(v): &Bytes, size: usize) -> u32 {
|
||||
let mut rating = 0;
|
||||
let chunks: Vec<&[u8]> = v.chunks(size).collect();
|
||||
let mut count = 0;
|
||||
for i in 0..chunks.len() {
|
||||
for j in (i + 1)..chunks.len() {
|
||||
rating += Bytes::hemming(&Bytes(chunks[i].to_vec()), &Bytes(chunks[j].to_vec()));
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
rating / count
|
||||
}
|
||||
|
||||
fn read(path: &str) -> Bytes {
|
||||
let s = std::fs::read_to_string(path).unwrap();
|
||||
BytesBase64::from_base64(&s).to_bytes()
|
||||
}
|
||||
|
||||
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 mut correct: usize = 0;
|
||||
let key = Bytes::from_utf8("YELLOW SUBMARINE");
|
||||
let ciphertext = read("data/7.txt");
|
||||
let text = ecb::decrypt(&key, &ciphertext);
|
||||
for _ in 0..count {
|
||||
let (ciphertext, encryption_type) = encryption_oracle(&text);
|
||||
let guessed_encryption_type = cbsecb_detection_oracle(&ciphertext);
|
||||
let rating = rate(&ciphertext, 16);
|
||||
if encryption_type == guessed_encryption_type {
|
||||
correct += 1;
|
||||
}
|
||||
println!("actual: {:?}; rating: {} ", encryption_type, rating);
|
||||
}
|
||||
println!("[open] Challenge 11: [{} / {}]", correct, count);
|
||||
}
|
||||
run_oracle(10);
|
||||
// println!("[open] Challenge 11: workingonit");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user