Base64 Encode & Decode in Perl

Encode or decode below — it runs locally in your browser — then grab the canonical Perl code. Perl's core MIME::Base64 module ships with every install, so encode_base64 and decode_base64 work with no dependencies.

INPUT
OUTPUT
Type or paste to encode / decode

Encode and decode a string

Import MIME::Base64, then encode with an empty second argument to keep the output on one line:

use MIME::Base64;

# Encode — pass "" as the second arg for a single unbroken line
my $encoded = encode_base64("Hello, world!", "");
print $encoded;  # SGVsbG8sIHdvcmxkIQ==

# Decode
my $decoded = decode_base64($encoded);
print $decoded;  # Hello, world!

Tip: By default encode_base64 inserts a newline every 76 characters and appends a trailing "\n". Pass an empty string as the second argument — encode_base64($data, "") — to get a single unbroken line.

One-liners

Encode and decode straight from the shell with perl -M, no script file needed:

# Encode
perl -MMIME::Base64 -e 'print encode_base64("Hello, world!", "")'

# Decode (reads each line from stdin)
perl -MMIME::Base64 -ne 'print decode_base64($_)'

URL-safe Base64

For JWTs, URLs, and filenames, MIME::Base64 exports URL-safe helpers on request. They use the - and _ alphabet with no padding:

use MIME::Base64 qw(encode_base64url decode_base64url);

my $url = encode_base64url("Hello, world!");
print $url;  # SGVsbG8sIHdvcmxkIQ

my $back = decode_base64url($url);
print $back;  # Hello, world!

Frequently asked questions

How do I Base64 encode a string in Perl?

Use the core MIME::Base64 module: encode_base64("Hello, world!", "") returns SGVsbG8sIHdvcmxkIQ==. The empty second argument keeps it on one line.

How do I Base64 decode in Perl?

decode_base64("SGVsbG8sIHdvcmxkIQ==") returns the original Hello, world!; it ignores whitespace, so wrapped input decodes fine.

How do I do URL-safe Base64?

Import the helpers: use MIME::Base64 qw(encode_base64url decode_base64url);. They use the -/_ alphabet with no padding.

Why does my Perl Base64 have line breaks?

encode_base64 wraps at 76 characters and adds a trailing newline unless you pass "" as the second argument.

Need image, file, or URL-safe modes?

The main base64.dev tool handles text, images, files, and URL-safe Base64 with auto-detect.

Open base64.dev →