Remove duplicated code and make base64 error handling better

This commit is contained in:
2022-08-14 09:12:36 -04:00
parent fbf26efa44
commit 5158c16d56
8 changed files with 132 additions and 88 deletions

View File

@@ -9,21 +9,12 @@ use rand::Rng;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::HashSet;
use std::io::{BufRead, BufReader};
pub fn challenge17() {
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 key = Bytes::random(16);
let encrypt = || -> (Bytes, Bytes, usize) {
// The first function should select at random one of the ten strings
let cleartexts = read("data/17.txt");
let cleartexts = utils::read_base64_lines("data/17.txt");
let index: usize = rand::thread_rng().gen_range(0..cleartexts.len());
let mut cleartext = Bytes(cleartexts[index].0.to_vec());
@@ -95,7 +86,7 @@ pub fn challenge17() {
roundtrip.0.append(&mut clear_block.0);
}
roundtrip.remove_pkcs7(16);
let cleartexts = read("data/17.txt");
let cleartexts = utils::read_base64_lines("data/17.txt");
let cleartext = Bytes(cleartexts[cleartext_index].0.to_vec());
assert_eq!(roundtrip, cleartext);
println!("[okay] Challenge 17: {}", roundtrip.to_utf8());
@@ -112,20 +103,13 @@ pub fn challenge18() {
let cipher = BytesBase64::from_base64(
"L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==",
)
.unwrap()
.to_bytes();
let cleartext = ctr::decrypt(&key, 0, &cipher).to_utf8();
println!("[okay] Challenge 18: {cleartext}");
}
pub fn challenge19() {
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()
}
fn xor_to_char_set(letters: &Vec<u8>) -> HashMap<u8, RefCell<HashSet<u8>>> {
let mut h = HashMap::new();
for i in 0..255_u8 {
@@ -255,7 +239,7 @@ pub fn challenge19() {
decrypts[37].borrow_mut()[30] = b'i';
}
let plaintexts = read("data/19.txt");
let plaintexts = utils::read_base64_lines("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();
@@ -271,14 +255,6 @@ pub fn challenge19() {
}
pub fn challenge20() {
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()
}
fn attack(ciphers: Vec<Bytes>) -> Vec<Bytes> {
let min_cipher_len = ciphers.iter().map(|c| c.len()).min().unwrap_or(0);
let mut key: Vec<u8> = vec![];
@@ -294,7 +270,7 @@ pub fn challenge20() {
.collect()
}
let plaintexts = read("data/20.txt");
let plaintexts = utils::read_base64_lines("data/20.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();