stage
This commit is contained in:
9
problems/p5/Cargo.toml
Normal file
9
problems/p5/Cargo.toml
Normal 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
26
problems/p5/src/main.rs
Normal 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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user