Skip to content

Commit

Permalink
Solve challenge 19
Browse files Browse the repository at this point in the history
  • Loading branch information
mitiko committed Jun 30, 2024
1 parent e85d43b commit 38be635
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
40 changes: 40 additions & 0 deletions data/set3/challenge19.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
SSBoYXZlIG1ldCB0aGVtIGF0IGNsb3NlIG9mIGRheQ==
Q29taW5nIHdpdGggdml2aWQgZmFjZXM=
RnJvbSBjb3VudGVyIG9yIGRlc2sgYW1vbmcgZ3JleQ==
RWlnaHRlZW50aC1jZW50dXJ5IGhvdXNlcy4=
SSBoYXZlIHBhc3NlZCB3aXRoIGEgbm9kIG9mIHRoZSBoZWFk
T3IgcG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==
T3IgaGF2ZSBsaW5nZXJlZCBhd2hpbGUgYW5kIHNhaWQ=
UG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==
QW5kIHRob3VnaHQgYmVmb3JlIEkgaGFkIGRvbmU=
T2YgYSBtb2NraW5nIHRhbGUgb3IgYSBnaWJl
VG8gcGxlYXNlIGEgY29tcGFuaW9u
QXJvdW5kIHRoZSBmaXJlIGF0IHRoZSBjbHViLA==
QmVpbmcgY2VydGFpbiB0aGF0IHRoZXkgYW5kIEk=
QnV0IGxpdmVkIHdoZXJlIG1vdGxleSBpcyB3b3JuOg==
QWxsIGNoYW5nZWQsIGNoYW5nZWQgdXR0ZXJseTo=
QSB0ZXJyaWJsZSBiZWF1dHkgaXMgYm9ybi4=
VGhhdCB3b21hbidzIGRheXMgd2VyZSBzcGVudA==
SW4gaWdub3JhbnQgZ29vZCB3aWxsLA==
SGVyIG5pZ2h0cyBpbiBhcmd1bWVudA==
VW50aWwgaGVyIHZvaWNlIGdyZXcgc2hyaWxsLg==
V2hhdCB2b2ljZSBtb3JlIHN3ZWV0IHRoYW4gaGVycw==
V2hlbiB5b3VuZyBhbmQgYmVhdXRpZnVsLA==
U2hlIHJvZGUgdG8gaGFycmllcnM/
VGhpcyBtYW4gaGFkIGtlcHQgYSBzY2hvb2w=
QW5kIHJvZGUgb3VyIHdpbmdlZCBob3JzZS4=
VGhpcyBvdGhlciBoaXMgaGVscGVyIGFuZCBmcmllbmQ=
V2FzIGNvbWluZyBpbnRvIGhpcyBmb3JjZTs=
SGUgbWlnaHQgaGF2ZSB3b24gZmFtZSBpbiB0aGUgZW5kLA==
U28gc2Vuc2l0aXZlIGhpcyBuYXR1cmUgc2VlbWVkLA==
U28gZGFyaW5nIGFuZCBzd2VldCBoaXMgdGhvdWdodC4=
VGhpcyBvdGhlciBtYW4gSSBoYWQgZHJlYW1lZA==
QSBkcnVua2VuLCB2YWluLWdsb3Jpb3VzIGxvdXQu
SGUgaGFkIGRvbmUgbW9zdCBiaXR0ZXIgd3Jvbmc=
VG8gc29tZSB3aG8gYXJlIG5lYXIgbXkgaGVhcnQs
WWV0IEkgbnVtYmVyIGhpbSBpbiB0aGUgc29uZzs=
SGUsIHRvbywgaGFzIHJlc2lnbmVkIGhpcyBwYXJ0
SW4gdGhlIGNhc3VhbCBjb21lZHk7
SGUsIHRvbywgaGFzIGJlZW4gY2hhbmdlZCBpbiBoaXMgdHVybiw=
VHJhbnNmb3JtZWQgdXR0ZXJseTo=
QSB0ZXJyaWJsZSBiZWF1dHkgaXMgYm9ybi4=
2 changes: 1 addition & 1 deletion src/set1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ lazy_static! {
};
}

fn xor_cross_entropy_analysis(encoded: &[u8]) -> (u8, f64) {
pub fn xor_cross_entropy_analysis(encoded: &[u8]) -> (u8, f64) {
let mut min_cross_entropy = f64::MAX;
let mut best_key = 0;
for key in 0..=255 {
Expand Down
34 changes: 32 additions & 2 deletions src/set3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{cbc::*, ctr::aes128_ctr_decrypt, ecb::*, utils::{conversions::base64_to_raw, io::*}};
use crate::{
cbc::*,
ctr::aes128_ctr_decrypt,
ecb::*,
set1::xor_cross_entropy_analysis,
utils::{conversions::base64_to_raw, io::*},
};
use lazy_static::lazy_static;
use rand::{Rng, SeedableRng};

Expand Down Expand Up @@ -139,8 +145,32 @@ fn challange17() {

#[test]
fn challange18() {
let ciphertext = base64_to_raw("L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==");
let ciphertext =
base64_to_raw("L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==");
let key = b"YELLOW SUBMARINE";
let plaintext = aes128_ctr_decrypt(&ciphertext, key, 0, 0);
assert!(String::from_utf8_lossy(&plaintext).contains("Ice, Ice, baby"));
}

#[test]
fn challange19() {
let ciphertexts = read_base64_lines("data/set3/challenge19.txt");
let mut plaintexts = vec![Vec::new(); ciphertexts.len()];
let len = ciphertexts.iter().map(|c| c.len()).max().unwrap();
for idx in 0..len {
let cipher_bytes: Vec<u8> = ciphertexts
.iter()
.filter_map(|c| c.get(idx))
.map(|&x| x)
.collect();
let (key, _) = xor_cross_entropy_analysis(&cipher_bytes);
for (i, plaintext) in plaintexts.iter_mut().enumerate() {
if let Some(byte) = ciphertexts[i].get(idx) {
plaintext.push(byte ^ key);
}
}
}
assert!(String::from_utf8_lossy(&plaintexts[5]).contains("polite meaningless words"));
assert!(String::from_utf8_lossy(&plaintexts[34]).contains("yet I number him in the song"));
// doesn't decode all the way correctly but gets the bulk of the job done
}

0 comments on commit 38be635

Please sign in to comment.