Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script - decode encoded string to byte array

Tags:

linux

bash

shell

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?

like image 250
Madhu CM Avatar asked Oct 19 '25 02:10

Madhu CM


2 Answers

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

like image 67
KamilCuk Avatar answered Oct 21 '25 22:10

KamilCuk


#!/bin/bash

read -r -a my_array < <(echo 'dGVzdA==' | base64 -d | sed 's/./& /g')
echo "${#my_array[@]}"
like image 23
vgersh99 Avatar answered Oct 21 '25 23:10

vgersh99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!