Start with challenge 17.

This commit is contained in:
2022-07-02 10:42:36 -04:00
parent 956d75ab4e
commit d1be95cbe1
4 changed files with 68 additions and 20 deletions

36
src/set3.rs Normal file
View File

@@ -0,0 +1,36 @@
use crate::bytes::Bytes;
use crate::bytes_base64::BytesBase64;
use crate::cbc;
use rand::Rng;
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()
}
fn encrypt(ten_strings: &Vec<Bytes>, key: &Bytes) -> (Bytes, Bytes) {
// The first function should select at random one of the ten strings
let index: usize = rand::thread_rng().gen_range(0..ten_strings.len());
let mut cleartext = Bytes(ten_strings[index].0.to_vec());
// pad the string out to the 16-byte AES block size and
cleartext.pad_pkcs7(16);
// CBC-encrypt it under that key, providing the caller the ciphertext
// and IV.
let iv = Bytes::random(16);
(cbc::encrypt(&key, &iv, &cleartext), iv)
}
let cleartexts = read("data/17.txt");
// generate a random AES key (which it should save for all future encryptions)
let key = Bytes::random(16);
let (cipher, _iv) = encrypt(&cleartexts, &key);
println!("[xxxx] Challenge 17: {}", cipher.len());
}