Skip to content

Instantly share code, notes, and snippets.

@sergeysova
Created May 4, 2016 09:40
Show Gist options
  • Select an option

  • Save sergeysova/66376134a8341302e3e75b1f31c59ea5 to your computer and use it in GitHub Desktop.

Select an option

Save sergeysova/66376134a8341302e3e75b1f31c59ea5 to your computer and use it in GitHub Desktop.
GPG Cheat Sheet

GPG

Create keys

You can select the defaults.

gpg --gen-key

List keys

Public keys

gpg --list-keys

Secret keys

gpg --list-secret-keys

Exporting

Public key

gpg --export -a "UserName" > gpg.public.key

Hint: instead "UserName" you can type email or GPG id

Secret key

gpg --export-secret-key -a "name@domain.com" > gpg.secret.key

Hint: instead "name@domain.com" you can type user name or GPG id

Importing

Public key

gpg --import gpg.public.key

Secret key

gpg --allow-secret-key-import --import gpg.secret.key

Deleting

Public key

gpg --delete-key "User Name"

Hint: Instead "User Name" you can type GPG id or email

Secret key

gpg --delete-secret-key 35E82649

Hint: Instead 35E82649 (GPG id) you can type user name or email

Fingerprint

To generate a short list of numbers that you can use via an alternative method to verify a public key, use:

gpg --fingerprint > gpg.fingerprint

This creates the file fingerprint with your fingerprint info.

Encrypting data

To encrypt data, use:

gpg -e -u "Sender User Name" -r "Receiver User Name" mydata.tar

Hint: Instead typing user name of sender and receiver, you can type email

There are some useful options here, such as -u to specify the secret key to be used, and -r to specify the public key of the recipient. As an example: gpg -e -u "Charles Lockhart" -r "A Friend" mydata.tar This should create a file called "mydata.tar.gpg" that contains the encrypted data. I think you specify the senders username so that the recipient can verify that the contents are from that person

NOTE!: mydata.tar is not removed, you end up with two files, so if you want to have only the encrypted file in existance, you probably have to delete mydata.tar yourself.

An interesting side note, I encrypted the preemptive kernel patch, a file of 55,247 bytes, and ended up with an encrypted file of 15,276 bytes.

Decrypting data

To decrypt data, use:

gpg -d mydata.tar.gpg

If you have multiple secret keys, it'll choose the correct one, or output an error if the correct one doesn't exist. You'll be prompted to enter your passphrase. Afterwards there will exist the file "mydata.tar", and the encrypted "original," mydata.tar.gpg.

NOTE: when I originally wrote this cheat sheet, that's how it worked on my system, however it looks now like "gpg -d mydata.tar.gpg" dumps the file contents to standard output. The working alternative (worked on my system, anyway) would be to use "gpg -o outputfile -d encryptedfile.gpg", or using mydata.tar.gpg as an example, I'd run "gpg -o mydata.tar -d mydata.tar.gpg". Alternatively you could run something like "gpg -d mydata.tar.gpg > mydata.tar" and just push the output into a file. Seemed to work either way.

Ok, so what if you're a paranoid bastard and want to encrypt some of your own files, so nobody can break into your computer and get them? Simply encrypt them using yourself as the recipient.

Sharing secret keys

NOTE!: the following use cases indicate why the secret-key import/export commands exist, or at least a couple ideas of what you could do with them. HOWEVER, there's some logistics required for sharing that secret-key. How do you get it from one computer to another? I guess encrypting it and sending it by email would probably be ok, but I wouldn't send it unencrypted with email, that'd be DANGEROUS.

Use Case *.1 : Mentioned above were the commands for exporting and importing secret keys, and I want to explain one reason of why maybe you'd want to do this. Basically if you want one key-pair for all of your computers (assuming you have multiple computers), then this allows you export that key-pair from the original computer and import it to your other computers.

Use Case *.2 : Mentioned above were the commands for exporting and importing secret keys, and I want to explain one reason of why maybe you'd want to do this. Basically, if you belonged to a group, and wanted to create a single key-pair for that group, one person would create the key-pair, then export the public and private keys, give them to the other members of the group, and they would all import that key-pair. Then a member of the group or someone outside could use the group public key, encrypt the message and/or data, and send it to members of the group, and all of them would be able to access the message and/or data. Basically you could create a simplified system where only one public key was needed to send encrypted stuffs to muliple recipients.

@sergeysova

sergeysova commented May 4, 2016

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment