How to export and import keys with GPG (2024)

To encrypt email and files, you need to know how to generate, export, and import GPG keys. For instance, you have generated a GPG key pair by using the “gpg –gen-key” command, and now you want to export your public and private keys to exchange in communication. With the help of the “gpg” command, you can easily export and import the public key and private key. However, in the case of the private key, a passphrase will be associated with the exported file that can be utilized to import that specific private key in a secret keyring.

This write-up will guide you in exporting and importing public and private keys with GPG. Moreover, a practical example will be provided to show you the procedure of exporting and importing GPG keys between two systems. So, let’s start!

Types of GPG keys

The GPG key pair comprises two types of keys: Private and Public keys. The private GPG keys are encrypted and stored in the secret keyring, and public keys are maintained with certificates attesting to their trustworthiness in the public keyring. You can use the public key for the data encryption, and that encrypted data will be decrypted using the Private key. Anyone can use your public key to encrypt the data; however, the private key will be needed to decrypt it.

Note: We have already generated two GPG keypairs for the demonstration purpose, one for “john” and the other for “fred” on two separate systems. Now, we will export john’s private and public key, then import it on the other system.

How to export public key with GPG

Before sending your public key to a correspondent, you must first export it using the gpg command. In the gpg command, an additional argument is specified for identifying the public key, which will be the user ID “uid” in our case, and to generate the output of the exported file in ASCII format, the “-a” or “–armor” option is added in the “gpg” command.

To export a particular public key, first of all, list out the generated GPG keys on your system and select the key which you want to export. To do so, execute the below-given “GPG” command:

$ gpg --list-keys

For instance, for exporting the public key of the user “john,” we will note down its user ID “uid” from the list:

How to export and import keys with GPG (1)

To export the public key of “john,” we will add the “–export” option in the GPG command. Here, the “-a” option is utilized for creating an ASCII representation of the public key, and the “>” redirect operator is used for redirecting the output of the gpg command to the “public.key” file:

$ gpg --export -a john > public.key

How to export and import keys with GPG (2)

The error-free output declares that our “public.key” file is all ready to export. To check out its content, execute the following “cat” command:

$ cat public.key

As you can see, the “public.key” has stored the ASCII representation of john’s public key:

How to export and import keys with GPG (3)

How to export and import keys with GPG (4)

How to export private key with gpg

Exporting a GPG private key is useful when you have multiple computers and want one key pair for all systems. In this situation, the “gpg” command permits you to export the private key from the system where you have generated the key pair to all other computers. Also, if you belong to a specific group and want to create a single key-pair system for all group members, you can export the private key and share it with the group members. After that, your group members can import that specific private key to their systems. This operation will establish a simplified system, where the only public key will be required for sending the encrypted files or documents to multiple recipients.

The “–export-secret-key” option is added in the “gpg” command for exporting the private key. To export the private key of “john” in ASCII format, we will type out the following “gpg” command:

$ gpg --export-secret-key -a john > private key

How to export and import keys with GPG (5)

Enter a strong passphrase for exporting the private key. The receiver will use this passphrase for importing the exported private key. For instance, in our case, the other system user will utilize this passphrase for importing the “private.key” in its Secret keyring:

How to export and import keys with GPG (6)

After clicking the “OK” button, the “private.key” file will be generated with the associated passphrase:

How to export and import keys with GPG (7)

You can check out the content of the “private.key” file to know if your private key is successfully converted to ASCII representation:

$ cat private.key

How to export and import keys with GPG (8)

How to export and import keys with GPG (9)

Till this point, we have created two files, “public.key” and “private.key” that contain the ASCII representation of the public and private key of “john” uid, respectively. Now, we will share these files with another system user so that the “newuser” can import the GPG keys to its keyrings.

How to import public key with GPG

With the gpg command, importing a public key to your keyring is as simple as exporting them. By importing the sender’s public key, you can decrypt the received encrypted files, documents, or emails.

