GPG, GnuPG :: Encryption

Personal Use

$ gpg --full-gen-key
$ gpg --output ~/Documents/GPG/revocation.crt --gen-revoke
$ chmod 600 ~/revocation.crt

Encrypt a file using password:

gpg -c classified.txt

and it creates the file classified.txt.gpg.

Now you can open it in emacs and in vim (with the plugin gnupg.vim) and when you save the file it will ask you for the password. At this point you can remove the original .txt file.

If you forget your password, your will not be able to recover its contents.

Import secret key to another computer (method 1)

  • link:

  • link:

  • link:gpg-list-keys-command-outputs-uid-unknown-after-importing-private-key-onto

In machine 1 (will ask password):

$ gpg --export-secret-key -a > classified.asc

In machine 2 (will ask password):

$ gpg --import classified.asc

Do this in both machines:

$ shred --remove classified.asc

Then, the key on the new machine shows as "[unknown]" trust level with --list-secret-keys. Do this:

gpg --edit-key user@email.xz

and now, from gpg prompt:

gpg> trust
choose option 5 (five) here
gpg> save

import secret key to another computer (method 2)

$ gpg --export-secret-keys --armor --output privkey.asc user-id

Example on machine 1, where the key was created:

$ gpg --export-secret-keys --armor --output \ \

Then, on machine 2, 3, etc:

$ gpg --import
  • link:

Run encrypted shell script

Be careful not to accidentally lose access to your script or accidentally destroy it when following these steps. You have been warned.


  • Encrypt a file using a symmetric key (instead of public and private keys, even though that would be possible too).

  • Optionally securely destroy the original, non encrypted script (if you encrypted it in the first place, it means the file contains sensitive information you might not want to be hanging around).

  • Use a redirection to pass the decrypted script to bash to run the original contents of the script.

  • Done!

$ tree -C ./

1 directory, 1 file

$ bash ./

$ gpg \
> --no-symkey-cache \
> --symmetric \
> --output ./ \
> ./

$ tree -C ./

1 directory, 2 files

$ cat ./
(output is a bunch of inscrutable stuff, as expected)

$ bash < <(2> /dev/null gpg --decrypt --quiet ./

Then we can use shred from coreutils to securely destroy the original file:

shred -vzun5 ./

If the script needs to be updated, first save it back to an unencrypted, plain text format:

$ gpg \
  --decrypt \
  --quiet \
  --no-symkey-cache \
  --output ./ \

Edit the file, save it and encrypt it again (like shown earlier above).

tree -C ./ example