Skip to content

Instantly share code, notes, and snippets.

@alexander-hanel
Last active April 20, 2022 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexander-hanel/2e24b53549e04a41a18a50ce8eca958b to your computer and use it in GitHub Desktop.
Save alexander-hanel/2e24b53549e04a41a18a50ce8eca958b to your computer and use it in GitHub Desktop.
Cryptopals Rust Solutions

Cryptopals

link

Set 1

Challenge 1: Convert hex to base64

use std::str;
extern crate base64;

fn main() {
    let input = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
    let decoded = hex::decode(input).expect("Decoding Failed");
    
    println!("{:?}", &decoded); 

    let s = match str::from_utf8(&decoded) {
        Ok(v) => v,
        Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
    };
    let encoded = base64::encode(&s);
    println!("result: {}", encoded);

}

Challenge 2: Fixed XOR

use std::str;

fn main() {
    let key = "1c0111001f010100061a024b53535009181c";
    let data = "686974207468652062756c6c277320657965";
    // convert hex bytes to string 
    let decoded_key = hex::decode(key).expect("key decode failed");
    let decoded_data = hex::decode(data).expect("data decode failed");

    let encoded_bytes : Vec<u8> = decoded_key.iter().zip(decoded_data).map(|(d, k)| d ^ k).collect();

    let ice = match str::from_utf8(&encoded_bytes) {
        Ok(v) => v,
        Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
    };
    let kid_dont_play = hex::encode(ice);
    println!("{:?}", kid_dont_play);    
}

Challenge 3: Single-byte XOR cipher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment