1
0

refactor: 统一workspace依赖管理和完成p12 ECB字节逐步解密

- 在根Cargo.toml中添加workspace.dependencies统一管理版本
- 所有问题crate改用workspace依赖声明
- 为p12添加依赖并实现ECB字节逐步解密攻击
- 添加cryptopal_book/.gitignore忽略构建产物
This commit is contained in:
2025-07-31 17:46:22 +08:00
parent 6f54d41c8e
commit 23d016407c
16 changed files with 118 additions and 22 deletions

10
Cargo.lock generated
View File

@@ -90,6 +90,12 @@ dependencies = [
[[package]] [[package]]
name = "p12" name = "p12"
version = "0.1.0" version = "0.1.0"
dependencies = [
"anyhow",
"base64",
"common",
"rand",
]
[[package]] [[package]]
name = "p13" name = "p13"
@@ -214,9 +220,9 @@ checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.9.1" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [ dependencies = [
"rand_chacha", "rand_chacha",
"rand_core", "rand_core",

View File

@@ -5,3 +5,9 @@ edition = "2024"
[workspace] [workspace]
members = ["problems/*", "common"] members = ["problems/*", "common"]
[workspace.dependencies]
hex = "0.4.3"
base64 = "0.22.1"
anyhow = "1.0.98"
rand = "0.9.2"

1
cryptopal_book/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
book

View File

@@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
hex = "0.4.3" hex = { workspace = true }
base64 = "0.22.1" base64 = { workspace = true }

View File

@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = { workspace = true }
hex = "0.4" hex = { workspace = true }
base64 = "0.22" base64 = { workspace = true }
common = { path = "../../common" } common = { path = "../../common" }

View File

@@ -5,4 +5,4 @@ edition = "2024"
[dependencies] [dependencies]
common = { path = "../../common" } common = { path = "../../common" }
rand = "0.9.1" rand = { workspace = true }

View File

@@ -4,3 +4,7 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
common = { path = "../../common" }
rand = { workspace = true }
base64 = { workspace = true }
anyhow = { workspace = true }

View File

@@ -1,3 +1,82 @@
fn main() { use std::process::exit;
println!("Hello, world!");
use anyhow::Result;
use base64::{Engine, engine::general_purpose::STANDARD};
use common::{aes_ecb_enc, pkcs7_padding};
use rand::{prelude::*, rand_core::block};
fn gen_random_key() -> [u8; 16] {
let mut rng = rand::rng();
let mut key = [0u8; 16];
rng.fill(&mut key);
key
}
fn oracle(controlled_input: &[u8], key: &[u8; 16], unknown_string: &[u8]) -> Vec<u8> {
let mut data = Vec::new();
data.extend_from_slice(controlled_input);
data.extend_from_slice(unknown_string);
pkcs7_padding(&mut data, 16);
aes_ecb_enc(&data, key).unwrap()
}
fn main() -> Result<()> {
let b64_unknown_string = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK";
let unknown_string = STANDARD.decode(b64_unknown_string)?;
let key = gen_random_key();
// let plaintext = Vec::new();
// step1. find padding length and unknown_string length.
let mut padding_len = 0;
let prev_cipher = oracle(&vec![0; padding_len], &key, &unknown_string);
while prev_cipher.len() + 16 != oracle(&vec![0; padding_len], &key, &unknown_string).len() {
padding_len += 1;
}
println!("padding_len: {padding_len}"); // 6
let unknown_str_len = prev_cipher.len() - padding_len;
println!("unknown_str_len: {unknown_str_len}");
assert_eq!(unknown_str_len, unknown_string.len()); // debug use
let mut cracked: Vec<u8> = Vec::new();
let mut flag = false;
while cracked.len() != unknown_str_len {
padding_len += 1;
for i in 0..=255u8 {
let mut block = Vec::new();
block.push(i);
block.extend_from_slice(&cracked[..cracked.len().min(15)]);
pkcs7_padding(&mut block, 16);
block.extend(b"1".repeat(padding_len));
let cipher = oracle(&block, &key, &unknown_string);
let chunks: Vec<&[u8]> = cipher.chunks(16).collect();
let first_16 = chunks[0];
let need_crack_16 = chunks[chunks.len() - 1 - ((cracked.len() + 1) / 16)];
if first_16 == need_crack_16 {
cracked.insert(0, i);
flag = true;
break;
}
}
if flag {
flag = false;
} else {
println!("Error. Some byte not found.");
println!("Cerrent length: {}", cracked.len());
exit(0)
}
}
println!("{}", String::from_utf8_lossy(&cracked));
// println!("{}", String::from_utf8_lossy(&plaintext));
assert_eq!(cracked, unknown_string);
Ok(())
} }

View File

@@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
anyhow = "1.0.98" anyhow = { workspace = true }

View File

@@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
hex = "0.4.3" hex = { workspace = true }

View File

@@ -5,4 +5,4 @@ edition = "2024"
[dependencies] [dependencies]
common = { path = "../../common/" } common = { path = "../../common/" }
hex = "0.4.3" hex = { workspace = true }

View File

@@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
hex = "0.4.3" hex = { workspace = true }
common = { path = "../../common/" } common = { path = "../../common/" }

View File

@@ -5,5 +5,5 @@ edition = "2024"
[dependencies] [dependencies]
common = { path = "../../common/" } common = { path = "../../common/" }
hex = "0.4.3" hex = { workspace = true }
anyhow = "1.0.98" anyhow = { workspace = true }

View File

@@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
base64 = "0.22.1" base64 = { workspace = true }
anyhow = "1.0.98" anyhow = { workspace = true }
common = { path = "../../common/" } common = { path = "../../common/" }

View File

@@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
anyhow = "1.0.98" anyhow = { workspace = true }
hex = "0.4.3" hex = { workspace = true }
base64 = "0.22.1" base64 = { workspace = true }

View File

@@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
hex = "0.4.3" hex = { workspace = true }