GPG Encryption-Decryption Cheat sheet

gpg key commands:
To list the existing gpg keys:
gpg --list-secret-keys --keyid-format=long
gpg --list-signatures
gpg -k --keyid-format long
To generate gpg keys(Will ask for pass phrase), these commands will generate the pair of keys i.e. public and private keys:
gpg --gen-key -> with default values
gpg --quick-generate-key 'User Name' or
gpg --full-generate-key -> To over-ride default values (Key Type and Key length)
* If you want generate gpg key pairs without passphrase, then do not enter any password when asked in above command while generating secret key.
To see the public key using which a encrypted file was encrypted. It can also tell the filename, file size which was encrypted. Command: gpg --pinentry-mode cancel --list-packets your_file.gpg gpg --list-packets your_file.gpg
Output: mukesh@IT08-0013:~/Desktop$ gpg --list-packets encrypted_json gpg: encrypted with 3072-bit RSA key, ID 09CE092424315711, created 2024-06-24 "Mukesh mukesh.kumar@gmail.com" # off=0 ctb=85 tag=1 hlen=3 plen=396 :pubkey enc packet: version 3, algo 1, keyid 09CE092424315711 data: [3071 bits] # off=399 ctb=d2 tag=18 hlen=3 plen=433 new-ctb :encrypted data packet: length: 433 mdc_method: 2 # off=421 ctb=a3 tag=8 hlen=1 plen=0 indeterminate :compressed packet: algo=2 # off=423 ctb=ad tag=11 hlen=3 plen=909 :literal data packet: mode b (62), created 1719226089, name="sample_json.json", raw data: 887 bytes
Exporting Keys to ASCII format: You can export your public key in an ASCII-armored format using the --armor and --export options. This format is suitable for email transmission: gpg --output private.pgp --armor --export-secret-key mukesh.kumar@gmail.com gpg --output public.pgp --armor --export mukesh.kumar@gmail.com
Importing ASCII format key: gpg --import pubkey.asc
To encrypt a file: gpg -c file.txt --> Encrypt with default key
To encrypt a file using specific key: gpg --yes --quiet -e -r 42076B20635F7B336400105A1E5D8A3E12277BFC -o ./encrypted_json ./sample_json.json
To de-crypt a file: gpg -d file.txt.gpg
To de-crypt a file using ascii key by passing the passphrase. Using password protected private key to decrypt. ipetronik is the pssphrase here: gpg --import private.asc gpg --pinentry-mode=loopback --passphrase "ipetronik" --decrypt -o ./decripted_with_asc_1.json ./encrypted_json
To de-crypt a file using ascii key. Using non-password protected private key to decrypt: gpg --import private.asc gpg --decrypt -o ./decripted_with_asc_1.json ./encrypted_json
Flag Description Example -c Encrypts a file with a symmetric cipher. gpg -c file.txt -d Decrypts data. gpg -d file.txt.gpg --gen-key Generates a new key pair. gpg --gen-key --import Imports keys. gpg --import pubkey.asc --export Exports keys. gpg --export -a 'User Name' > public.key --list-keys Lists the public keys in your keyring. gpg --list-keys --list-secret-keys Lists the private keys in your keyring. gpg --list-secret-keys --delete-key Deletes a public key. gpg --delete-key 'User Name' --delete-secret-key Deletes a private key. gpg --delete-secret-key 'User Name' --sign Creates a digital signature. gpg --sign file.txt --verify Verifies a digital signature. gpg --verify sigfile --armor Creates ASCII armored output. gpg --armor --export 'User Name' -r Encrypts for the given user. gpg -r 'User Name' -e file.txt
About the author
I am a Software Development lead in a product based company.
Comments
Join the discussionNo comments yet. Be the first to comment.



