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

@@ -138,17 +138,16 @@ pub fn challenge6() {
result
}
fn guess_key(bytes: &Bytes) -> u8 {
let mut h: Vec<(Bytes, u8)> = (0..=255).map(|i| (Bytes::xor_byte(bytes, i), i)).collect();
h.sort_by(|a, b| a.0.ascii_score().partial_cmp(&b.0.ascii_score()).unwrap());
h.last().unwrap().1
}
_test_roundtrip();
let bytes = read("data/6.txt");
let key_size = guess_key_size(&bytes);
let bytes_tranposed = transpose(&bytes, key_size);
let key = Bytes(bytes_tranposed.iter().map(|b| guess_key(&b)).collect());
let key = Bytes(
bytes_tranposed
.iter()
.map(|b| Bytes::guess_key(&b))
.collect(),
);
let msg = Bytes::xor_cycle(&bytes, &key).to_utf8();
let partial_msg = msg[..20].to_string();