GPG, also known as GNU Privacy Guard, is a free software program that executes the OpenPGP standard for secure data encryption and digital signing. In GPG, private and public keys are utilized to encrypt and decrypt data. The GPG keys are stored in a keyring file in the home directory. Once users have generated a GPG key pair, utilize it to encrypt and sign data, such as emails or files, for secure transmission or storage.
Considering its significance, this article will demonstrate various methods to export GPG private and public keys to a file:
- Prerequisites: Generate a GPG Key
- How to Export a GPG Key Pair (Private/Public Keys) to a File?
- List All GPG Keys
- Export a GPG Private Key to a File
- Verify the Exported Private Key
- Export the Public Key to a File
- Verify the Exported Public Key
Prerequisite: Generate a GPG Key Pair
GPG is based on PGP that provides similar encryption and digital signature functionality. To generate a GPG key pair, follow our article “How to Generate GPG Keys”.
How to Export a GPG Key Pair (Private/Public Keys) to a File?
A GPG private key is a secret key that encrypts data. It is kept secret by the user and not shared with others. With a private key, a user can sign and decrypt messages.
On the other hand, a GPG public key is a key that is shared with others. It is distributed freely and is utilized to encrypt messages that the owner of the related private key decrypts.
List All GPG Keys
To list the GPG keys, use the “gpg” command and the “list-keys” utility. To do so, execute the following command:
$ gpg --list-keys
It displays a list of all GPG keys and their corresponding usernames. In our case, the current username is “itslinuxfoss”.
Note: Identify the username for the GPG key pair that will be helpful to export public and private keys.
Export a GPG Private Key to a File
To export the private key to the specified file, type the “gpg” command with the “export-secret-key” option. In addition, specify the username, such as “itslinuxfoss”, and the file name as “prv.key” in the following command:
$ gpg --export-secret-key -a itslinuxfoss > prv.key
It navigates to the new pop-up window, which can be visualized as below:
Enter the GPG passphrase (a password that users set when creating or importing a GPG key in Linux) to complete this command and hit the “OK” button. This command exports the private key to a file called “prv.key” in the current directory.
Verify the Exported Private Key to a File
To visualize the exported private key; users can utilize the “cat” command by specifying the filename. In our case, the filename is “prv.key”:
$ cat prv.key
The output shows that the private key has been exported successfully.
Export the Public Key to a File
To export the public key, use the “gpg” command with the “export” option by specifying the username and filename. For instance, “itslinuxfoss” and “pub.key” are specified to export the public key:
$ gpg --export -a itslinuxfoss > pub.key
This command exports the public key to a file called “pub.key” in the current directory.
Note: Users can also save the exported key to a file named “.asc” or “.txt” format.
Verify the Exported Public Key to a File
To verify the exported public key, utilize the “cat” command by specifying the file name as “pub.key” in the below command:
$ cat pub.key
The output shows that the public key has been successfully exported to the “pub.key” file.
Conclusion
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.
This guide has illustrated the step-by-step procedure to export the GPG private and public keys to the file.