Encrypting Files with GPG | Generated by AI
Encrypting a File with Your GPG Key
Your GPG key listing shows a public key (pub) with the fingerprint 982740390B5D33E90B6B6133892BC5A4789B71CD associated with your email lzwjava@gmail.com. To encrypt a file using this key (so only you can decrypt it with your private key), follow these steps in your terminal:
- Navigate to the directory containing the file you want to encrypt (e.g., if your file is
secret.txt):cd ~/path/to/your/file -
Encrypt the file using the
--encryptoption and specify the recipient (your email or key ID). This creates an encrypted.gpgfile.- Using your email (simpler):
gpg --encrypt --recipient lzwjava@gmail.com secret.txt - Using the key ID (full fingerprint):
gpg --encrypt --recipient 982740390B5D33E90B6B6133892BC5A4789B71CD secret.txt
This outputs
secret.txt.gpg, which is the encrypted version. The originalsecret.txtremains unchanged. - Using your email (simpler):
- Verify the encryption (optional):
gpg --list-packets secret.txt.gpgThis shows details about the encrypted packet.
Decrypting the File (For Later Reference)
To decrypt it back (only possible with your private key and passphrase):
gpg --decrypt secret.txt.gpg > secret.txt.decrypted
Enter your passphrase when prompted.
Tips
- Symmetric encryption (if you don’t need a recipient key, just a passphrase): Use
gpg -c secret.txtinstead. - Encrypt and sign (to prove it’s from you): Add
--signto the encrypt command, e.g.,gpg --encrypt --sign --recipient lzwjava@gmail.com secret.txt. - If you encounter errors (e.g., key not trusted), run
gpg --edit-key lzwjava@gmail.comand usetrustto set it to ultimate. - Always back up your
~/.gnupgdirectory for key safety.
For more details, check the GPG manual: GPG Documentation.