Finish challenge 20 statistical CTR.

This commit is contained in:
2022-07-29 09:20:59 -04:00
parent 3248609119
commit 6485a2f068
5 changed files with 112 additions and 9 deletions

View File

@@ -93,6 +93,16 @@ impl Bytes {
r
}
pub fn guess_key(&self) -> u8 {
// Assuming all bytes have been xor-encrypted by the same u8 key, find that key
// by trying all u8 values and compute an ascii score for the resulting "plaintext".
// The u8 key for the "plaintext" with the highest score is returned as the guessed
// key.
let mut h: Vec<(Bytes, u8)> = (0..=255).map(|i| (Bytes::xor_byte(self, i), i)).collect();
h.sort_by(|a, b| a.0.ascii_score().partial_cmp(&b.0.ascii_score()).unwrap());
h.last().unwrap().1
}
pub fn pad_pkcs7(&mut self, block_size: usize) -> () {
let Bytes(v) = self;
let padding_value = (block_size - v.len() % block_size) as u8;