Base64 エンコード・デコード チートシート:Windows、Linux、Mac のコマンド例
Windows、Linux、および Mac 上でファイルを Base64 形式でエンコードおよびデコードします。
このチートシートでは、Windows、Linux、macOS のコマンドラインから、追加のツールなしでファイルや文字列を Base64 でエンコードおよびデコードする方法を示します。Base64 エンコードとデコード チートシート。

このガイドは、開発者ツール:モダンな開発ワークフロー完全ガイド の一部です。
Base64 は、64 文字(A–Z、a–z、0–9、+、/)を使用してバイナリデータを ASCII テキストに変換します。HTTP Basic 認証での認証情報のエンコード、JWT ペイロード(JWT トークンのデコードと出力 を参照)、Kubernetes のシークレット、HTML/CSS のデータ URI、JSON や環境変数へのバイナリデータの埋め込みなど、広く使用されています。コマンドラインを使用することで、機密データをローカルに保ち、CI/CD パイプラインでスクリプト化できます。
Windows での Base64 エンコードとデコード
Windows には専用の base64 コマンドは付属していませんが、組み込みツールを使用することができます。
Certutil を使用したコマンドプロンプトでの操作
-
コマンドプロンプトを開きます。
-
ファイルを Base64 でエンコードします:
certutil -encode input.bin output.b64input.binをファイルパスに、output.b64を希望する出力ファイル名に置き換えてください。 -
一時ファイルを保存せずに Base64 をコンソールに出力するには:
certutil -encode input.bin tmp.b64 && findstr /v /c:- tmp.b64 && del tmp.b64findstr /v /c:-は、certutil が追加する-----BEGIN CERTIFICATE-----と-----END CERTIFICATE-----の行を削除します。
Windows での Base64 デコード(Certutil)
certutil -decode encoded.b64 decoded.bin
PowerShell を使用したエンコードとデコード
ファイルのエンコード:
[Convert]::ToBase64String([System.IO.File]::ReadAllBytes("C:\path\to\file.bin"))
文字列のエンコード(UTF-8):
[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("Hello, World!"))
# 出力: SGVsbG8sIFdvcmxkIQ==
Base64 デコードして文字列化:
[System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("SGVsbG8sIFdvcmxkIQ=="))
# 出力: Hello, World!
Base64 デコードしてファイル化:
[System.IO.File]::WriteAllBytes("decoded.bin", [Convert]::FromBase64String((Get-Content encoded.b64 -Raw)))
これらの方法は、追加のソフトウェアをインストールせずに機能します。シェル自動化の詳細については、PowerShell チートシート を参照してください。
Linux での Base64 エンコードとデコード
base64 コマンドは、ほとんどの Linux ディストリビューション(GNU coreutils)にプリインストールされています。
基本的な構文
base64 [OPTIONS] [FILE]
ファイルがない場合、またはファイルが - の場合、標準入力から読み取ります。
ファイルを Base64 でエンコード
base64 input_file > output_file
例:
base64 myfile.txt > encoded.txt
エンコードされた内容をターミナルに出力するには:
base64 myfile.txt
文字列を Base64 でエンコード
echo -n "Hello, World!" | base64
# 出力: SGVsbG8sIFdvcmxkIQ==
echo に -n を使用することで、末尾の改行を避けます。JWT の文字列デコードについては、JWT トークンのデコードと出力 を参照してください。
行折り返しを無効にする (-w 0)
デフォルトでは、base64 は出力を 76 文字で折り返します。JSON、環境変数、URL、または Terraform base64encode の使用のために、折り返しを無効にします:
base64 -w 0 input_file > output_file
Linux での Base64 デコード
base64 -d encoded_file > decoded_file
# または
base64 --decode encoded_file > decoded_file
例:
echo "SGVsbG8sIFdvcmxkIQ==" | base64 -d
# 出力: Hello, World!
代替手段:OpenSSL
echo -n "data" | openssl base64 -e
echo "ZGF0YQ==" | openssl base64 -d
コマンドラインワークフローの詳細については、Bash チートシート でパイプとスクリプティングを参照してください。
macOS での Base64 エンコードとデコード
macOS には Linux と同様の base64 コマンドが含まれていますが、ファイル入力の -i フラグが必要です。
ファイルを Base64 でエンコード
base64 -i input_file > output_file
例:
base64 -i myfile.txt > encoded.txt
ターミナルに出力するには:
base64 -i myfile.txt
エンコードしてクリップボードにコピー
base64 -i input_file | pbcopy
Mac での Base64 デコード
base64 -D -i encoded_file > decoded_file
-D フラグはデコードモードを有効にし、-i は入力ファイルを指定します。これらのコマンドは、10.7 以降のすべての最新の macOS バージョンで動作します。
Linux base64 コマンド参照
GNU base64 ツールの完全なヘルプ出力:
$ base64 --help
Usage: base64 [OPTION]... [FILE]
Base64 encode or decode FILE, or standard input, to standard output.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-d, --decode decode data
-i, --ignore-garbage when decoding, ignore non-alphabet characters
-w, --wrap=COLS wrap encoded lines after COLS character (default 76).
Use 0 to disable line wrapping
--help display this help and exit
--version output version information and exit
The data are encoded as described for the base64 alphabet in RFC 4648.
When decoding, the input may contain newlines in addition to the bytes of
the formal base64 alphabet. Use --ignore-garbage to attempt to recover
from any other non-alphabet bytes in the encoded stream.
...
一般的な使用例
| 使用例 | コマンド/アプローチ |
|---|---|
| JWT ヘッダー/ペイロード | base64 -d または jq でデコード — JWT トークンのデコードと出力 を参照 |
| HTTP Basic 認証 | Authorization: Basic <result> 用に echo -n "user:pass" | base64 -w 0 |
| Kubernetes シークレット | kubectl create secret generic mysecret --from-literal=key=$(base64 -w 0 file.bin) |
| Basic 認証付き cURL | -u user:pass または -H "Authorization: Basic $(echo -n user:pass | base64 -w 0)" — cURL チートシート を参照 |
| HTML の Data URI | data:image/png;base64,$(base64 -w 0 image.png) |