Start with challenge 17.
This commit is contained in:
36
src/set3.rs
Normal file
36
src/set3.rs
Normal 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());
|
||||
}
|
||||
Reference in New Issue
Block a user