Get a Pentest and security assessment of your IT network.

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:

  • Base64: Usually contains letters (A-Z, a-z), numbers (0-9), plus signs (+), forward slashes (/), and sometimes equals signs (=) for padding. It’s often longer than the original data.
  • HEX: Contains only hexadecimal characters (0-9 and A-F). Each byte of data is represented by two hex characters. It will be exactly twice as long as the original data in bytes.

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?

  • Incorrect Encoding: The string might be encoded using a different method entirely (e.g., URL encoding, ASCII85).
  • Corruption: The string may be corrupted or incomplete.
  • Not Encoded: It could just be random data that isn’t meant to be decoded.

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:

Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation