Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save valyakuttan/6a796113c2b9392ff0a8f5a8216afe7f to your computer and use it in GitHub Desktop.
Save valyakuttan/6a796113c2b9392ff0a8f5a8216afe7f to your computer and use it in GitHub Desktop.
[Backup and Restore GPG and SSH keys] #git #gpg #ssh

GnuPG Cheat sheet

Import and Backup secret keys

  1. Import secret keys from a backup

    $ gpg --decrypt backup_file.pgp | gpg --import
    
    $ gpg --edit-key user-id
    
    gpg> trust
    
    $ # Use 5 for ultimate trust 
    
  2. Backup private key to a file

    $ gpg --armor --export-secret-keys \
        user-id | gpg --armor --symmetric --output mykey.sec.asc
    

List keys

  1. To list keys in your public key ring:

         $ gpg --list-keys
  2. To list keys in your secret key ring:

         $ gpg --list-secret-keys

Export and Import public keys

  1. To export ASCII version of public key:

         $ gpg --export --armor --output public-key.asc user-id
  2. To import a public key to your key ring:

         $ gpg --import public-key.asc

Encrypt and decrypt

Asymmetric Encryption
  1. To encrypt a file with the name doc, use:

         $ gpg --armor --output encrypted.asc --recipient user-id --encrypt doc
  2. To decrypt (option -d/--decrypt) a file with the name encrypted.asc encrypted with your public key, use:

         $ gpg --output msg --decrypt encrypted.asc
Symmetric Encryption
  1. To encrypt doc with AES-256 cipher, SHA-512 digest algorithm to mangle the passphrase and Mangles the passphrase for 65536 iterations, use:

         $ gpg --armor --output secret.asc -c --s2k-cipher-algo AES256 --s2k-digest-algo SHA512 --s2k-count 65536 doc
  2. To decrypt secret.asc encrypted with your public key, use:

         $ gpg --output msg --decrypt encrypted.asc

Sign and Encrypt

Asymmetric Encryption
  1. To encrypt and sign the file doc, use:

         $ gpg --armor --output encrypted.asc  --sign --recipient user-id --encrypt doc
Symmetric Encryption
  1. To encrypt doc with AES-256 cipher, SHA-512 digest algorithm to mangle the passphrase and Mangles the passphrase for 65536 iterations, use:

         $ gpg --armor --output secret.asc --sign -c --s2k-cipher-algo AES256 --s2k-digest-algo SHA512 --s2k-count 65536 doc
Decryption
  1. To decrypt a file with the name encrypted.asc encrypted with your public key (which also verifies the signature) use:

         $ gpg --output msg --decrypt encrypted.asc

Manage gpg-agent

  1. To reload gpg-agent

         $ gpg-connect-agent reloadagent /bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment