feat: finish p18, implment aes ctr mode
This commit is contained in:
@@ -1,3 +1,42 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use anyhow::Result;
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use rayon::prelude::*;
|
||||
|
||||
use common::aes_ecb_enc;
|
||||
|
||||
fn aes_ctr_enc(input: &[u8], key: &[u8; 16], nonce: u64) -> Result<Vec<u8>> {
|
||||
let mut key_stream = Vec::new();
|
||||
for round in 0..=(input.len() / 16) as u64 {
|
||||
let input: Vec<u8> = nonce
|
||||
.to_le_bytes()
|
||||
.into_iter()
|
||||
.chain(round.to_le_bytes())
|
||||
.collect();
|
||||
let stream_block = aes_ecb_enc(&input, key)?;
|
||||
key_stream.extend(stream_block);
|
||||
}
|
||||
let output: Vec<u8> = input
|
||||
.par_iter()
|
||||
.zip(key_stream)
|
||||
.map(|(&a, b)| a ^ b)
|
||||
.collect();
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn aes_ctr_dec(input: &[u8], key: &[u8; 16], nonce: u64) -> Result<Vec<u8>> {
|
||||
aes_ctr_enc(input, key, nonce)
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cipher = STANDARD
|
||||
.decode("L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==")?;
|
||||
|
||||
let key: [u8; 16] = "YELLOW SUBMARINE".as_bytes().try_into()?;
|
||||
|
||||
let plain = aes_ctr_dec(&cipher, &key, 0)?;
|
||||
let plain = String::from_utf8(plain)?;
|
||||
println!("{plain}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user