Encoding - decoding Base64 on Windows, Linux and Mac

When you urgently need to encode file to base64

Page content

Here are examples how to Encode and Decode file to Base64 on Windows, Linux and Mac.

bearded man is doing some advanced coding at night

Convert file to base64 on Windows

To convert an ordinary file to Base64 on Windows, one can use the following methods:

Using Command Prompt with Certutil

  1. Open Command Prompt.

  2. Run the command:

    certutil -encode  
    

    Replace with your file's path and with the desired output file name (e.g., encoded.b64).

  3. To display the Base64 content directly on the screen without saving to a temporary file:

    certutil -encode  tmp.b64 && findstr /v /c:- tmp.b64 && del tmp.b64
    

    This avoids leaving temporary files by deleting tmp.b64 after processing.


Using PowerShell

  1. Open PowerShell.
  2. Run the command:
    [Convert]::ToBase64String([System.IO.File]::ReadAllBytes("your_file_path"))
    
    Replace "your_file_path" with the full path to your file. This will output the Base64 string directly in the console.

Using a Custom Script

If you need more flexibility, you can use a script like ConvertTo-Base64.ps1. It allows encoding files to Base64 and optionally saving the output to a file or formatting it as JSON.


These methods enable quick and efficient Base64 encoding without requiring additional software installations.

Convert file to base64 on Linux

To convert an ordinary file to Base64 on Linux, you can use the base64 command, which is typically pre-installed on most Linux distributions. This one is so much better compared to Windows! :)

Here’s how to do it:

Basic Syntax

base64 [OPTIONS] [FILE]

Steps to Encode a File

  1. Open a terminal.

  2. Run the following command:

    base64 input_file > output_file
    

    Replace input_file with the path to your file and output_file with the name of the file where you want to save the Base64-encoded content.

    Example:

    base64 myfile.txt > encoded.txt
    
  3. If you want to display the encoded content directly in the terminal:

    base64 input_file
    

Optional Flags

  • -w : Wrap lines after a specified number of characters (default is 76). Use -w 0 for no line wrapping. Example:
    base64 -w 0 input_file > output_file
    

Decode a Base64 File

To decode a Base64-encoded file back to its original format:

base64 --decode encoded_file > decoded_file

This method is efficient and works on most Linux distributions without requiring additional installations.

Encode / Decode base64 on Mac

This one is close to Linux but differs a bit.

To convert an ordinary file to Base64 on macOS, you can use the built-in base64 command. Here’s how:

Steps to Encode a File

  1. Open the Terminal.

  2. Run the following command:

    base64 -i input_file > output_file
    

    Replace input_file with the path to your file and output_file with the name of the file where you want to save the Base64-encoded content.

    Example:

    base64 -i myfile.txt > encoded.txt
    
  3. If you want to display the Base64-encoded content directly in the terminal:

    base64 -i input_file
    

Optional Clipboard Integration

You can encode a file and copy the result directly to your clipboard using:

base64 -i input_file | pbcopy

Decoding a Base64 File

To decode a Base64-encoded file back to its original format:

base64 -D -i encoded_file > decoded_file

These commands are efficient and work on all modern macOS versions starting from 10.7