# Problems properly decoding a base64 encoded string

**URL:** <https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185>\
**Category:** Questions\
**Tags:** decoding\
**Created:** [January 6, 2019, 1:41am UTC](https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185 "2019-01-06T01:41:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeramyRR](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeramyrr/32/722_2.png) [@jeramyRR](https://forum.elixirforum.com/u/jeramyRR)\
**Post date:** [January 6, 2019, 1:41am UTC](https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185/1 "2019-01-06T01:41:41Z")

</div>

I am trying to decode an Azure Storage Key that’s supposedly base64 encoded, but I’m getting all kinds of wonkey characters. Example:

```elixir
Base.decode64! "ZG2p8/z2ATu8NVLBXmeFtkX9chpkbk2EK9fH4qVKxvC//mSlIdsqzvZpNa4k7tPPR86RmiGkdi0BLwAEtPbkXA==" |> String.codepoints

```

Results in:

```elixir
[
  "d",
  "m",
  <<169>>,
  <<243>>,
  <<252>>,
  <<246>>,
  <<1>>,
  ";",
  <<188>>,
  "5",
  "R",
  <<193>>,
  "^",
  "g",
  <<133>>,
  <<182>>,
  "E",
  <<253>>,
  "r",
  <<26>>,
  "d",
  "n",
  "M",
  <<132>>,
  "+",
  <<215>>,
  <<199>>,
  <<226>>,
  <<165>>,
  "J",
  <<198>>,
  <<240>>,
  <<191>>,
  <<254>>,
  "d",
  <<165>>,
  "!",
  <<219>>,
  "*",
  <<206>>,
  <<246>>,
  "i",
  "5",
  <<174>>,
  "$",
  <<238>>,
  <<211>>,
  <<207>>,
  "G",
  "Α",
  ...
]

```

This definitely isn’t correct, or I’m not thinking about something correctly here. Is there something I need to do special for utf8 encoded strings?

Thanks for the help,  
Jeramy

---

<div class="post-metadata">

**Author:** ![massimo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/massimo/32/5555_2.png) [@massimo](https://forum.elixirforum.com/u/massimo)\
**Post date:** [January 6, 2019, 2:08am UTC](https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185/2 "2019-01-06T02:08:14Z")

</div>

It’s correct

the decoded string contains those characters

```
echo "ZG2p8/z2ATu8NVLBXmeFtkX9chpkbk2EK9fH4qVKxvC//mSlIdsqzvZpNa4k7tPPR86RmiGkdi0BLwAEtPbkXA==" | base64 -D -i - | hexdump -c
0000000 d m � � � � 001 ; � 5 R � ^ g 205 �
0000010 E � r 032 d n M 204 + � � � � J � �
0000020 � � d � ! � * � � i 5 � $ � � �
0000030 G � 221 232 ! � v - 001 / \0 004 � � � \
0000040

```

---

<div class="post-metadata">

**Author:** ![jeramyRR](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeramyrr/32/722_2.png) [@jeramyRR](https://forum.elixirforum.com/u/jeramyRR)\
**Post date:** [January 6, 2019, 2:18am UTC](https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185/3 "2019-01-06T02:18:11Z")

</div>

I’m unable to use the decoded string to reencode other strings it seems. When I try to create a signature using :crypto.hmac(:sha256, decoded\_shared\_key, “This is a test”) I get back gibberish again.

```elixir
shared_key = "ZG2p8/z2ATu8NVLBXmeFtkX9chpkbk2EK9fH4qVKxvC//mSlIdsqzvZpNa4k7tPPR86RmiGkdi0BLwAEtPbkXA=="
decoded = Base.decode64! shared_key
:crypto.hmac(:sha256, decoded, "This is a test")
<<22, 6, 194, 235, 170, 235, 226, 199, 1, 90, 191, 4, 222, 153, 97, 47, 90, 238,
  217, 126, 252, 23, 12, 117, 221, 127, 150, 78, 14, 236, 193, 141>>

```

That binary returned again contains invalid characters that aren’t recognized on the other side of my http request.

This is baffling me, because none of the other azure sdks (ruby, java, go, c#) have any problem with these keys.

Even using [https://www.base64decode.org/](https://www.base64decode.org/) comes out fine.

base64decode results: dm;5R^gErdnM+Jd!\*i5$GΑ!v-/\

---

<div class="post-metadata">

**Author:** ![jeramyRR](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeramyrr/32/722_2.png) [@jeramyRR](https://forum.elixirforum.com/u/jeramyRR)\
**Post date:** [January 6, 2019, 2:24am UTC](https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185/4 "2019-01-06T02:24:25Z")

</div>

Looks like I may be able to remove all unprintable characters, but I’m not sure that’s going to work as far as Azure looking for the fully signed string using the full decoded shared key.

---

<div class="post-metadata">

**Author:** ![jeramyRR](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeramyrr/32/722_2.png) [@jeramyRR](https://forum.elixirforum.com/u/jeramyRR)\
**Post date:** [January 6, 2019, 3:59am UTC](https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185/5 "2019-01-06T03:59:41Z")

</div>

We can close this thread. I got it figured out. There was nothing wrong with the key. There was however a mistake in my spelling.

---

<div class="post-metadata">

**Author:** ![massimo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/massimo/32/5555_2.png) [@massimo](https://forum.elixirforum.com/u/massimo)\
**Post date:** [January 6, 2019, 1:18pm UTC](https://forum.elixirforum.com/t/problems-properly-decoding-a-base64-encoded-string/19185/6 "2019-01-06T13:18:48Z")

</div>

[base64decode.org](http://base64decode.org) is simply removing the non printable characters.  
try on this one and see the difference [https://codebeautify.org/base64-decode](https://codebeautify.org/base64-decode)
