Implement challenge 26 ez katka
This commit is contained in:
41
src/set4.rs
41
src/set4.rs
@@ -1,4 +1,4 @@
|
||||
use crate::{bytes::Bytes, ctr, utils};
|
||||
use crate::{bytes::Bytes, ctr, parser, utils};
|
||||
|
||||
pub fn challenge25() {
|
||||
let cipher = utils::read_base64("data/25.txt");
|
||||
@@ -43,5 +43,42 @@ pub fn challenge25() {
|
||||
}
|
||||
|
||||
pub fn challenge26() {
|
||||
println!("[xxxx] Challenge 26: TBD");
|
||||
fn encrypt(input: &str, key: &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 data = Bytes(r.as_bytes().to_vec());
|
||||
ctr::encrypt(key, 0, &data)
|
||||
}
|
||||
|
||||
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=aaaaaaaaaaaaaaaa;comment2=%20like%20a%20pound%20of%20bacon
|
||||
// comment1=cooking%20MCs;userdata=fobar;admin=true;comment2=%20like%20a%20pound%20of%20bacon
|
||||
let input = "aaaaaaaaaaaaaaaa";
|
||||
let cipher = encrypt(&input, &key);
|
||||
let keystream = utils::xor(&cipher.0[32..48], &Bytes::from_utf8(input).0);
|
||||
|
||||
let input = "fobar;admin=true";
|
||||
let mut flipped_cipher = cipher.0[0..32].to_vec();
|
||||
flipped_cipher.append(&mut utils::xor(&keystream, &input.as_bytes()));
|
||||
flipped_cipher.append(&mut cipher.0[48..cipher.len()].to_vec());
|
||||
|
||||
let cleartext = ctr::decrypt(&key, 0, &Bytes(flipped_cipher));
|
||||
let dict = parser::parse_key_value(&cleartext.to_utf8());
|
||||
let admin_status = dict.get("admin").unwrap();
|
||||
assert_eq!(admin_status, "true");
|
||||
println!("[okay] Challenge 26: admin={}", admin_status);
|
||||
}
|
||||
|
||||
pub fn challenge27() {
|
||||
println!("[xxxx] Challenge 27: tbd");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user