In the previous section, we showed the procedure to export john’s “public.key”. Now, we will guide you about the procedure of importing it on another “newuser” account. Before importing the “public.key”, we will list out the keys present in the keyring:

$ gpg --list-keys

Currently, “newuser” has only the GPG keypair for “fred” uid, which can be seen in the following output:

How to export and import keys with GPG (10)

Now, to import the john’s “public.key”, we will execute the “gpg” command with the “–import” option:

$ gpg --import public.key

The below-given output shows that the public key of “john” is imported successfully:

How to export and import keys with GPG (11)

For the confirmation of the specified operation, we will list out the “newuser” Public keyring:

$ gpg --list-public-keys

John’s public key is imported successfully, which can be seen in the below-given image:

How to export and import keys with GPG (12)

How to import private key with GPG

The following GPG command will help us to import the “private.key” of the “john” to the Secret keyring of the “newuser”:

$ gpg --import private.key

How to export and import keys with GPG (13)

You will be asked to enter the passphrase used while exporting the john’s private.key. After typing out that passphrase, click on the “OK” button:

How to export and import keys with GPG (14)

The output will let you know that the secret key is imported:

How to export and import keys with GPG (15)

For the verification purpose, you can also list out the private keys which are present in the Secret keyring:

$ gpg --list-secret-keys

Now, check out john’s secret key in the below-given output:

How to export and import keys with GPG (16)

Conclusion

To communicate with each other using the GPG key cryptography technique, the intended recipients must have a copy of your GPG keys. In such a case, you can export your GPG keypair with the correspondents, and then they can import it to their keyring using the GPG command. After that, you can send the encrypted files, documents, or emails, and the particular receiver will decrypt them easily. This write-up showed you how to export and import using GPG. Moreover, a practical example is also provided to demonstrate the procedure of exporting and importing GPG keys between two systems.

How to export and import keys with GPG (2024)

FAQs

How to export and import keys with GPG? ›

Exporting a public key

The command-line option --export is used to do this. It takes an additional argument identifying the public key to export. As with the --gen-revoke option, either the key ID or any part of the user ID may be used to identify the key to export.

How do I export my GPG public key? ›

Exporting a public key

The command-line option --export is used to do this. It takes an additional argument identifying the public key to export. As with the --gen-revoke option, either the key ID or any part of the user ID may be used to identify the key to export.

How do I export PGP keys? ›

Export PGP keys
  1. Open Encryption Desktop.
  2. Click "PGP Keys".
  3. Highlight the PGP key you want to export, then select the File menu and click Export > Key. ...
  4. When the Export Key to File window appears, select a location to export the key, then click Save.
Sep 6, 2022

How do I export a private key from GPG? ›

To export a GPG private and public key to a file, execute the “gpg –export-secret-key -a <username> > prv. key” and “gpg –export -a <username> > pub. key” commands. Before it, users ensure that GPG keys have been generated that can be visualized through the “gpg –list-keys” command.

How do I Import a PGP key into GPG? ›

Import PGP keys
  1. Select File ▸ Import….
  2. In the new dialog, select the key you wish to import. PGP keys end with pgp or asc, GPG keys end with gpg.
  3. Click Open.

How do I Import a GPG key from GitHub? ›

In the "Title" field, type a name for your GPG key. In the "Key" field, paste the GPG key you copied when you generated your GPG key. Click Add GPG key. To confirm the action, authenticate to your GitHub account.

Where are the GPG keys stored? ›

Your keys are stored in a directory called “. gnupg” in your home directory. This directory will also store the public keys of anyone that has sent encrypted files to you. When you import their public keys, they are added to an indexed database file in that directory.

What is the difference between GPG and PGP? ›

PGP stand for Pretty Good Privacy. GPG stands for GNU privacy Guard. Both of these programs are used to encrypt and decrypt data, messages, and emails. The difference between the two is that GPG is open-sourced where PGP is not.

