Convert hex to base64

Hi,
I’m new to Julia and want to try solving the cryptopals challenges using Julia: The Cryptopals Crypto Challenges
However, I’m already struggling on the first task to convert hex to base64.

This hex string should be converted to base64

str = ‘49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d’

The result should be:

SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t

What I tried:

julia> str |> hex2bytes |> String
“I’m killing your brain like a poisonous mushroom”

using Base64
julia> str |> hex2bytes |> base64decode
30-element Array{UInt8,1}:
0x22
0x69
0x22
0x96
0x58

0x21
0xae
0x8a
0x26

A second question, what is the best crypto library in julia (similar to pycryptodome in python)? I found mostly unmaintained/deprecated julia libraries for cryptography.

Thanks!

First of all, welcome! You can mark your code as code by using three backticks (```) to create a code block. This makes your paste easier to read.

You’ve got encoding and decoding backwards:

julia> str |> hex2bytes |> base64encode
"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"

Note that base64encode can also take the UInt8 vector directly.

I’m not aware that something like pycryptodome exists in julia, those that do exist are pre-1.0 and haven’t been updated.

There’s AES.jl for directly using AES, which is probably enough for all cryptopals challenges.

EDIT: :fast_parrot:

3 Likes

You want base64encode, not decode:

julia> str |> hex2bytes |> base64encode
"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"

Edit: @Sukera beat me to it :slightly_smiling_face:

2 Likes