1

I have following commands in linux as, base64 encoded string 'rITqHhVbOjGIWelaJbg==' in file test64.dat. used commands -

# base64 -d -i test64.dat >> test.dat
# echo "IV=" `hexdump test.dat | head -1 | cut -f 2- -d ' ' | sed s/' '//g ` >> abc.txt
# cat abc.txt
IV= 84ac1eea7515e86c21c6a5676f6b6ec9

I want to use python base64 module for decoding same string as in test64.dat, i tried below code

hexdump.dump(base64.b64decode('rITqHhVbOjGIWelaJbg==')) 

It gives output as.

'AC 84 EA 1E 15 5B 3A 31 88 59 E9 5A 25 B8'

Can anyone please tell me why base64 decoded output is different, am i doing something wrong?

3
  • 1
    Your padding is broken, to start with - you should only have one = at the end, to end up with a multiple of 4 characters as input. At that point, the Python output is correct. Commented Apr 18, 2016 at 6:20
  • Thanks Jon for replying but i m bit confused, does it mean that i will have to write a loop in python which will decode 4 characters at a time? Commented Apr 18, 2016 at 6:32
  • Well you should start by fixing the input. Part of the discrepancy is that the Linux output includes "base64: invalid input" Commented Apr 18, 2016 at 7:09

1 Answer 1

2

There are two issues here:

1) Your input is invalid. It has too many = at the end, so the Linux output includes "base64: invalid input" which you're merrily decoding as hex afterwards.

2) hexdump isn't giving you the format you really want; it's treating it as a sequence of pairs of bytes, and reversing each pair. For example, input of "ABCD" ends up with default output of "4241 4443" - note the ordering of 42 before 41, and 44 before 43.

Once you've fixed the input, you can pass an output format to hexdump to give you the format you want

echo rITqHhVbOjGIWelaJbg= | base64 -d | hexdump -e '16/1 "%02X " "\n"'

Output:

AC 84 EA 1E 15 5B 3A 31 88 59 E9 5A 25 B8
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jon for the reply, it helped a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.