Install:
brew install gnupg # on Mac OS
apt-get install gnupg # on Ubuntu/Debian
...
Create new key. Choose a name and email, if asked about crypto scheme pick RSA and 2048 bit minimum.
gpg --full-gen-key
gpg --list-keys [email protected] # check the key is there
Also create a revocation certificate. If you loose your private key or it is compromised, you can publish it to warn others. If asked for a reason, pick something generic.
gpg --output ~/revocation.crt --gen-revoke [email protected]
chmod 600 ~/revocation.crt
Export your public key and publish it somewhere for people to see
gpg --output ~/public.key --armor --export [email protected]
Back up your key and revocation certificate and store somewhere safe
gpg --output ~/private.key --armor --export-secret-key [email protected]
chmod 600 ~/private.key
mv ~/revocation.crt /path/to/safe/place/revocation.crt
mv ~/private.key /path/to/safe/place/private.key
You could restore your keys on a new machine with
gpg --import public.key
gpg --allow-secret-key-import --import private.key
gpg --edit-key [email protected] # interactive, choose "trust", then "5" (for ultimate), then "save" - "?" for help
Last step is editing the trust of the key since you know your backup is genine (not strictly necessary)
If you have to revoke your key, check out this site for some details on how to act in case you got compromised: https://www.rossde.com/PGP/pgp_keyserv.html
Encrypt a file - make sure you have the recipient's public key imported! You also sign it with your own private key to ensure authenticity.
gpg --encrypt --sign --output /path/to/encrypted.gpg --recipient [email protected] /path/to/plaintext.txt
Decrypt the file afterwards - make sure you have the recipient's private key imported!
gpg --output /path/to/decrpyted.xyz --decrypt /path/to/encrypted.gpg
file /path/to/decrpyted.xyz # check what you got
Add flag --armor
to output a text-encoded (but probably huge) file that could be sent via email/messenger.
Complete guide: https://gnupg.org/gph/en/manual.html DigitalOcean guide: https://www.digitalocean.com/community/tutorials/how-to-use-gpg-to-encrypt-and-sign-messages