refactor: 统一workspace依赖管理和完成p12 ECB字节逐步解密
- 在根Cargo.toml中添加workspace.dependencies统一管理版本 - 所有问题crate改用workspace依赖声明 - 为p12添加依赖并实现ECB字节逐步解密攻击 - 添加cryptopal_book/.gitignore忽略构建产物
This commit is contained in:
@@ -4,5 +4,5 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
hex = "0.4.3"
|
||||
base64 = "0.22.1"
|
||||
hex = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
|
||||
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
hex = "0.4"
|
||||
base64 = "0.22"
|
||||
anyhow = { workspace = true }
|
||||
hex = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
common = { path = "../../common" }
|
||||
|
||||
@@ -5,4 +5,4 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../../common" }
|
||||
rand = "0.9.1"
|
||||
rand = { workspace = true }
|
||||
|
||||
@@ -4,3 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../../common" }
|
||||
rand = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
@@ -1,3 +1,82 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::process::exit;
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
@@ -4,4 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
anyhow = { workspace = true }
|
||||
|
||||
@@ -4,4 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
hex = "0.4.3"
|
||||
hex = { workspace = true }
|
||||
|
||||
@@ -5,4 +5,4 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../../common/" }
|
||||
hex = "0.4.3"
|
||||
hex = { workspace = true }
|
||||
|
||||
@@ -4,5 +4,5 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
hex = "0.4.3"
|
||||
hex = { workspace = true }
|
||||
common = { path = "../../common/" }
|
||||
|
||||
@@ -5,5 +5,5 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../../common/" }
|
||||
hex = "0.4.3"
|
||||
anyhow = "1.0.98"
|
||||
hex = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
@@ -4,6 +4,6 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
anyhow = "1.0.98"
|
||||
base64 = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
common = { path = "../../common/" }
|
||||
|
||||
@@ -4,6 +4,6 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
hex = "0.4.3"
|
||||
base64 = "0.22.1"
|
||||
anyhow = { workspace = true }
|
||||
hex = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
|
||||
@@ -4,4 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
hex = "0.4.3"
|
||||
hex = { workspace = true }
|
||||
|
||||
Reference in New Issue
Block a user