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:
@@ -4,5 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
hex = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
@@ -1,13 +1,106 @@
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use anyhow::{Ok, Result, anyhow};
|
||||
|
||||
fn hex_to_base64(hex_str: &str) -> String {
|
||||
let bytes = hex::decode(hex_str).expect("解码失败");
|
||||
STANDARD.encode(&bytes)
|
||||
const BASE_TABLE: [char; 64] = [
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
|
||||
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
|
||||
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
|
||||
'5', '6', '7', '8', '9', '+', '/',
|
||||
];
|
||||
|
||||
fn hex_to_base64(hex_str: &str) -> Result<String> {
|
||||
if hex_str.is_empty() {
|
||||
return Ok("".to_string());
|
||||
}
|
||||
let mut hex_str = hex_str.to_lowercase();
|
||||
if hex_str.len() % 2 == 1 {
|
||||
hex_str.insert(0, '0');
|
||||
}
|
||||
let mut hex_bytes: Vec<u8> = Vec::new();
|
||||
|
||||
let mut result = String::new();
|
||||
|
||||
for i in hex_str.as_bytes().chunks(2) {
|
||||
let high = match i[0] {
|
||||
b'0'..=b'9' => i[0] - b'0',
|
||||
b'a'..=b'f' => i[0] - b'a' + 10,
|
||||
b'A'..=b'F' => i[0] - b'A' + 10,
|
||||
_ => return Err(anyhow!("Invalid hex character: {}", i[0] as char)),
|
||||
};
|
||||
let low = match i[1] {
|
||||
b'0'..=b'9' => i[1] - b'0',
|
||||
b'a'..=b'f' => i[1] - b'a' + 10,
|
||||
b'A'..=b'F' => i[1] - b'A' + 10,
|
||||
_ => return Err(anyhow!("Invalid hex character: {}", i[0] as char)),
|
||||
};
|
||||
|
||||
hex_bytes.push(high << 4 | low);
|
||||
}
|
||||
|
||||
for base_bytes in hex_bytes.chunks(3) {
|
||||
match base_bytes.len() {
|
||||
1 => {
|
||||
let index_1 = base_bytes[0] >> 2 & 0x3F;
|
||||
let index_2 = (base_bytes[0] & 0x03) << 4;
|
||||
|
||||
result.push(BASE_TABLE[index_1 as usize]);
|
||||
result.push(BASE_TABLE[index_2 as usize]);
|
||||
result.push('=');
|
||||
result.push('=');
|
||||
}
|
||||
2 => {
|
||||
let index_1 = base_bytes[0] >> 2 & 0x3F;
|
||||
let index_2 = (base_bytes[0] & 0x03) << 4 | (base_bytes[1] >> 4) & 0x0f;
|
||||
let index_3 = (base_bytes[1] & 0x0F) << 2;
|
||||
result.push(BASE_TABLE[index_1 as usize]);
|
||||
result.push(BASE_TABLE[index_2 as usize]);
|
||||
result.push(BASE_TABLE[index_3 as usize]);
|
||||
result.push('=');
|
||||
}
|
||||
3 => {
|
||||
let index_1 = base_bytes[0] >> 2 & 0x3F;
|
||||
let index_2 = (base_bytes[0] & 0x03) << 4 | (base_bytes[1] >> 4) & 0x0f;
|
||||
let index_3 = (base_bytes[1] & 0x0F) << 2 | (base_bytes[2] >> 6) & 0x03;
|
||||
let index_4 = base_bytes[2] & 0x3F;
|
||||
result.push(BASE_TABLE[index_1 as usize]);
|
||||
result.push(BASE_TABLE[index_2 as usize]);
|
||||
result.push(BASE_TABLE[index_3 as usize]);
|
||||
result.push(BASE_TABLE[index_4 as usize]);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<()> {
|
||||
// 从十六进制字符串解码为原始字节
|
||||
let hex_str = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
|
||||
let base64_str = hex_to_base64(hex_str);
|
||||
let base64_str = hex_to_base64(hex_str)?;
|
||||
println!("十六进制字符串: {base64_str}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*; // 导入上层的函数
|
||||
#[test]
|
||||
fn test_hex_to_base64_empty() {
|
||||
let hex_str = "";
|
||||
let expected_base64 = "";
|
||||
assert_eq!(hex_to_base64(hex_str).unwrap(), expected_base64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hex_to_base64_single_byte() {
|
||||
let hex_str = "4a";
|
||||
let expected_base64 = "Sg==";
|
||||
assert_eq!(hex_to_base64(hex_str).unwrap(), expected_base64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hex_to_base64_all_bytes() {
|
||||
let hex_str = "000102ff";
|
||||
let expected_base64 = "AAEC/w==";
|
||||
assert_eq!(hex_to_base64(hex_str).unwrap(), expected_base64);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user