# Convert hex to base64

**URL:** https://discourse.julialang.org/t/convert-hex-to-base64/42003
**Category:** New to Julia
**Tags:** convert, cryptography
**Created:** [June 24, 2020, 5:34pm UTC](https://discourse.julialang.org/t/convert-hex-to-base64/42003 "2020-06-24T17:34:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pihive](https://avatars.discourse-cdn.com/v4/letter/p/58f4c7/32.png) [@pihive](https://discourse.julialang.org/u/pihive)
#### Post date: [June 24, 2020, 5:34pm UTC](https://discourse.julialang.org/t/convert-hex-to-base64/42003/1 "2020-06-24T17:34:12Z")

</div>

Hi,  
I’m new to Julia and want to try solving the cryptopals challenges using Julia: [The Cryptopals Crypto Challenges](https://cryptopals.com/sets/1)  
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!

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 24, 2020, 6:06pm UTC](https://discourse.julialang.org/t/convert-hex-to-base64/42003/2 "2020-06-24T18:06:33Z")

</div>

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
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](https://github.com/kanav99/AES.jl) for directly using AES, which is probably enough for all cryptopals challenges.

EDIT: :fast\_parrot:

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 24, 2020, 6:06pm UTC](https://discourse.julialang.org/t/convert-hex-to-base64/42003/3 "2020-06-24T18:06:36Z")

</div>

You want `base64encode`, not `decode`:

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

```

Edit: @Sukera beat me to it 🙂
