1
0

feat: finish p18, implment aes ctr mode

This commit is contained in:
2025-08-24 22:28:40 +08:00
parent e400b87e9f
commit b2424c1fba
6 changed files with 165 additions and 4 deletions

View File

@@ -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(())
}