hoseinisalim

hoseinisalim

Convert base64 string to byte string

how to convert base64 string to byte string?

Most Liked

chrismcg

chrismcg

You can turn it into a list:

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

NobbZ

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"
"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)
{:ok,
 {<<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…

NobbZ

NobbZ

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.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement