Flow Control - Loops (Introduction to Bash Scripting)

Hi all,

I am at my wits end with this Question:

Create a “For” loop that encodes the variable “var” 28 times in “base64”. The number of characters in the 28th hash is the value that must be assigned to the “salt” variable.

No matter what I put, I always get a ‘bad decrypt’ back. My answer is:

for i in {1..28}

do

    var=$(echo $var | base64)
    
    if [[ $i == 28 ]]
    then
        salt=$(echo $var | wc -c)
    fi    
done
...

Does anyone have any insight to this. I have looked at StackOverFlow and on here, and there is nothing that insightful, apart from people saying that the questions is poorly worded, but thats to be expected with HTB at this point ha!! 

Any help or light anyone can shed on this, would be highly appreciated xx
$ var=1234
$ echo $var | wc -c
5
$ echo -n $var | wc -c
4
$ echo ${#var}
4

The echo $var writes the content of var and one \n. So wc -c returns not the number of characters in var.

You can use the -n option for output without \n or the bash length function.

2 Likes

I found that the ${#var} method worked the best for me, however I had to add +1 to the value for it to work…
Not sure why that is happening.

Good evening, did you manage to solve it?!
var=“9M”
hash=“VTJGc2RHVmtYMTl2ZnYyNTdUeERVRnBtQWVGNmFWWVUySG1wTXNmRi9rQT0K”
salt=“”

for i in {1…28}; do
var=$(echo $var | base64)
if [ $i -eq 28 ]; then
salt=${#var}
fi
done

echo “salt: $salt”

This is where I stand but I keep getting wrong results…

The command echo XYZ sends XYZ and a newline character to stdout. Therefore, echo $var sends the content of the variable var and a newline character to stdout. But you need only the content of the variable var base64 encoded.

var=“9M”
hash=“VTJGc2RHVmtYMTl2ZnYyNTdUeERVRnBtQWVGNmFWWVUySG1wTXNmRi9rQT0K”
salt=“”

for i in {1…28}; do
var=$(echo var | base64)
if [ $i -eq 28 ]; then
salt=$var
fi
done

echo “salt: $salt”

I get “salt: dmFyCg==” as result but it`s not the correct answer… Ive tried million things…

Are you sure this is code line is correct? You base64 encode the text “var” plus newline. You don’t use the variable named var and you add a newline.

Try using salt=${#var}.

I know what’s wrong there should be only two dots in
for loop like:
for i in {1…28}

and after ${#var} you have to add one to it