Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 30, 2021 07:29 pm GMT

CryptoPals Crypto Challenges Using Rust: Fixed XOR

This is Challenge 2 of Cryptopals challenges implemented in Rust language.

Context

Given two hex encoded strings of similar length we have to return xor of it.
XOR (or, Exclusive OR) is a binary operation (like AND, OR) on bits. XOR gives true/1 as when the two inputs differ, otherwise false/0:

|  A  |  B  |XOR(A^B)||-----|-----|--------||  0  |  0  |    0   ||  0  |  1  |    1   ||  1  |  0  |    1   ||  1  |  1  |    0   |

So, theoretically you can convert hex to binary and xor them to get output, like:

10110011 ^ 01101011 = 11011000

To solve the challenge with Rust, we can make use of hex crate to decode hex strings to bytes vec, zip the two vecs, then perform xor byte by byte to get XORed bytes. And finally encode xored bytes to hex:

use hex::{decode, encode};pub fn fixed_xor(hex1: &str, hex2: &str) -> String {    let bytes1 = decode(hex1).unwrap();    let bytes2 = decode(hex2).unwrap();    let xor_bytes: Vec<u8> = bytes1        .iter()        .zip(bytes2.iter())        .map(|(&b1, &b2)| b1 ^ b2)        .collect();    encode(xor_bytes)}

And we're done.

See the code on Github.

Find me on:
Twitter - @heyNvN

naveeen.com


Original Link: https://dev.to/thenvn/cryptopals-crypto-challenges-using-rust-fixed-xor-4e96

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To