Encrypt-Decrypt file using GPG
With GPG you can encrypt and decrypt files with a password. GPG is an encryption and signing tool for Linux/UNIX like operating system such as OpenBSD/Solaris/Fedora.
In this first example I will show you the basics on how to encrypt a file. But before we do that lets create a file called encrypt_example.
# touch encrypt_example
To encrypt single file, use the GPG command as follows: gpg -c encrypt_example. This will create a encrypt_example.gpg file. The -c option will Encrypt with symmetric cipher
Now to decrypt it, use command - gpg encrypt_example.gpg
Now for some more advanced features.
1. Key Generation
gpg # Initialize GPG for this user (e.g. create ~/.gnupg). Only have to run once.
gpg –gen-key # Start key generation process. Follow prompts.
2. Viewing Keys
gpg –list-keys # View public keys
gpg –list-secret-keys # View private keys
3. Exporting Public Keys
gpg –export # Exports key in binary format
gpg –export –armor # Export in a usable, ASCII format
4. Importing Public Keys
gpg –import /path/to/public/key/file
5. Encrypting a Message
gpg –encrypt –armor –recipient message_file # Creates encrypted message in an ASCII format
6. Decrypting a Message
gpg encrypted_message_file
You will be prompted for the filename to use for the output of the decryption process.
7. Encrypting with a Symmetric Key
gpg –symmetric –armor message_file
8. Signing and Encrypting a Message
gpg –sign –encrypt –armor –recipient message_file
9. Creating a Detached Signature
gpg –detach-sign –armor message_file # Sender
gpg –verify message_file.asc message_file # Recipient
10. Signing Another’s Public Key
A is going to sign B’s key.
# First, A must do:
gpg –sign-key B
gpg –export –armor B > B.key
# Then, B must do:
gpg –import B.key
This tutorial will be updated to be more verbose in it’s explination.
Related Posts
Tags: gpg