Generate Gpg Key Command Line
The command expects the files to bee verified either on the commandline or reads the filenames from stdin; each anem muts be on separate line. The command is intended for quick checking of many files.list-keys names, -list-public-keys names List all keys from the public keyrings, or just the ones given on the command line.
Related
To generate your own unique public/secret key pair: gpg -gen-key. Command options that can be used in combination with other command options. To produce a ciphertext file in ASCII format, just add the -a option when encrypting or signing a message or extracting a key. May 26, 2017 You can import someone’s public key in a variety of ways. If you’ve obtained a public key from someone in a text file, GPG can import it with the following command: gpg -import nameofpubkeyfile; There is also the possibility that the person you are wishing to communicate with has uploaded their key to a public key server.
Introduction
GPG, or GNU Privacy Guard, is a public key cryptography implementation. This allows for the secure transmission of information between parties and can be used to verify that the origin of a message is genuine.
In this guide, we will discuss how GPG works and how to implement it. We will be using an Ubuntu 16.04 server for this demonstration, but will include instructions for other distributions as well.
How Public Key Encryption Works
A problem that many users face is how to communicate securely and validate the identity of the party they are talking to. Many schemes that attempt to answer this question require, at least at some point, the transfer of a password or other identifying credentials, over an insecure medium.
Ensure That Only the Intended Party Can Read
To get around this issue, GPG relies on a security concept known as public key encryption. The idea is that you can split the encrypting and decrypting stages of the transmission into two separate pieces. That way, you can freely distribute the encrypting portion, as long as you secure the decrypting portion.
This would allow for a one-way message transfer that can be created and encrypted by anyone, but only be decrypted by the designated user (the one with the private decrypting key). If both of the parties create public/private key pairs and give each other their public encrypting keys, they can both encrypt messages to each other.
So in this scenario, each party has their own private key and the other user’s public key.
Validate the Identity of the Sender
Another benefit of this system is that the sender of a message can “sign” the message with their private key. The public key that the receiver has can be used to verify that the signature is actually being sent by the indicated user.
Set Up GPG Keys
GPG is installed by default in most distributions.
If for any reason GPG is not installed, on Ubuntu and Debian, you can update the local repo index and install it by typing:
On CentOS, you can install GPG by typing:
To begin using GPG to encrypt your communications, you need to create a key pair. You can do this by issuing the following command:
This will take you through a few questions that will configure your keys:
- Please select what kind of key you want: (1) RSA and RSA (default)
- What keysize do you want? 4096
- Key is valid for? 1y (expires after 1 year. If you are just testing, you may want to create a short-lived key the first time by using a number like “3” instead.)
- Is this correct? y
- Real name: your real name here
- Email address: your_email@address.com
- Comment: Optional comment that will be visible in your signature
- Change (N)ame, ©omment, (E)mail or (O)kay/(Q)uit? O
- Enter passphrase: Enter a secure passphrase here (upper & lower case, digits, symbols)
At this point, gpg
will generate the keys using entropy. Entropy describes the amount of unpredictability and nondeterminism that exists in a system. GPG needs this entropy to generate a secure set of keys.
This process may take a long time depending on how active your system is and the keysize you selected. To generate additional entropy more easily, you can use a tool called haveged. Open up a new terminal and SSH into the server again to set up haveged
on your server.
Create a Revocation Certificate
You need to have a way of invalidating your key pair in case there is a security breach or in case you lose your secret key. There is an easy way of doing this with the GPG software.
This should be done as soon as you make the key pair, not when you need it. This revocation key must be generated ahead of time and kept in a secure, separate location in case your computer is compromised or inoperable. To generate a revocation key, type:
You will be asked to confirm the revocation key creation and then prompted for the reason that it is being revoked. This information will be visible to other users if the revocation is used in the future. You can choose any of the available options, but since this is being done ahead of time, you won’t have the specifics. Often, it is a good idea to create a revocation certificate for each of the likely scenarios for maximum flexibility.
Afterwards, you will then be asked to supply a comment and finally, to confirm the selections. Before creating the revocation certificate, you will need to enter your GPG key’s passphrase to confirm your identity. The revocation certificate will be written to the file specified by the --output
flag (revocation.crt
in our example):
You should immediately restrict the permissions on the generated certificate file in order to prevent unauthorized access:
The revocation certificate must be kept secure so that other users cannot revoke your key. As the message states, you should consider backing the certificate up to other machines and printing it out, as long as you can secure it properly.
How To Import Other Users’ Public Keys
GPG would be pretty useless if you could not accept other public keys from people you wished to communicate with.
You can import someone’s public key in a variety of ways. If you’ve obtained a public key from someone in a text file, GPG can import it with the following command:
There is also the possibility that the person you are wishing to communicate with has uploaded their key to a public key server. These key servers are used to house people’s public keys from all over the world.
A popular key server that syncs its information with a variety of other servers is the MIT public key server. You can search for people by their name or email address by going here in your web browser:
You can also search the key server from within GPG by typing the following:
You can use this method of searching by name or email address. You can import keys that you find by following the prompts.
How To Verify and Sign Keys
While you can freely distribute your generated public key file and people can use this to contact you in a secure way, it is important to be able to trust that the key belongs to who you think it does during the initial public key transmission.
Verify the Other Person’s Identity
How do you know that the person giving you the public key is who they say they are? In some cases, this may be simple. You may be sitting right next to the person with your laptops both open and exchanging keys. This should be a pretty secure way of identifying that you are receiving the correct, legitimate key.
But there are many other circumstances where such personal contact is not possible. You may not know the other party personally, or you may be separated by physical distance. If you never want to communicate over insecure channels, verification of the public key could be problematic.
Luckily, instead of verifying the entire public keys of both parties, you can simply compare the “fingerprint” derived from these keys. This will give you a reasonable assurance that you both are using the same public key information.
You can get the fingerprint of a public key by typing:
This will produce a much more manageable string of numbers to compare. You can compare this string with the person themselves, or with someone else who has access to that person.
Sign Their Key
Signing a key tells your software that you trust the key that you have been provided with and that you have verified that it is associated with the person in question.
To sign a key that you’ve imported, simply type:
When you sign the key, it means you verify that you trust the person is who they claim to be. This can help other people decide whether to trust that person too. If someone trusts you, and they see that you’ve signed this person’s key, they may be more likely to trust their identity too.
You should allow the person whose key you are signing to take advantage of your trusted relationship by sending them back the signed key. You can do this by typing:
You’ll have to type in your passphrase again. Afterwards, their public key, signed by you, will be displayed. Send them this, so that they can benefit from gaining your “stamp of approval” when interacting with others.
When they receive this new, signed key, they can import it, adding the signing information you’ve generated into their GPG database. They can do this by typing:
They can now demonstrate to other people that you trust that their identity is correct.
How To Make Your Public Key Highly Available
Because of the way that public key encryption is designed, there is not anything malicious that can happen if unknown people have your public key.
With this in mind, it may be beneficial to make your public key publicly available. People can then find your information to send you messages securely from your very first interaction.
You can send anyone your public key by requesting it from the GPG system:
You can then send this file to the other party over an appropriate medium.
If you want to publish your key to a key server, you can do it manually through the forms available on most of the server sites.
Another option is to do this through the GPG interface. Look up your key ID by typing:
The highlighted portion in the output below is the key ID (look for the pub
along the left-hand column if you’re uncertain about which one to use). It is a short way to reference the key to the internal software.
To upload your key to a certain key server, you can then use this syntax:
The key will be uploaded to the specified server. Afterwards, it will likely be distributed to other key servers around the world.
Encrypt and Decrypt Messages with GPG
You can easily encrypt and decrypt messages after you have shared your keys with the other party.
Encrypt Messages
You can encrypt messages using the “–encrypt” flag for GPG. The basic syntax would be:
This encrypts the message using the recipient’s public key, signs it with your own private key to guarantee that it is coming from you, and outputs the message in a text format instead of raw bytes. The filename will be the same as the input filename, but with an .asc
extension.
You should include a second “-r” recipient with your own email address if you want to be able to read the encrypted message. This is because the message will be encrypted with each person’s public key, and will only be able to be decrypted with the associated private key.
So if it was only encrypted with the other party’s public key, you would not be able to view the message again, unless you somehow obtained their private key. Adding yourself as a second recipient encrypts the message two separate times, one for each recipient.
Decrypt Messages
When you receive a message, simply call GPG on the message file:
Windows git ssh key generation. The software will prompt you as necessary.
If instead of a file, you have the message as a raw text stream, you can copy and paste it after typing gpg
without any arguments. You can press “CTRL-D” to signify the end of the message and GPG will decrypt it for you.
Key Maintenance
There are a number of procedures that you may need to use on a regular basis to manage your key database.
To list your available GPG keys that you have from other people, you can issue this command:
Your key information can become outdated if you are relying on information pulled from public key servers. You do not want to be relying on revoked keys, because that would mean you are trusting potentially compromised keys.
You can update the key information by issuing:
This will fetch new information from the key servers.
You can pull information from a specific key server by using:
You may receive error messages if any of your keys cannot be found on the key server.
Conclusion
Using GPG correctly can help you secure your communications with different people. This is extremely helpful, especially when dealing with sensitive information, but also when dealing with regular, everyday messaging.
Because of the way that certain encrypted communications can be flagged by monitoring programs, it is recommended to use encryption for everything, not just “secret” data. That will make it more difficult for people to know when you are sending important data or just sending a friendly hello.
This tutorial series will teach you how to use GPG in Linux terminal. I will not tell you a bunch of theory to overwhelm you. Instead, I show you quick and dirty examples to get you started, and explain the basic theory along the way.
This is part 1 of this series. At the end of this post, you should be able to generate your own public/private keypair and a revocation certificate. This certificate is used to revoke your public/private keypair when your private key is compromised or you forget the passphrase for your private key.
GPG can be used for encryption and for signing. This software is pre-installed on most Linux distributions. Currently the stable version is GPG 2.0. I’m using the modern version GPG 2.2 on Arch Linux.
Check Your GPG Version
How To Generate Gpg Key
First Let’s check out the version of GPG on your system and some interesting tidbits. Run the following command.
As you can see, I’m using GPG 2.2.8, which is the latest version. We also know that the configuration directory is ~/.gnupg, which will hold our public/private key files. The default option file is ~/.gnupg/gpg.conf and ~/.gnupg/dirmngr.conf. It also tells us what algorithms are supported.
If you look closely, you can see that the insecure hash algorithm SHA1 is still supported in version 2.2.8 SHA1 is obsolete and you don’t want to use it to generate signature.
Create Your Public/Private Key Pair and Revocation Certificate
Use gpg --full-gen-key
command to generate your key pair.
It asks you what kind of key you want. Notice there’re four options. The default is to create a RSA public/private key pair and also a RSA signing key. Let’s hit Enter to select the default.
Next it asks you the key length. The default is 2048 bits long. 1024 RSA key is obsolete. The longer 4096 RSA key will not provide more security than 2048 RSA key. So hit Enter to select the default.
After that it asks you how long the key should be valid, 2 years is fine. You can always update the expiration time later on.
Generate Gpg Key Command Line Download
Now it asks you if it’s correct. Notice that the default is No. So press y
then Enter to confirm it’s correct.
And now we need to provide some user identification information for the key. This is important because this information will be included in our key. It’s one way of indicating who is owner of this key. The email address is a unique identifier for a person. You can leave Comment blank.
Select Okay.
Now it asks you to enter a passphrase to protect your private key. Enter a good and long passphrase and remember it. Because if you forget this passphrase, you won’t be able to unlock you private key.
Once you enter and confirm your passphrase. GPG will generate your keys.
It will take a while for GPG to generate your keys. So you can now do other stuff.
It took about 4 minutes on my system to generate my key pair.
This first line tells us that GPG created a unique identifier for public key. This unique identifier is in hex format. When someone wants to download you public key, they can refer to you public key via your email address or this hex value.
The third line tells us that GPG created a revocation certificate and its directory.Your should never share you private key with anyone.If you private key is compromised, you can use revocateion certificate to revoke your key. That means you tell the rest of the world that the old public key shall not be used any more.I suggest that you open this revocation certificate with your text editor to see what’s inside there.
Let’s look at the last three lines. They tell us the public key is 2048 bits using RSA algorithm. The public key ID 4F0BDACC matchs the last 8 bits of key fingerprint. The key fingerprint is a hash of your public key.
Oct 04, 2018 Elliptic curve cryptography, or ECC, is a powerful approach to cryptography and an alternative method from the well known RSA. It is an approach used for public key encryption by utilizing the mathematics behind elliptic curves in order to generate security between key pairs. Apr 10, 2017 This is part 11 of the Blockchain tutorial explaining how the generate a public private key using Elliptic Curve. In this video series different topics will be explained which will help you to. Elliptic curve cryptography key generation download. Oct 14, 2015 John Wagnon discusses the basics and benefits of Elliptic Curve Cryptography (ECC) in this episode of Lightboard Lessons. Check out this article on DevCentra. This might seem like we're cheating a bit, however this meets the criteria for public key encryption (anyone with the public key can encrypt, only the holder of the private key can decrypt), and it also sidesteps the issue of translating the message into an elliptic curve point reversibly (which can be done, but it can be kludgy). Feb 22, 2012 The fig 3 show are simple elliptic curve. Key Generation. Key generation is an important part where we have to generate both public key and private key. The sender will be encrypting the message with receiver’s public key and the receiver will decrypt its private key. Now, we have to select a number ‘d’ within the range of ‘n’.
It also lists our user ID information: your name and your email address. And it also indicates the subkey which is 2048 bits using RSA algorithm and the unique identifier of the subkey.
Now you can find that there are two files created under ~/.gnupg/private-keys-v1.d/ directory. These two files are binary files with .key extension.
Export Your Public Key
Others need your public key to send encrypted message to you and only your private key can decrypt it. Use the following command to export your public key. --armor
option means that the output is ASCII armored. The default is to create the binary OpenPGP format. user-id
is your email address.
The exported public key is written to pubkey.asc
file.
Export Your Private Key
Issue the following command to export your private key.
The exported key is written to privkey.asc
file.
Protect Your Private Key and Revocation Certificate
Your private key should be kept in a safe place, like an encrypted flash drive. Treat it like your house key. Only you can have it and don’t lose it. And you must remember your passphrase, otherwise you can’t unlock your private key.
You should also protect your revocation certificate. If others have your revocation certificate, they can immediately revoke your public/private keypair and generate a fake public/priavte keypair.
In part 2 we will look at how to encrypt message with your public key and how to decrypt it with your private key. Take care!