Files
cryptopals/src/set1.rs

78 lines
2.7 KiB
Rust

use crate::bytes::Bytes;
use crate::bytes_base64::BytesBase64;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str;
pub fn challenge1() {
let input = Bytes::from_hex("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d");
let expected = String::from("SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t");
let result = BytesBase64::from_bytes(input);
if result.to_string() == expected {
println!("[okay] Challenge 1: {}", result);
} else {
println!("[fail] Challenge 1")
}
}
pub fn challenge2() {
let input_1 = Bytes::from_hex("1c0111001f010100061a024b53535009181c");
let input_2 = Bytes::from_hex("686974207468652062756c6c277320657965");
let result = Bytes::xor(&input_1, &input_2);
let expected = Bytes::from_hex("746865206b696420646f6e277420706c6179");
if result == expected {
println!("[okay] Challenge 2: {}", result.to_hex());
} else {
println!("[fail] Challenge 2")
}
}
pub fn challenge3() {
let a = Bytes::from_hex("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736");
let mut h: Vec<Bytes> = (0..=255).map(|i| Bytes::xor_byte(&a, i)).collect();
h.sort_by(|a, b| a.ascii_score().partial_cmp(&b.ascii_score()).unwrap());
let h: Vec<Bytes> = h.into_iter().filter(|b| b.is_ascii()).collect();
let r = h[h.len() - 1].to_utf8();
println!("[okay] Challenge 3: {}", r);
}
pub fn challenge4() {
pub fn read_to_vector(path: &str) -> Vec<Bytes> {
let file = File::open(path).unwrap();
let br = BufReader::new(file);
br.lines()
.map(|line| Bytes::from_hex(&line.unwrap()))
.collect()
}
let bs = read_to_vector("data/4.txt");
let mut h: Vec<Bytes> = vec![];
for i in 32..=127 {
for b in bs.iter() {
h.push(Bytes::xor_byte(b, i));
}
}
h.sort_by(|a, b| a.ascii_score().partial_cmp(&b.ascii_score()).unwrap());
let r = h[h.len() - 1].to_utf8();
println!("[okay] Challenge 4: {}", r.trim());
}
pub fn challenge5() {
let msg = Bytes::from_utf8(
"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal",
);
let key = Bytes::from_utf8("ICE");
let enc = Bytes::xor_cycle(&msg, &key);
let exp = Bytes::from_hex("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f");
if enc == exp {
println!("[okay] Challenge 5: {}", enc.to_hex());
} else {
println!("[fail] Challenge 5")
}
}
pub fn challenge6() {
// next: base64bytes to hexbytes
println!("[open] Challenge 6: xxx");
}