How do I get my GPG public key? ›

To generate a new GPG key:
  1. In a terminal, use this command to generate a GPG key: gpg --gen-key.
  2. Provide the information asked at the prompts. Enter your identifying information. ...
  3. Use this command to list your GPG keys. ...
  4. Copy the GPG key ID to use with Bitbucket. ...
  5. Get your public key you'll add to Bitbucket.
Sep 23, 2022

How do I list all GPG keys? ›

Use the gpg --list-secret-keys --keyid-format=long command to list the long form of the GPG keys for which you have both a public and private key. A private key is required for signing commits or tags.

How do I share my GPG key? ›

Export Your Public Key

Send the public-key. gpg file to the recipient in an email or copy and paste the contents of the public-key. gpg file. The recipient should import the public key and validate it in order to use it to decrypt a message sent by you.

How do I import my private key? ›

Importing the keys from any self-custody wallet which you own is a relatively easy and straightforward process that should only take a few minutes.
  1. Step 1: Choose a trusted, compatible wallet provider. ...
  2. Step 2: Locate your recovery phrase. ...
  3. Step 3: Import the seed using your recovery phrase. ...
  4. Step 4: Verify your balance.
Mar 2, 2023

How to import PGP key command line? ›

Import Key From File:
  1. Open a command prompt.
  2. From the command prompt, enter: pgp --import (input)
  3. Press "Enter" when the command is complete.

Do PGP keys work with GPG? ›

GPG can open and decrypt files encrypted by PGP or Open PGP, meaning it works well with other products.

Can you use PGP key with GPG? ›

OpenPGP stands for Open-source PGP and became the IETF-approved standard for PGP communication which allows both PGP and GPG software to communicate with each other.

How do I add a GPG key to my repository? ›

Add GPG keys to Git repository manager

Run the command gpg --armor --export KEY-ID to get your GPG public key and add it to your repository manager. These keys are then used to generate badges to indicate if your commits are verified.

How do I import GPG keychain? ›

After you receive a public key from somebody, you can import into your keychain. Select Import from the menu in GPG Keychain Access and select the public key that was sent to you. After importing a public key, you should verify that the key actually belongs to the person that you believe it belongs to.

What are SSH and GPG keys? ›

They are used for different things on github. SSH is used for authentication while GPG is used for signing tags and commits.

How do GPG keys work? ›

GPG uses public key encryption. You can use someone's public key to encrypt a message or document, in a way that only the owner of the corresponding private key will be able to decrypt. This is a great way to send somebody highly confidential data.

Are GPG keys public? ›

GnuPG uses public-key cryptography so that users may communicate securely. In a public-key system, each user has a pair of keys consisting of a private key and a public key.

How are PGP keys stored? ›

PGP stores the keys in two files on your hard disk; one for public keys and one for private keys. These files are called keyrings. As you use PGP, you will typically add the public keys of your recipients to your public keyring. Your private keys are stored on your private keyring.

What is the best GPG key type? ›

The GnuPG developers recommend using 2k RSA keys for both encryption and signing.

Are GPG public and secret keys the same? ›

Yes. this is expected behavior. A Key ID (actually a shortened version of the key Fingerprint) is calculated using only Public Key material and so can ONLY refer to the Public Key. Secret Keys do not have a separate "Key ID", gpg is just showing the "Public Key ID" that the Secret Key is paired to.

Does GPG encrypt with public or private key? ›

GPG uses a method of encryption known as public key (asymmetric) cryptography, which provides a number of advantages and benefits. In a public key (asymmetric) encryption system, any person can encrypt a message using a public key. That encrypted message can only be decrypted with the corresponding private key.

Do GPG public keys expire? ›

PGP public keys can be used to encrypt files up to the day they expire. Once the key expires it can no longer be used to encrypt data. A private key will continue to decrypt data that was encrypted by that public key, even after the public key expires.

How do I change my GPG key? ›

Procedure for changing your GPG passphrase

Run gpg --edit-key your-key-id command. At the gpg> prompt enter the passwd to change the passphrase. First enter the urrent passphrase when prompted. Then type the new passphrase twice to confirm it.

Can I have two GPG keys? ›

Additionally, the PGP software recognizes "signing files/messages" and "signing PGP keys" as two distinct purposes and easily allows you to have separate subkeys for them. (More precisely, the primary/root key is always used to sign other people's PGP keys, but a subkey can be used to sign general data.)

How many GPG keys should I have? ›

In general, one key per identity should be fine. One key can include: Several UIDs (for separate mail addresses, ...) Several subkeys (for different devices, so you can put some subkey on your mobile; if it gets lost, revoke only this)

Where to store GPG master key? ›

Store this USB stick in your ork guarded vault:
  • master-secret-key. gpg - copy of your master secret key.
  • sub-secret-keys. gpg - copy of your secret sub keys.
  • ~/. gnupg - your entire keyring. It will be used as the GNUPGHOME for future subkeys.

How do I send a public PGP key? ›

Open PGP Desktop. Open your email client, create a new message, and address it to the recipient to whom you are sending your public key. From PGP Desktop, select your key, then drag and drop your key onto the body of the email message. Your key block displays as an attachment in the email message.

How do I import a key into my GPG YubiKey? ›

To import the key on your YubiKey:

Insert the YubiKey into the USB port if it is not already plugged in. Enter the GPG command: gpg --edit-key 1234ABC (where 1234ABC is the key ID of your key) Enter the command: keytocard. When prompted if you really want to move your primary key, enter y (yes).

How do I import a key file? ›

Import the Certificate File and Private Key File
  1. In Firefox, click Tools and then Options.
  2. Select the Advanced tab and then the Encryption tab.
  3. Click the View Certificates button.
  4. Click the Authorities tab in the Certificate Manager dialog box, and select the certificate file to import.
  5. Click Import.

How do I import a private key and certificate? ›

Assign the existing private key to a new certificate

Select Start, select Run, type mmc, and then select OK. On the File menu, select Add/Remove Snap-in. In the Add/Remove Snap-in dialog box, select Add. Select Certificates, and then select Add.

Can you import a private key into keystore? ›

You cannot directly import private key information to a keystore using keytool . You must convert the certificate and private key into a PKCS12 ( . p12 ) file, and then you can import the PKCS12 file to your keystore. where the [password] is the password you specified when you created the private key.

What is the difference between GPG and GPG2? ›

GPG2 is extended version of GPG. Where changes are mostly in internal levels, thus commands are same. A major change between GPG and GPG2 is that all the crypto operations have been moved to the GPG agent, so without an agent, GPG2 can't work.

Can I use GPG key as SSH? ›

To use a GPG key, you'll use a similar program, gpg-agent, that manages GPG keys. To get gpg-agent to handle requests from SSH, you need to enable support by adding the line enable-ssh-support to the ~/. gnupg/gpg-agent. conf.

Is GPG still secure? ›

Integrity & Verification. GPG verifies the owner and sender of the data. If a GPG encrypted reply is sent, it confirms the sender, and lastly, it authenticates that the message has not been modified in any way. It is impossible to decode an encrypted message in a subtle or secretive manner.

What is the difference between GPG key and certificate? ›

The owner of the key pair makes the public key available to anyone, but keeps the private key secret. A certificate verifies that an entity is the owner of a particular public key.

What is PGP vs Openpgp vs GPG? ›

Open PGP: The IETF-approved standard that describes any encryption technology that uses processes interoperable with PGP. PGP: A proprietary encryption solution owned by Symantec. GPG: Another popular solution that follows the Open PGP standard and provides an interface for users to easily encrypt their files.

How to decrypt PGP file using GPG? ›

How to encrypt and decrypt files using GPG
  1. Generate a new key with default configuration (Quick Key Generation)
  2. Generate a new key with your own configuration (Full Key Generation)
  3. List all GPG public keys.
  4. List all GPG private key pairs.
  5. Export Public Key in ASCII Format.
Sep 30, 2021

What is the difference between PGP key and PGP certificate? ›

Quick and dirty: Public key is used to encrypt, Private key used to decrypt. The certificate advertises your Public key (among other things) and a signed Certificate authenticates that you are who you say you are.

What can I do with PGP private key? ›

The most common reason for PGP encryption use is to enable people to confidentially send messages and data to each other using a combination of their public and private keys. It is often used to encrypt and decrypt emails, files, text messages, and entire disk partitions, and to authenticate digital certificates.

How to encrypt files using PGP? ›

PGP Tool
  1. Locate file using the file explorer and Double-click.
  2. Enter the passphrase (might not be needed if remembered)
  3. Confirm decryption. Edit file using associated application. Close it.
  4. Return to PGP Tool application.
  5. For the file in history click “Encrypt back"
  6. Confirm parameters and click Encrypt.

How do I share my public GPG key? ›

Export Your Public Key

Send the public-key. gpg file to the recipient in an email or copy and paste the contents of the public-key. gpg file. The recipient should import the public key and validate it in order to use it to decrypt a message sent by you.

How do I get my public key from GPG suite? ›

To search for public keys on the key server, open GPG Keychain, press ⌘F (or click the Spotlight icon to Lookup Key ). Type the email address for which you are looking for a public key and click Search . Optionally we recommend to verify public keys.

How do I share my PGP public key? ›

  1. Open PGP Desktop.
  2. Click the PGP Keys Control box.
  3. Right-click the key you want to send and select Send To > Mail Recipient.
  4. Your email client opens a message with your public key as an attachment.
  5. Type a recipient for the message and send the message.
Jan 27, 2012

How do I import a public key? ›

Drag and Drop Method
  1. Open Encryption Desktop.
  2. Click the PGP Keys Control box to display a list of keys in the right pane.
  3. Drag the . asc file containing the public key to the PGP Keys window.
  4. Click Import. The key is then added to your keyring.
Aug 12, 2021

How do I import a public key in Linux? ›

Upload a public key for Linux
  1. Enter: ssh-keygen -t rsa. The type of 'rsa' means RSA protocol 2 ('rsa1' means RSA protocol 1). ...
  2. Enter a name for the file where the key will be saved (/home/myself/. ssh/id_rsa).
  3. Click Enter.
Mar 9, 2023

How do I add a GPG key to Git? ›

Add GPG keys to Git repository manager

Run the command gpg --armor --export KEY-ID to get your GPG public key and add it to your repository manager. These keys are then used to generate badges to indicate if your commits are verified.

What is the difference between public key and private key in GPG? ›

GnuPG uses public-key cryptography so that users may communicate securely. In a public-key system, each user has a pair of keys consisting of a private key and a public key. A user's private key is kept secret; it need never be revealed. The public key may be given to anyone with whom the user wants to communicate.

How to extract private key from public key? ›

How to Extract the Private and Public Key From pfx File
  1. Extract the key-pair. #openssl pkcs12 -in sample.pfx -nocerts -nodes -out sample.key.
  2. Get the Private Key from the key-pair. ...
  3. Get the Public Key from key pair. ...
  4. Need to do some modification to the private key -> to pkcs8 format. ...
  5. Get those files.

Where do I copy public key? ›

Copying Public Keys to Remote Servers
  1. Use the ssh-copy-id command to append the public key in the local ~/.ssh/id_rsa.pub file to the ~/.ssh/authorized_keys file on the remote system, for example: Copy. ssh-copy-id remote_user@host.
  2. When prompted, enter your password for the remote system.

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5713

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.