Problems properly decoding a base64 encoded string

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

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

Results in:

[
  "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

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
1 Like

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.

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/ comes out fine.

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

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.

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.

2 Likes

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