Skip to content

Instantly share code, notes, and snippets.

@johnptoohey
Forked from rust-play/playground.rs
Created May 1, 2019 19:25
Show Gist options
  • Select an option

  • Save johnptoohey/094a4ffac7856991d477aa9572d5623e to your computer and use it in GitHub Desktop.

Select an option

Save johnptoohey/094a4ffac7856991d477aa9572d5623e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
extern crate rustc_serialize as serialize;
use self::serialize::hex::{FromHex, ToHex};
use std::str;
fn parse_hex(hex_asm: &str) -> Vec<u8> {
let mut hex_bytes = hex_asm
.as_bytes()
.iter()
.filter_map(|b| match b {
b'0'...b'9' => Some(b - b'0'),
b'a'...b'f' => Some(b - b'a' + 10),
b'A'...b'F' => Some(b - b'A' + 10),
_ => None,
})
.fuse();
let mut bytes = Vec::new();
while let (Some(h), Some(l)) = (hex_bytes.next(), hex_bytes.next()) {
bytes.push(h << 4 | l)
}
bytes
}
fn fixed_xor(xor_input: &str, xored_against: &str) -> String {
let xor_input_unhexed: Vec<u8> = xor_input.from_hex().unwrap();
let xored_against_unhexed: Vec<u8> = xored_against.from_hex().unwrap();
if xor_input_unhexed.len() != xored_against_unhexed.len() {
panic!("WTF?");
}
let xored_result: Vec<u8> = xor_input_unhexed
.iter()
.zip(xored_against_unhexed.iter())
.map(|(&x, &y)| x ^ y)
.collect();
xored_result.to_hex()
}
fn main() {
println!("JMA XOR");
// Input
let xor_input = "56424875616b7a5a59585b51745b5e424942445e061a5d5f5e525b51594d1b525a59";
// Key
let xored_against = "37323833363835343531353434383130393130393734373238333638353435313534";
// Cipher
let xored_output = fixed_xor(xor_input, xored_against);
println!("Cipher: {:?}", xored_output);
let j = parse_hex(&xored_output);
let plain_text = str::from_utf8(&j).unwrap();
println!("Plain Text: {:?}", plain_text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment