Finish challenge 20 statistical CTR.
This commit is contained in:
10
src/bytes.rs
10
src/bytes.rs
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user