1
0
This commit is contained in:
2025-06-20 11:23:21 +08:00
commit b5601aef8c
47 changed files with 1829 additions and 0 deletions

9
problems/p5/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "p5"
version = "0.1.0"
edition = "2024"
[dependencies]
common = { path = "../../common/" }
hex = "0.4.3"
anyhow = "1.0.98"

26
problems/p5/src/main.rs Normal file
View File

@@ -0,0 +1,26 @@
use anyhow::{Result, anyhow};
fn xor_with_key(input: &[u8], key: &[u8]) -> Result<Vec<u8>> {
if key.is_empty() {
return Err(anyhow!("empty key"));
}
Ok(input
.iter()
.zip(key.iter().cycle())
.map(|(&a, &b)| a ^ b)
.collect())
}
fn main() -> Result<()> {
let plaintexts = r#"
Burning 'em, if you ain't quick and nimble I go crazy when I hear a cymbal
"#;
let key = b"ICE";
for plaintext in plaintexts.lines() {
let plaintext_bytes = plaintext.as_bytes();
let cipher = xor_with_key(plaintext_bytes, key)?;
println!("{}", hex::encode(cipher));
}
Ok(())
}