gpg Linux ファイル テキスト 暗号化

gpgでLinuxのファイルやテキストを暗号化する

Linuxパソコン・サーバーのコマンドラインから活用できる暗号化・復号化ソフトウェア、gpgでLinuxのファイルやテキストを暗号化する方法について紹介します。パスフレーズだけ。公開鍵認証…

Shou Arisaka
1 分で読める
2019年6月24日

Linuxパソコン・サーバーのコマンドラインから活用できる暗号化・復号化ソフトウェア、gpgでLinuxのファイルやテキストを暗号化する方法について紹介します。

パスフレーズだけ

$ cat > doc.txt
contents here.
$ gpg --symmetric doc.txt
gpg: gpg-agent is not available in this session
$ ls
total 0
 13792273860433232 -rwxrwxrwx 1 yuis yuis   92 Apr 30 13:35 doc.txt.gpg
 12947848930301330 drwxrwxrwx 1 yuis yuis 4096 Apr 30 13:35 .
  8162774326223499 -rwxrwxrwx 1 yuis yuis   15 Apr 30 13:34 doc.txt
166351711236005864 drwxrwxrwx 1 yuis yuis 4096 Apr 30 13:34 ..
$ gpg --decrypt doc.txt.gpg^C
$ cat doc.txt.gpg
♦♥☻R�D u�D∟`�K☺U�e�↕←�q�<(�NCsh§`�>� ՞☺x�♦∟ޑ,a�Y�>2�☻0�t��↑�4��*m�Y¶��q
&\�$ gpg --decrypt doc.txt.gpg
gpg: AES encrypted data
gpg: gpg-agent is not available in this session
gpg: encrypted with 1 passphrase
contents here.

上記のコマンドは以下のような手順で実行されます。

  1. gpg --symmetric doc.txt で暗号化
  2. パスフレーズを入力
  3. doc.txt.gpg が生成される
  4. gpg --decrypt doc.txt.gpg で復号化
  5. パスフレーズを入力
  6. doc.txt が生成される

公開鍵認証

gpg2 --list-keys
    # メアドやパスフレーズの設定 e.g. [email protected]

cat << EOT > file.txt
content.
EOT

gpg --encrypt --recipient '[email protected]' file.txt
# or
# gpg --encrypt --recipient '[email protected]' <<< 'content.' > file.txt.gpg

gpg --decrypt file.txt.gpg
# or
# gpg --output out.txt --decrypt hoge.txt.gpg

上記のコマンドは以下のような手順で実行されます。

  1. gpg2 --list-keys で公開鍵のリストを表示
  2. cat << EOT > file.txt でファイルを作成
  3. gpg --encryptで暗号化
  4. メアドを入力
  5. file.txt.gpg が生成される
  6. gpg --decrypt file.txt.gpg で復号化
  7. パスフレーズを入力
  8. file.txt が生成される

この記事をシェア

Shou Arisaka 2019年6月24日

🔗 リンクをコピー