Solve challenge 7 using openssl.

This commit is contained in:
2022-03-27 14:06:41 -04:00
parent 110fb61e18
commit 6cc8ed65d0
4 changed files with 87 additions and 6 deletions

View File

@@ -9,4 +9,5 @@ fn main() {
set1::challenge4();
set1::challenge5();
set1::challenge6();
set1::challenge7();
}

View File

@@ -1,6 +1,7 @@
#![allow(dead_code)]
use crate::bytes::Bytes;
use crate::bytes_base64::BytesBase64;
use openssl::symm::{decrypt, Cipher};
use std::io::{BufRead, BufReader, Write};
use std::str;
@@ -148,11 +149,25 @@ pub fn challenge6() {
let key_size = guess_key_size(&bytes);
let bytes_tranposed = transpose(&bytes, key_size);
let key = Bytes(bytes_tranposed.iter().map(|b| guess_key(&b)).collect());
let msg = Bytes::xor_cycle(&bytes, &key);
let msg = Bytes::xor_cycle(&bytes, &key).to_utf8();
let partial_msg = Bytes(msg.0[..20].to_vec()).to_utf8();
println!(
"[okay] Challenge 6: size={} msg={}...",
key_size, partial_msg
);
let partial_msg = msg[..20].to_string();
println!("[okay] Challenge 6: {}...", partial_msg);
}
pub fn challenge7() {
fn read(path: &str) -> Bytes {
let s = std::fs::read_to_string(path).unwrap();
BytesBase64::from_base64(&s).to_bytes()
}
let ciphertext = &read("data/7.txt").0;
let key = b"YELLOW SUBMARINE";
let cipher = Cipher::aes_128_ecb();
let data = decrypt(cipher, key, None, ciphertext);
let result = match data {
Ok(data) => Bytes(data).to_utf8(),
Err(err) => err.to_string(),
};
let partial_msg = result[..20].to_string();
println!("[okay] Challenge 7: {}...", partial_msg);
}