Finish challenge 16 and problem set 2.
This commit is contained in:
34
src/set2.rs
34
src/set2.rs
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user