Restart with challenge 12 and implement find key length.

main
Felix Martin 2022-06-11 11:15:43 -04:00
parent eb0f8c4ede
commit e64cee1e2e
2 changed files with 29 additions and 7 deletions

4
data/12.txt Normal file
View File

@ -0,0 +1,4 @@
Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg
aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq
dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg
YnkK

View File

@ -102,15 +102,33 @@ pub fn challenge11() {
}
pub fn challenge12() {
let key = Bytes::random(16);
fn encrypt(Bytes(data): &Bytes, key: &Bytes) -> Bytes {
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 b = Bytes::from_utf8("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK");
data.append(&mut b.0);
let mut string = read("data/12.txt");
data.append(&mut string.0);
let cipher = ecb::encrypt(&key, &Bytes(data));
cipher
}
let text = Bytes::from_utf8("aaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccdddd");
let cipher = encrypt(&text, &key);
println!("[xxxx] Challenge 12: {}", cipher.0.len());
let key = Bytes::random(16); // consistent but unknown key
// 1. Find key length
let mut v = vec![];
let initial_cipher_len = encryption_oracle(&key, &Bytes(v.to_vec())).0.len();
let mut new_cipher_len = initial_cipher_len;
while initial_cipher_len == new_cipher_len {
v.push(b'A');
let cipher = encryption_oracle(&key, &Bytes(v.to_vec()));
new_cipher_len = cipher.0.len();
}
let key_length = new_cipher_len - initial_cipher_len;
println!("[xxxx] Challenge 12: key_length={}", key_length);
}