プログラミング

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.

公開鍵認証

gpg2 --list-keys
    # メアドやパスフレーズの設定 e.g. user@gmail.com

cat << EOT > file.txt
content.
EOT

gpg --encrypt --recipient 'user@gmail.com' file.txt
# or
# gpg --encrypt --recipient 'user@gmail.com' <<< 'content.' > file.txt.gpg

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