feat: 实现多个挑战并改进测试
- 重写p1实现纯Rust的hex到base64转换 - 完成p13 ECB剪切粘贴攻击和破解脚本 - 实现p33 Diffie-Hellman密钥交换算法 - 修复p9的PKCS#7测试用例 - 在common库添加gen_random_key和pkcs7_unpadding函数 - 更新workspace依赖管理
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use rand::prelude::*;
|
||||
|
||||
const SBOX: [u8; 256] = [
|
||||
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
||||
@@ -396,6 +397,22 @@ pub fn pkcs7_padding(data: &mut Vec<u8>, block_size: usize) {
|
||||
data.extend(vec![padding_length as u8; padding_length]);
|
||||
}
|
||||
|
||||
pub fn pkcs7_unpadding(input: &[u8]) -> Result<Vec<u8>> {
|
||||
if input.is_empty() {
|
||||
return Err(anyhow!("Input cannot be empty"));
|
||||
}
|
||||
let padding_length = *input.last().unwrap();
|
||||
if padding_length == 0 || padding_length > input.len() as u8 {
|
||||
return Err(anyhow!("Invalid PKCS#7 padding"));
|
||||
}
|
||||
for &byte in input.iter().rev().take(padding_length as usize) {
|
||||
if byte != padding_length {
|
||||
return Err(anyhow!("Invalid PKCS#7 padding"));
|
||||
}
|
||||
}
|
||||
Ok(input[..input.len() - padding_length as usize].to_vec())
|
||||
}
|
||||
|
||||
pub fn is_ecb(cipher: &[u8]) -> bool {
|
||||
// Check if the input is a valid ECB encrypted data
|
||||
let mut seen_blocks = HashSet::new();
|
||||
@@ -458,3 +475,10 @@ pub fn xor_with_key(input: &[u8], key: &[u8]) -> Result<Vec<u8>> {
|
||||
.map(|(&a, &b)| a ^ b)
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn gen_random_key() -> [u8; 16] {
|
||||
let mut rng = rand::rng();
|
||||
let mut key = [0u8; 16];
|
||||
rng.fill(&mut key);
|
||||
key
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user