How to encrypt and decrypt with GPG
In this post I'd like to talk about GNU Privacy Guard (GPG). GPG allows us to encrypt or decrypt data. You might be aware of PGP, GPG is its FOSS alternative.
Content:Keys
Keys are used for encryption and decryption. There are private and public keys. You can share your public keys with others to send you encrypted data and then use your private key to decrypt it. Always keep your private keys secret, otherwise whoever has your private key could also decrypt data.
Creating keys
Use gpg --full-generate-key
to generate keys.
You will be asked about algorithm you would like to use, we will go with RSA and RSA (option 1).
The next it will ask you about how many bits long should the keys be, your options are between 1024 and 4096 bits. Default keysize is 3072.
You also need to set how long should the keys be valid, default is 0
- forever, never expiring. Confirm your choice.
Next you need to fill in your name, email address, and possibly a comment. Confirm everything is correct.
Choose your passphrase, this is to protect your private key and you should definitely remember it.
After you're finished, your keys are going to be created.
Exporting public keys
If you'd like, you can export your public key to a file.
gpg -export -a > publicKey.asc
or gpg -export > public.key
-export
- exports the public key-a
- or-armor
exports it in ASCII aromoured ouput
gpg –list-public-key
Lists public keys and their id and user id.
Encrypting and decrypting with GPG
Encrypting and decrypting with keys
As mentioned before, you need to share your public keys for this. The other side also needs to import the public key, with
gpg –import public.key
Encrypting files
gpg -e -r publicKeyID file
-e
- encrypt data-r
- for recipient (user id or public key id)
Decrypting files
gpg file.asc
Run this on the encrypted file, you'll be prompted for your passphrase and after then gpg will run decryption.
Encrypting and decrypting with password
You can also encrypt and decrypt files with a password instead of keys. This is symmetric. If you've read my earlier article about backup to cloud with rclone, we've used it there.
Encrypt with password
gpg -c file
This command will encrypt file
, you'll be prompted for passphrase and then it will create a new file file.gpg
.
gpg -c file.txt > encryptedFile.gpg
if you would like the encrypted file under different name.
Decrypt with password
gpg -d file.gpg
This command will decrypt your file, you will be prompted for the password you used earlier to create the encrypted file.
gpg file.gpg -o decryptedFile
will output decrypted file.gpg
to decryptedFile
That's all, folks
Well, that's it for today. If you have any questions or notes, please let me know on fedi @bugbear@alt.lawndale.space.
If you'd like to read even more about it, I suggest man page for GPG.
see you, space cowboy