I realized that I am fine if I can choose the plaintext. This solves 11.
This commit is contained in:
53
src/set2.rs
53
src/set2.rs
@@ -57,8 +57,6 @@ pub fn challenge11() {
|
||||
// Write a function that encrypts data under an unknown key --- that is, a
|
||||
// function that generates a random key and encrypts under it.
|
||||
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.
|
||||
@@ -78,52 +76,47 @@ pub fn challenge11() {
|
||||
(data, encryption_type)
|
||||
}
|
||||
|
||||
fn cbcecb_detection_oracle(Bytes(_data): &Bytes) -> EncryptionType {
|
||||
fn cbcecb_detection_oracle(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::CBC
|
||||
}
|
||||
|
||||
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;
|
||||
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
|
||||
}
|
||||
rating / count
|
||||
}
|
||||
|
||||
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 rating = rate(&data, 16);
|
||||
if rating < 50 {
|
||||
EncryptionType::ECB
|
||||
} else {
|
||||
EncryptionType::CBC
|
||||
}
|
||||
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 text = Bytes::from_utf8("aaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccdddd");
|
||||
let mut correct: usize = 0;
|
||||
println!("{}", text.0.len());
|
||||
for _ in 0..count {
|
||||
let (ciphertext, et) = encryption_oracle(&text);
|
||||
let get = cbcecb_detection_oracle(&ciphertext);
|
||||
let rating = rate(&ciphertext, 16);
|
||||
if et == get {
|
||||
correct += 1;
|
||||
}
|
||||
println!("{:?}[{}] - rating: {} ", et, text.0.len(), rating);
|
||||
}
|
||||
println!("[open] Challenge 11: [{} / {}]", correct, count);
|
||||
if correct == count {
|
||||
println!("[okay] Challenge 11: [{} / {}]", correct, count);
|
||||
} else {
|
||||
println!("[fail] Challenge 11: [{} / {}]", correct, count);
|
||||
}
|
||||
}
|
||||
run_oracle(10);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user