stage
This commit is contained in:
26
problems/p4/src/main.rs
Normal file
26
problems/p4/src/main.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use common::is_valid_english;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let ciphers = fs::read_to_string("./problems/p4/4.txt")?;
|
||||
let cipher: String = ciphers.lines().collect();
|
||||
|
||||
let cipher_bytes = hex::decode(cipher).expect("decode error");
|
||||
let window_size = 20;
|
||||
for key in 0..(1 << 8) {
|
||||
let key: u8 = key as u8;
|
||||
// 对密文的每个字节应用异或操作,得到明文
|
||||
let plaintext: Vec<u8> = cipher_bytes.iter().map(|&byte| byte ^ key).collect();
|
||||
for i in 0..=(plaintext.iter().len() - window_size) {
|
||||
if let Ok(text) = std::str::from_utf8(&plaintext[i..i + window_size]) {
|
||||
if is_valid_english(text, None) {
|
||||
println!("Found valid sentence with key {key}: {}", text);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 尝试将字节转换为字符串
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user