Blog | G5 Cyber Security

Decode Base64 or HEX?

TL;DR

Figure out if a string is Base64 or HEX encoded and decode it using common tools.

1. Check the String Format

First, look at the string itself. This often gives you clues:

Example:

Base64 Example: SGVsbG8gV29ybGQ=
HEX Example: 48656c6c6f20576f726c64

2. Try Decoding as Base64

Use a command-line tool or online decoder to attempt decoding the string as Base64.

Using Command Line (Linux/macOS)

The base64 command is commonly available:

echo "SGVsbG8gV29ybGQ=" | base64 -d

If the decoding succeeds and produces readable output, it’s likely Base64.

Using Python

import base64

try:
    decoded_bytes = base64.b64decode("SGVsbG8gV29ybGQ=")
    decoded_string = decoded_bytes.decode("utf-8")
    print(decoded_string) # Output: Hello World
except Exception as e:
    print(f"Base64 decoding failed: {e}")

3. Try Decoding as HEX

If Base64 decoding fails, try decoding the string as hexadecimal.

Using Command Line (Linux/macOS)

You can use xxd -r or a similar tool:

echo "48656c6c6f20576f726c64" | xxd -r -p

Using Python

try:
    decoded_bytes = bytes.fromhex("48656c6c6f20576f726c64")
    decoded_string = decoded_bytes.decode("utf-8")
    print(decoded_string) # Output: Hello World
except Exception as e:
    print(f"HEX decoding failed: {e}")

4. What if Both Fail?

If both attempts fail, investigate the source of the string for more information about its intended encoding.

5. Online Decoders

Many online tools can decode Base64 and HEX strings:

Exit mobile version