Convert base64 string to byte string

how to convert base64 string to byte string?

As there is no native kind of array in elixir, you can’t.

But at least there is the Base module, which allows you to decode base64 encoded stuff to a binary.

1 Like

You can turn it into a list:

iex(11)> "Like this 😀" |> Base.encode64
iex(12)> "TGlrZSB0aGlzIPCfmIA=" |> Base.decode64!() |> :binary.bin_to_list()
[76, 105, 107, 101, 32, 116, 104, 105, 115, 32, 240, 159, 152, 128]

Why should one want that? Most of the time, the binary consumes much less memory, and if you are randomly reading bytes from it, its also much faster. Also writing it to a file is probably faster, as it can be read from continous memory, while writing from a list would mean to follow a pointer for each next byte.

Also binaries are much easier to handle if we want to match on exact bits.

So, do not convert from a binary to a list unless you really have to, and then you should be sure to use the correct conversion function.

1 Like

The only place I have actually used this is when doing the Cryptopals challenges. I personally found it much easier to follow along when I had lists of bytes.

In fact, I created a aes_key_256 with :base64 format in c# program. To be able to use this key for encryption, I need to convert bytes. I dont know convert it

Well, a binary comes very close to a C-like array.

It is a fat pointer with a link to the start of the binary and information about its length.

If you need a binary or a list of bytes or something totally different, I can’t tell, as I do not know where you want to use the decoded information.

But in general, using the functions from the Base module is a starting point and you can transform the raw bytes you get to whatever format you need.

My Code:
message = "A very important message."
{:ok, aes_256_key} = ExCrypto.generate_aes_key(:aes_256, :base64)
{:ok, {init_vec, cipher_text}} = ExCrypto.encrypt(aes_256_key, message)
{:ok, val} = ExCrypto.decrypt(aes_256_key, init_vec, cipher_text)

Get error blew:
(exit) an exception was raised:
** (MatchError) no match of right hand side value: {:error, "IV must be exactly 128 bits and key must be exactly 128, 192 or 256 bits"}

For use in ExCrypto.encrypt/2 you need to generate the key as :bytes.

1 Like

My Problem is here. I need to have the key to be base64.

Have you tried:

{:ok, {init_vec, cipher_text}} = ExCrypto.encrypt(Base.decode64!(aes_256_key), message)


Yes, but did not answer

No other solution?

Looking at the source of the library it uses Base.url_encode64 so try Base.url_decode64!

1 Like

So I tested it before, but did not answer.

What do you mean by “did not answer”?

Mean I get an error:

(exit) an exception was raised:
** (MatchError) no match of right hand side value: {:error, “IV must be exactly 128 bits and key must be exactly 128, 192 or 256 bits”}

I already told you a few times, the documentation of that library is far from par.

Also @chrismcg told you to try Base.url_decode64!/1, which you obviously didn’t, as it works for me:

iex(1)> message = "Foo"
iex(2)> {:ok, key64} = ExCrypto.generate_aes_key(:aes_256, :base64)
{:ok, "YBFgCMzhnxIOVeirWf5hgzbFmUcvZcHY0yKRJJ4Ocks="}
iex(3)> key = Base.url_decode64!(key64)
<<96, 17, 96, 8, 204, 225, 159, 18, 14, 85, 232, 171, 89, 254, 97, 131, 54, 197,
  153, 71, 47, 101, 193, 216, 211, 34, 145, 36, 158, 14, 114, 75>>
iex(4)> {:ok, {iv, ciphered}} = ExCrypto.encrypt(key, message)
 {<<195, 207, 172, 38, 128, 248, 158, 10, 14, 239, 231, 195, 48, 154, 244, 177>>,
  <<4, 238, 11, 86, 7, 6, 5, 86, 13, 182, 189, 113, 193, 85, 194, 141>>}}
iex(5)> {:ok, clear} = ExCrypto.decrypt(key, iv, ciphered)
{:ok, "Foo"}

It seems as if you really need to actively read and understand the sources of this library to be able to use it.

My suggestion is, that you use :bytes with the key generator and then convert the resulting binary to exactly the format you really need. No surprises anymore because of bad documentation, that claims to return base64 encoded string but actually does return an Base64-URL-encoded string…