Base64 is a binary-to-text encoding scheme that encodes binary data (such as images, files, or other non-text data) into an ASCII string format. This is often used to embed binary data into text files, such as in email attachments or HTML/XML documents.
Base64 encoding is useful for transmitting data in environments that only support text, while decoding is used to convert Base64-encoded data back to its original binary format.
Common Base64 Options
--encode
: Encode the input data into Base64 format.--decode
: Decode a Base64-encoded string back to its original data.-w <width>
: Wrap the Base64 output at a specified number of characters per line (useful for long encoded strings). Default is no wrapping.-i <file>
: Specify an input file for encoding or decoding.-o <file>
: Specify an output file for decoding the Base64 data.-d
: Decode from Base64 format. This is the default if usingbase64
on the command line.-b <base64_encoding_file>
: Allows you to encode or decode from a file.
Examples of Base64 Commands
Encoding Examples
- Encode the text “Hello, World!” into Base64:
> echo -n "Hello, World!" | base64
Output:SGVsbG8sIFdvcmxkIQ==
- Encode a file named
image.jpg
into Base64:> base64 -i image.jpg
- Encode a string with line breaks after every 76 characters:
> echo -n "This is a long string that will be encoded in Base64." | base64 -w 76
Decoding Examples
- Decode the Base64 string
SGVsbG8sIFdvcmxkIQ==
back to its original text:> echo "SGVsbG8sIFdvcmxkIQ==" | base64 --decode
Output:Hello, World!
- Decode a Base64-encoded file
encoded.txt
back to its original format:> base64 --decode -i encoded.txt -o decoded_image.jpg
- Decode a Base64 string with special characters:
> echo "U29tZSBleGFtcGxlIHRleHQ=" | base64 --decode
Output:Some example text