Finish challenge 16 and problem set 2.

This commit is contained in:
2022-06-28 15:56:31 -04:00
parent fd6f9464cc
commit 956d75ab4e
3 changed files with 58 additions and 3 deletions

View File

@@ -411,5 +411,37 @@ pub fn challenge15() {
}
pub fn challenge16() {
println!("[xxxx] Challenge 16: TBD");
fn encrypt(input: &str, key: &Bytes, iv: &Bytes) -> Bytes {
let mut r = String::new();
for c in input.chars() {
if c == ';' || c == '=' {
panic!("encrypt: invalid char {}", c);
}
}
r.push_str("comment1=cooking%20MCs;userdata=");
r.push_str(input);
r.push_str(";comment2=%20like%20a%20pound%20of%20bacon");
let mut cleartext = Bytes(r.as_bytes().to_vec());
cleartext.pad_pkcs7(16);
cbc::encrypt(&key, &iv, &cleartext)
}
let iv = Bytes::random(16);
let key = Bytes::random(16);
// 0 16 32 48 64
// 0..34..78..bc..f0..34..78..bc..f0..34..78..bc..f0..34..78..bc..f0..34..78..bc..f
// comment1=cooking%20MCs;userdata=xxx=x;admin=true;comment2=%20like%20a%20pound%20of%20bacon
// flip_bit(2, '9') = '='; flip_bit(1, '9') = ';'
let mut cipher = encrypt("xxx9x9admin9true", &key, &iv);
cipher.flip_bit(19, 2); // results in flipping same bit in byte of the following block
cipher.flip_bit(21, 1);
cipher.flip_bit(27, 2);
let mut cleartext = cbc::decrypt(&key, &iv, &cipher);
cleartext.remove_pkcs7(16);
let cleartext_stripped = Bytes(cleartext.0[32..].to_vec());
let dict = parser::parse_key_value(&cleartext_stripped.to_utf8());
let admin_status = dict.get("admin").unwrap();
assert_eq!(admin_status, "true");
println!("[okay] Challenge 16: admin={}", admin_status);
}