Monday, January 29, 2024

[SOLVED] Bash script - decode encoded string to byte array

Issue

I am new to bash scripting and I am writing a small script where I need to decode the encoded string and convert it to a byte array.

my_array=(`echo "dGVzdA==" | base64 -d`)

when I print the size of the array it's showing as 1 instead of 4.

echo "${#my_array[*]}"

Question - will not base64 -d convert to a byte array? There is an equivalent java code where it does for me:

byte [] b = Base64.getDecoder().decode("dGVzdA==");

Any thoughts?


Solution

to byte array

There are no "byte arrays" in shells. You can basically choose from two types of variables:

  • a string
  • an array of strings (bash extension, not available on all shells)

How you interpret the data is up to you, the shell can only store strings. Bash has an extension called "bash arrays" that allows to create an array. Note that strings are zero terminated - it's impossible to store the zero byte.

Question - will it not base64 -d converts into byte array ?

The string dGVzdA== is base64 encoded string test. So you can convert the string dGVzdA== to the string test:

$ echo "dGVzdA==" | base64 -d
test# no newline is printed after test, as the newline is not in input

Any thoughts ?

You could convert the string test to a string that contains the representation of each byte as hexadecimal number, typically with xxd:

$ echo "dGVzdA==" | base64 -d | xxd -p
74657374

Now you could insert a newline after each two characters in the string and read the output as a newline separated list of strings into an array:

$ readarray -t array < <(echo "dGVzdA==" | base64 -d | xxd -p -c1)
$ declare -p array
declare -a array=([0]="74" [1]="65" [2]="73" [3]="74")

Each element of the array is one byte in the input represented as a string that contains a number in base16. This may be as close as you can get to a "byte array".



Answered By - KamilCuk
Answer Checked By - Mary Flores (WPSolving Volunteer)