feat: 完善AES加密实现和多个问题的解决方案
- 在common库中添加了完整的AES-128加密解密实现 - 实现了AES-ECB和AES-CBC模式的加密解密函数 - 添加了密钥扩展和所有必要的AES操作函数 - 完成了问题10的AES-CBC模式实现 - 修复了问题11的加密oracle实现,使用正确的ECB检测 - 改进了代码风格,使用现代Rust格式化语法 - 为多个问题添加了common库的依赖 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,141 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
#![allow(dead_code)]
|
||||
use anyhow::{Result, anyhow};
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use common::{
|
||||
add_round_key, expand_key, inv_mix_columns, inv_shift_rows, inv_sub_bytes, mix_columns,
|
||||
shift_rows, sub_bytes,
|
||||
};
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn aes_cbc_enc(input: &[u8], key: &[u8; 16], iv: &[u8; 16]) -> Result<Vec<u8>> {
|
||||
if input.len() % 16 != 0 {
|
||||
return Err(anyhow!("Invalid input length"));
|
||||
}
|
||||
let mut cipher: Vec<u8> = Vec::new();
|
||||
let round_keys = expand_key(key);
|
||||
let mut prev_block = *iv;
|
||||
for i in 0..(input.len() / 16) {
|
||||
let mut block: [u8; 16] = input[(i * 16)..(i * 16 + 16)].try_into()?;
|
||||
block = block
|
||||
.iter()
|
||||
.zip(prev_block.iter())
|
||||
.map(|(b, iv)| b ^ iv)
|
||||
.collect::<Vec<u8>>()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
block = add_round_key(&block, &round_keys[0]);
|
||||
for round_key in round_keys.iter().take(10).skip(1) {
|
||||
block = sub_bytes(&block);
|
||||
block = shift_rows(&block);
|
||||
block = mix_columns(&block);
|
||||
block = add_round_key(&block, round_key);
|
||||
}
|
||||
block = sub_bytes(&block);
|
||||
block = shift_rows(&block);
|
||||
block = add_round_key(&block, &round_keys[10]);
|
||||
cipher.extend(block);
|
||||
prev_block = block;
|
||||
}
|
||||
|
||||
Ok(cipher)
|
||||
}
|
||||
|
||||
fn aes_cbc_dec(input: &[u8], key: &[u8; 16], iv: &[u8; 16]) -> Result<Vec<u8>> {
|
||||
if input.len() % 16 != 0 {
|
||||
return Err(anyhow!("Invalid input length"));
|
||||
}
|
||||
|
||||
let mut plaintext: Vec<u8> = Vec::new();
|
||||
let round_keys = expand_key(key);
|
||||
let mut prev_block = *iv;
|
||||
for i in 0..(input.len() / 16) {
|
||||
let mut block: [u8; 16] = input[(i * 16)..(i * 16 + 16)].try_into()?;
|
||||
block = add_round_key(&block, &round_keys[10]);
|
||||
block = inv_shift_rows(&block);
|
||||
block = inv_sub_bytes(&block);
|
||||
for j in 0..9 {
|
||||
block = add_round_key(&block, &round_keys[9 - j]);
|
||||
block = inv_mix_columns(&block);
|
||||
block = inv_shift_rows(&block);
|
||||
block = inv_sub_bytes(&block);
|
||||
}
|
||||
block = add_round_key(&block, &round_keys[0]);
|
||||
block = block
|
||||
.iter()
|
||||
.zip(prev_block.iter())
|
||||
.map(|(b, iv)| b ^ iv)
|
||||
.collect::<Vec<u8>>()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
plaintext.extend(block);
|
||||
prev_block = input[(i * 16)..(i * 16 + 16)].try_into()?;
|
||||
}
|
||||
|
||||
Ok(plaintext)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let key = "YELLOW SUBMARINE".as_bytes();
|
||||
let iv = [0u8; 16];
|
||||
let file_path = "./problems/p10/10.txt";
|
||||
let b64_cipher = read_to_string(file_path).expect("Failed to read file");
|
||||
let cipher = STANDARD
|
||||
.decode(b64_cipher.trim().replace('\n', ""))
|
||||
.expect("Failed to decode base64");
|
||||
let key_array: [u8; 16] = key.try_into().expect("Key must be 16 bytes long");
|
||||
let decrypted = aes_cbc_dec(&cipher, &key_array, &iv).expect("Decryption failed");
|
||||
let decrypted_str = String::from_utf8(decrypted).expect("Failed to convert to string");
|
||||
println!("Decrypted text: {decrypted_str}");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_aes_cbc_enc_dec() {
|
||||
let key: [u8; 16] = [0x00; 16];
|
||||
let iv: [u8; 16] = [0x00; 16];
|
||||
let plaintext: Vec<u8> = vec![0x01; 32]; // 2 blocks of 16 bytes
|
||||
|
||||
let ciphertext = aes_cbc_enc(&plaintext, &key, &iv).unwrap();
|
||||
let decrypted = aes_cbc_dec(&ciphertext, &key, &iv).unwrap();
|
||||
assert_eq!(decrypted, plaintext);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_aes_cbc_enc_with_openssl_cmd() {
|
||||
use std::io::Write;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
let key: [u8; 16] = [0x00; 16];
|
||||
let iv: [u8; 16] = [0x00; 16];
|
||||
let plaintext: Vec<u8> = vec![0x01; 32]; // 2 blocks of 16 bytes
|
||||
|
||||
// Encrypt using our implementation
|
||||
let ciphertext = aes_cbc_enc(&plaintext, &key, &iv).unwrap();
|
||||
|
||||
// Call openssl with stdin
|
||||
let mut child = Command::new("openssl")
|
||||
.args([
|
||||
"enc",
|
||||
"-aes-128-cbc",
|
||||
"-K",
|
||||
&hex::encode(key),
|
||||
"-iv",
|
||||
&hex::encode(iv),
|
||||
"-nopad",
|
||||
])
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute openssl");
|
||||
|
||||
let mut stdin = child.stdin.take().expect("failed to get stdin");
|
||||
stdin.write_all(&plaintext).unwrap();
|
||||
drop(stdin);
|
||||
|
||||
let output = child.wait_with_output().unwrap();
|
||||
assert_eq!(ciphertext, output.stdout);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user