Solve challenge 19 in Python because my Rust still sucks.

This commit is contained in:
2022-07-19 19:35:38 -04:00
parent e70c6c470c
commit 467c3bdbef
5 changed files with 232 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ use crate::bytes_base64::BytesBase64;
use crate::cbc;
use crate::ctr;
use rand::Rng;
use std::fs;
use std::io::{BufRead, BufReader};
pub fn challenge17() {
@@ -100,7 +101,7 @@ pub fn challenge18() {
let cleartext = Bytes::from_utf8("Let's see if we can get the party started hard my friends.");
let cipher = ctr::encrypt(&key, 42351234, &cleartext);
let roundtrip = ctr::encrypt(&key, 42351234, &cipher);
let roundtrip = ctr::decrypt(&key, 42351234, &cipher);
assert_eq!(cleartext, roundtrip);
let cipher = BytesBase64::from_base64(
@@ -112,5 +113,30 @@ pub fn challenge18() {
}
pub fn challenge19() {
println!("[xxxx] Challenge 19: TBD");
fn read(path: &str) -> Vec<Bytes> {
let file = std::fs::File::open(path).unwrap();
let br = BufReader::new(file);
br.lines()
.map(|line| BytesBase64::from_base64(&line.unwrap()).to_bytes())
.collect()
}
let plaintexts = read("data/19.txt");
let key = Bytes::from_utf8("YELLOW SUBMARINE");
let encrypt = |plaintext: &Bytes| -> Bytes { ctr::encrypt(&key, 0, plaintext) };
let ciphers: Vec<Bytes> = plaintexts.iter().map(|ct| encrypt(&ct)).collect();
let mut data = String::new();
for cipher in ciphers.iter() {
let copy_cipher = Bytes(cipher.0.to_vec());
let cipher_base64 = BytesBase64::from_bytes(copy_cipher);
data.push_str(&cipher_base64.to_string());
data.push_str("\n");
}
fs::write("data/19_enc.txt", data).expect("Unable to write file");
println!("[okay] Challenge 19: encrypted to data/19_enc.txt, attack in set3c19.py.");
}
pub fn challenge20() {
println!("[xxxx] Challenge 20: TBD");
}