Base64 is not encryption
Base64 turns bytes into text that travels anywhere. It hides nothing: here is what it really does, and where it misleads.
What Base64 is for
Many channels carry only text: the body of an email, an HTTP header, a JSON or XML document, a configuration line.
Arbitrary bytes, those of an image, a key or a compressed file, would be damaged there: a null byte truncates a
string, a line break gets rewritten, a control character is rejected. Base64, defined in RFC 4648, writes those
bytes with 64 characters that travel safely anywhere: A–Z, a–z,
0–9, + and /.
How it works: 6 bits per character
Since 64 = 2⁶, each character carries 6 bits. Base64 therefore takes the bytes three at a time, 24 bits, and splits them into four groups of 6 bits, each becoming one character of the alphabet. The word “Man”:
Text M a n
Bytes 01001101 01100001 01101110
6 bits 010011 010110 000101 101110
Values 19 22 5 46
Base64 T W F u
Three bytes become four characters: the output is about 33% larger than the input, slightly more when it is wrapped into lines. A 3 MB attachment therefore takes about 4 MB in the message.
The “=” padding
When the length is not a multiple of three, the last group is incomplete. Standard Base64 then pads the result with
one or two = signs, back to a multiple of four characters:
M → TQ==
Ma → TWE=
Man → TWFu
The = carries no data: it only tells how many bytes the last group is missing. Many decoders, including
the kit's, do without it.
Where you meet it
- Email attachments: MIME (RFC 2045) encodes files in Base64, flagged by
Content-Transfer-Encoding: base64, in lines of at most 76 characters. data:URIs: a small image is embedded directly in HTML or CSS, asdata:image/png;base64,iVBORw0KGgo…. Every encoded PNG image starts withiVBORw0KGgo, the translation of the format's signature.- Basic Auth: the
Authorization: Basicheader holds the username and password, joined by a colon and Base64-encoded. - JWT: the header and payload of a token are JSON encoded in Base64url.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9decodes to{"alg":"HS256","typ":"JWT"}, which is why nearly every JWT starts witheyJ.
Base64url, for addresses
In a URL, + is often read as a space, / separates path segments and =
separates a parameter from its value. Base64url (RFC 4648, section 5) replaces + with -
and / with _, and usually omits the padding. The bytes are the same; only the alphabet
changes:
Base64 Pz8/ aT8+
Base64url Pz8_ aT8-
A strict decoder rejects the variant it does not expect: when a token is refused, checking the variant is a good first step. The kit's Base64 decoder accepts both.
Base64 is not encryption
Encryption uses a key: without it, the text stays out of reach. Base64 has no key; the algorithm is public, the same for everyone, and anyone can decode in both directions. Its unreadable look gives a false sense of protection. Take a header spotted in a log:
Authorization: Basic YWRtaW46UzNjcmV0IQ==
One command is enough to read it:
$ printf '%s' 'YWRtaW46UzNjcmV0IQ==' | base64 --decode
admin:S3cret!
The username is admin, the password S3cret!. The same goes for a password “hidden” in
Base64 in a configuration file, or for a Kubernetes Secret, whose values are simply Base64-encoded: whoever reads
the file reads the secret.
What to use instead
- To store a secret: a secrets manager (HashiCorp Vault, Azure Key Vault, AWS Secrets Manager…) or the platform's environment variables, outside the Git repository, as explained in the guide on how to generate and store a secret key.
- To encrypt a file or a value: a real keyed algorithm such as AES-GCM, through proven tools like age or GnuPG.
- To check a password without keeping it: a slow, salted hash such as bcrypt or Argon2, as in a file produced by the htpasswd generator.
- For transport: HTTPS, whose TLS encryption protects the header on the way.
Encode and decode on the command line
On Linux and macOS, the base64 command does both. Use printf, which adds no trailing
newline, unlike echo:
# Encode
printf '%s' 'alice:password' | base64
# YWxpY2U6cGFzc3dvcmQ=
# Decode
printf '%s' 'YWxpY2U6cGFzc3dvcmQ=' | base64 --decode
# Encode a file on a single line (Linux)
base64 -w 0 image.png
On Linux, base64 wraps its output at 76 characters; -w 0 keeps it on one line. The long
option --decode works on both Linux and macOS; on Linux, its short form is -d. In
PowerShell:
# Encode
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes('alice:password'))
# Decode
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('YWxpY2U6cGFzc3dvcmQ='))
# Encode a file, with its full path
[Convert]::ToBase64String([IO.File]::ReadAllBytes('C:\temp\image.png'))
[IO.File] resolves a relative path from the process's current directory, which
Set-Location does not change: the full path removes any doubt.
[Convert]::FromBase64String expects the standard variant with its padding: for Base64url, first
replace - with + and _ with /, then pad with =.
Sharing a decoded credential
When the text you have just decoded is a password or a key, hand it over through a channel that does not keep it. With “Send with onlyU”, the decoder passes the result to onlyU from one tab to another in your browser, without putting it in an address or routing it through the kit's server. onlyU encrypts it in your browser and creates a link your recipient opens only once, valid for 24 hours, after which the link disappears. It is free, with no sign-up.