cryptopals

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

1-5.rs (783B)


      1 fn main() -> Result<(), Box<dyn std::error::Error>> {
      2     println!("Enter key:");
      3     let mut key = String::new();
      4     std::io::stdin().read_line(&mut key)?;
      5     let key_bytes = key.into_bytes();
      6     let mut crypted: Vec<Vec<u8>> = Vec::new();
      7     for _ in 0..2 {
      8         let mut input = String::new();
      9         std::io::stdin().read_line(&mut input)?;
     10         crypted.push(xor(&input.into_bytes(), &key_bytes));
     11     }
     12     println!("");
     13     for c in &crypted {
     14         println!("{}", bytes_to_hex(c));
     15     }
     16     Ok(())
     17 }
     18 
     19 fn bytes_to_hex(bytes: &[u8]) -> String {
     20     bytes.iter().map(|b| format!("{:02x}", b)).collect()
     21 }
     22 fn xor(first: &[u8], second: &[u8]) -> Vec<u8> {
     23     first
     24         .iter()
     25         .zip(second.iter().cycle())
     26         .map(|(a, b)| a ^ b)
     27         .collect()
     28 }