This article introduces how to encrypt Linux files and text with GPG, an encryption/decryption software that can be used from the Linux PC/server command line.
Passphrase Only
$ 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.
The above commands are executed in the following steps:
- Encrypt with
gpg --symmetric doc.txt - Enter passphrase
doc.txt.gpgis generated- Decrypt with
gpg --decrypt doc.txt.gpg - Enter passphrase
doc.txtis generated
Public Key Authentication
gpg2 --list-keys
# Set email address and passphrase 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
The above commands are executed in the following steps:
- Display list of public keys with
gpg2 --list-keys - Create file with
cat << EOT > file.txt - Encrypt with
gpg --encrypt - Enter email address
file.txt.gpgis generated- Decrypt with
gpg --decrypt file.txt.gpg - Enter passphrase
file.txtis generated