All about using GPG

Lord777

Professional
Messages
2,580
Reputation
15
Reaction score
1,332
Points
113
gpg is an encryption and electronic signing tool. It uses asymmetric encryption based on two keys: private and public. A private key is sometimes called a secret key. A public key is called a public key.
The essence of the work in general terms is as follows: anyone can generate a pair of keys for themselves. A public key (as the name implies) is not a secret key - this key can be publicly available. You can use this key to encrypt messages and files. Moreover, messages and files are encrypted only "in one direction" - it is no longer possible to decrypt them, even using this very public key. Their decryption is possible only if the corresponding private key is used.

Thus, if you want to send a secret message or encrypted file to a certain person, then you take the public key of this person (which may be freely available), encrypt the information and send this encrypted information to him - no one can find out except the owner of the corresponding private key.

If you exchange public keys, you can have an encrypted conversation with this person:
  • you encrypt your messages with the other person's public key and send them to them
  • you use your private key to read these messages
  • your conversation partner encrypts their messages with your public key and sends them to you
  • you use your private key to read your messages
  • etc
The private key can do another interesting trick: it can sign files. Moreover, as you can already guess, you can check the signature with the corresponding public key.

So, gpg is the OpenPGP part of the GNU Privacy Guard (GnuPG). This tool provides digital encryption and signature services using the OpenPGP standard. gpg has full key management features, as well as all the stray features you might expect from an OpenPGP implementation.

Warning about using GPG
Use a good password for your user account and a good passphrase to protect your private key. This passphrase is the weakest part of the entire system. Regarding GPG, you need to protect the folder from unauthorized access "~/.gnupg/".

For use in scripts, you have gpg there is a well-documented implementation of the API, to activate this interface, use the options --with-colons and --status-fd. This option may also be useful for certain operations --command-fd.

Which public GPG keys can I trust?
You can create a public-private key pair on your computer for any name. This is something like SSL certificates: you can create them for any site, but such a self-signed certificate will not be trusted by browsers. Therefore, a third party is involved the root certification authority (CA) (there is a little more complicated, there are also intermediate trusted certificate authorities). Actually, this service is paid for when purchasing an SSL certificate for the site - you can generate the certificate yourself in a second on any Linux device.

The situation is similar with GPG keys: they can be created in any name. Therefore, when you download a public GPG key to verify a digital signature, you must be completely sure that it is from the person you think it is. Otherwise, the whole procedure loses its meaning.

After the very first exchange of public keys, further exchange is more reliable: the other party, in order to confirm that the new public key comes from it, will sign this very new public key with its previous private key. Thus, after receiving a new public key from your interlocutor, you can verify the signature with the previous public key, and if the signature is correct, then, therefore, you can trust the new key, which should replace the previous public key.

How to generate a public-private key pair in GPG
It all starts with key generation. You can do this on your computer.

To generate a key pair, run the command:
gpg --gen-key

You will need to enter:
  • Your full name
  • Email address
Then the program will show the user ID you selected and ask if everything is correct? If everything is correct, you will need to enter O.

The program will then ask you to enter your password.

You can ask the question: what else is the password if an asymmetric algorithm with key files is used for encryption and decryption? Yes, that's right, the password you enter will not be involved in the encryption and decryption process, it is needed to protect your key, since it is stored on your computer in your home folder. In case an attacker gets physical access to your computer, they won't just be able to use your private key - they will first need to choose a password. Therefore, create a strong password. By the way, if you export a private key to a file (the commands for this will be shown below) to, for example, transfer it to another computer, then when importing this key to another computer, you will need to enter the password for this private key.

Then the program will show the tip:
You need to get a lot of random numbers. It is desirable that you perform some other actions during the generation process (typing on the keyboard, mouse movements, accessing disks); this will give the random number generator more opportunities to get a sufficient amount of entropy.
Instead of --gen-key, you can use the --full-generate-key option, which also generates a key pair, but shows more options. When using --gen-key, some of the option values are used by default.

How to view or save your public key in GPG
I'm not good with imagination, so use the following commands as the username (more precisely, the user ID).) I use Alex Collins - replace this string with your value.

To view the public key, use the --export option, as well as the-a option, which means that the output data should not be in binary format, but in ASCII (that is, text format suitable for copying and pasting into a messenger or email message). At the end, specify the user name:
gpg --export -a "Alex Collins"

To save the public key to a file, use output redirection:
gpg --export -a "Alex Collins" > public.key

As a result, the public key will be saved to the public.key file.

Or use the-o optionfollowed by the file name:
gpg --export -a -o public.key "Alex Collins"

By the way, please note that no password is requested for displaying the public key.

How to view or save your private (secret) key in GPG
To do this, use the --export-secret-key option:
gpg --export-secret-key -a "Alex Collins"

To save to a file:
gpg --export-secret-key -a "Alex Collins" > private.key

Or with the-o option:
gpg --export-secret-key -a -o private.key "Alex Collins"

How to import GPG keys
To import a public key, run:
gpg --import public.key

This command adds the public key, which is located in the "public.key" file.

To add a private key from the private file.key run the command:
gpg --import private.key

How to view a list of GPG keys
To view all public keys on your system, run:
gpg --list-keys

To view all private keys on your system, run:
gpg --list-secret-keys

How to delete GPG keys
To delete a public key, run:
gpg --delete-key "Alex Collins"

Remember: if your computer has a private key associated with this public key, you will get an error! You must delete the private key of this pair, and then you can delete the public key.

To delete a private key:
gpg --delete-secret-key "Alex Collins"

Why do I need fingerprints in GPG?
Fingerprint (fingerprints) of keys in gpg have approximately the following form:
D743 875D 1BB6 095A 366B B81F 3BFD 8738 3926 AC69

A fingerprint can serve as a key identifier - that is, instead of specifying the user name (in my example, this is Alex Collins), you can use a fingerprint.

For example, to delete a secret key using a fingerprint as the identifier:
gpg --delete-secret-key 'D743 875D 1BB6 095A 366B B81F 3BFD 8738 3926 AC69'

The second function of using a fingerprint is to verify the public key. As an example, the following story is given: Alice gave Bob a piece of paper with a fingerprint written on it (or called on the phone and dictated the fingerprint). Alice then sent Bob her public key. But since the key came from the Internet — it is not clear who exactly it is from? Was this key changed along the way? After importing the key, you can view its fingerprint. Since Bob has a fingerprint that he received from a trusted source (Alice dictated it with her voice or personally passed a note with the fingerprint), Bob can now compare these two fingerprints - if the fingerprints are identical, then the public key was actually sent by Alice and therefore it can be trusted.

To view typos for a specific user's key, use the command:
gpg --fingerprint 'Alex Collins'

Encrypt files and data with GPG
About encryption in gpg, you need to know what it can be:
  • asymmetric (encrypted with a public key, decrypted with a private one)
  • symmetric (encrypted and decrypted with a private key, encrypted and decrypted with the same passphrase)
The second thing to know is that encryption can be combined with file signing. We'll look at signing the file and verifying the signature later. We'll also look at simultaneous file encryption and signing later.

Third: you can encrypt with one or more public keys.

To encrypt a file using the symmetric method with a password, use the-c option (or its long counterpart --symmetric):

The following command is used to encrypt the file test.php with a password in gpg:
gpg -c test.php

As a result of encryption, a file with the .gpg extension will be created (in this case, it will be the test.php.gpg file).

In order to encrypt a file with symmetric encryption that can be decrypted with a private key (in this case, it can be decrypted with a private key or password), you need to use several options at once:
  • -e-means data encryption
  • -c-means symmetric encryption
  • -r 'id' - means encrypt data for a user with a specific id
Example of symmetric file encryption command test.php for the Alex Collins user with the ability to decrypt it with a private key OR to decrypt it with a password:
gpg -e -c -r 'Alex Collins' test.php

More precisely, combining the two options-e and -c encrypts the session key with a public key and a symmetric cipher, so both the private key and the password can be used for decryption (optional). If your private key is available on another computer where you are decrypting the file, the private key password will be requested during decryption. If the private key is missing, you will be prompted for the password that was used when encrypting the file.

For public key encryption (-e), so that the file (test.php) only the owner of the corresponding paired private key (-r 'Alex Collins') could decrypt it.):
gpg -e -r 'Alex Collins' test.php

Instead of the-r 'Destination Name' option, you can use the-R 'Destination Name' option or its long counterpart --hidden-recipient 'Destination Name'. It also encrypts the file for the specified destination, but the name of that destination is encrypted. By the way, it should be mentioned that even in an encrypted file, the destination name can be viewed by anyone — how to do this will be discussed below.

Example of file encryption test.php the public key of the user Alex Collins, but with the encrypted name of the recipient.
gpg -e -R 'Alex Collins' test.php

Please note that in all cases of encryption, the original file remains!!! You need to decide for yourself what to do with it, for example, delete it.

To avoid entering the recipient's name every time, you can set the default value with the --default-recipient option. It also comes with the --default-recipient-self and --no-default-recipient options.

How to encrypt files in GPG so that they can be sent as a message
By default, files are saved in the OpenPGP binary format. By the way, in this format, the file can take up significantly less space (at times!) than the original file before encryption. If you open an encrypted file, make sure that it is binary data that cannot be transferred by copy-pasting without losing its functionality.

However, sometimes you may need to forward an encrypted file or message via email or messenger in plain text. This is possible by using the-a option, which creates an encrypted file in ASCII format.

gpg -e -a -r 'Alex Collins' test.php

As a result, a file with the .asc extension will be created (in this case, the test.php.asc file).

How to encrypt a message in GPG
To encrypt a message, use the following construction:
echo 'MESSAGE TEXT' | gpg -e -a -r 'RECEIVER OF THE MESSAGE'

Please note that I added the-a-option so that the encrypted message can be copied and pasted to messenger or email (or printed on a printer). Without this option, binary data will be displayed.

The encrypted message will be output to standard output, i.e. to the terminal. If you want to save an encrypted message to a file, use the construct:
echo 'MESSAGE TEXT' | gpg -e -a -r 'MESSAGE RECEIVER'> FILE_WITH_CYPHER

Usage example:
echo 'I found a treasure' | gpg -e -a -r 'Alex Collins'

How to decrypt files in GPG
The-d option is used for decryption:
gpg -d test.php.gpg

And the data will be output directly to standard output! That is, to the terminal. To save data to a file, use the-o option, followed by the name of the new decrypted file:
gpg -d -o test.php test.php.gpg

Alternatively, use a construction of the form:
gpg -d ENCRYPTED_FILE> ENCRYPTED_FILE

Example:
gpg -d test.php.gpg > test.php

It also seems to work fine (for text files, for sure).

GPG: Unable to enter password after incorrect attempt
Symmetric encryption caches passwords so that you don't have to enter the same password many times. I don't know how this relates to security, but you can get very confused if you enter an incorrect password - when you try to open the same file later, the system will use the cached version instead of asking you for a new version, that is, it will simply give an error:
gpg: decryption failed: Invalid symmetric encryption algorithm

To overcome this situation, you need to explicitly specify not to use the cache. For this purpose, there is an option --no-symkey-cache, usage example:
gpg -d --no-symkey-cache test.php.gpg

How to view recipients of a GPG-encrypted file
If you don't want to decrypt the file (or maybe you just can't because there is no private key), but you want to see who it is intended for (this can be done even for encrypted files, if the recipients are not specifically encrypted as shown above), then the --list-only option is available.

Together with this option, you need to use the-d option, and also specify the file to be checked, for example:
gpg --list-only -d test.php.gpg

Can be output:
gpg: encrypted with 2048-bit RSA key with ID 0E5137A9EF8E8F15 generated on 2019-03-22

Or without localization:
gpg: encrypted with 2048-bit RSA key, ID 0x0E5137A9EF8E8F15, created 2019-03-22

This message indicates that asymmetric encryption has been performed for the user Alex Collins

With symmetric encryption, the message will look like this:
gpg: AES encrypted session key
gpg: encrypted with one passphrase
gpg: encrypted with 2048-bit RSA key with ID 0E5137A9EF8E8F15 generated on 2019-03-22

Or without localization:
gpg: AES encrypted session key
gpg: encrypted with 1 passphrase
gpg: encrypted with 2048-bit RSA key, ID 0x0E5137A9EF8E8F15, created 2019-03-22

And until 2019, almost until the latest versions of gpg, the --list-only option has an interesting bug: if the computer where the check is performed has the private key of the user to whom this file is intended, then nothing is displayed. This can be confusing, since nothing is output either if the recipient is encrypted. I found a mention of this bug in 2013 (see the list of sources), and it is still present in 2019 in the most recent version of Tails. To get around this bug, use the-v option, and you'll get something like the following command:
gpg --list-only -v -d test.php.gpg

It will output something like:
gpg: public key is 0x0E5137A9EF8E8F15

And you can already find the user by using the public key ID.

In the most recent versions of gpg (for example, in Arch Linux and BlackArch), this error has already been fixed. That is, --list-only works exactly as it is intuitively expected and the crutch in the form of the-v optionis no longer required.

Selecting a private key for decryption
As we have already found out, even encrypted information in GPG contains the recipient's name (unless it is specifically deleted). Therefore, the procedure for decrypting files is clear: GPG looks at the recipient and uses his private key to extract data.

And how does this happen if the recipient is encrypted? In this case, GPG simply goes through all the private keys available in the system and sees which one is suitable (such a kind of brute-forcing).

You can help the program by using the --try-secret-key NAME option. Instead of the NAME, you need to specify the user ID with the private key for which the file is encrypted.

I think you already understand that encrypted information contains at least such parts as: 1) recipient ID, 2) "payload" - the encrypted data itself. The recipient's ID may be fake — meaning that one person's public keys were used for encryption, but another person was specified as the recipient. This will cause the decryption attempt in GPG to fail.

To decrypt such files with a fake key ID, use the --try-all-secrets option. As you can understand from the name of the option itself, it will not look at the key identifier in the message, but will try all the secret keys until it finds the correct one for decryption. In other words, the behavior will be exactly the same as if the recipient (or recipients) were not specified at all. That is, as if the data was encrypted using the --throw-keyids or --hidden-recipient options.

How to sign a file in GPG
As mentioned above, a private key is used to sign the file. Since the private (secret) key is kept (obviously) secret by the owner, then based on the assumption that no one other than the owner has access to the private key, we can be sure that the file that is signed with this private key comes from this particular person. If the file was changed somehow after the signature, then this signature is no longer valid for this file. Therefore, signature verification allows you to guarantee:
  • the file comes from the person who signed it;
  • the file has not been modified in any way since it was signed.
To verify the signature of a certain person, you must have that person's public key.

To sign a file, use the-s option or the longer --sign option. If you have only one private key on your computer, it will be selected automatically.

If you have multiple private keys on your computer, you should also use the-u NAME option or its longer version --local-user NAME. This option overwrites the --default-key value.

By the way, about --default-key. It sets the key that will be used by default so that it doesn't have to be entered every time.

So, I want to sign the file test.php, with the private key of user Alex Collins, then my command is as follows:
gpg -u 'Alex Collins' -s test.php

A file with the .gpg extension will be created. In my case, this is the test.php.gpg file. This file includes both the source file and its digital signature. A new file in binary format — that is, even if it was previously a message or text file, it becomes unreadable.

Using the --clear-sign option (another spelling is --clearsign), you can create a file in plain text.

gpg -u 'Alex Collins' --clear-sign test.php

The test.php.asc file will be created, which will contain the source text after - - - - - BEGIN PGP SIGNED MESSAGE-----

Text content can be extracted without third-party tools, and you still need PGP to verify the signature.

In real life, public programs and other files are much more often distributed as a pair: the source file of the program + a separate signature. You can guess why: most users do not care about this electronic signature and they would not be able to extract files from .gpg the format. In principle, their position is clear: what is the point of verifying the signature, if we still do not personally know who exactly signed the file, and we also do not know who authenticates the public key. Unless, you can be sure that the file is really signed by the author of the program, and not by a hacker who hacked the server where the files are stored for download, and that the hacker did not replace the file during its "journey" to our computer.

So, to make such a pair (more precisely, we only need to make a separate signature file — nothing happens to the source file at all), PGP has the-b option (or a longer spelling --detach-sign), which is used as follows:
gpg -u 'Alex Collins' -b test.php

An additional .sig file will be created (in my case, test. php. sig). There is nothing in this file other than a digital signature. So you need to provide both the original file and the .sig file.

Editing GPG keys
To edit the key of a specific user, run the command (replace 'Alex Collins' with the desired user ID):
gpg --edit-key 'Alex Collins'

The --gen-revoke key creates a revocation certificate (by the way, a revocation certificate is generated from the very beginning, when creating a key pair), which, when distributed to people and key servers, tells them that your key is no longer valid.

To disable or enable a public key in your own public key ring:
gpg --batch --edit-key USERID disable

And:
gpg --batch --edit-key USERID enable

To change the private key password (replace 'Alex Collins' with your user ID):
gpg --batch --edit-key 'Alex Collins' passwd

Change the owner's trust level:
gpg --edit-key 'Alex Collins' trust

A message will be displayed:
Specify how much you trust this user to verify the validity of other users ' keys (checks the passport, checks key prints from different sources, etc.)

And you will need to make a choice by entering one of the following numbers:
1 = Don't know or won't answer
2 = Do not trust
3 = I have limited trust
4 = I completely trust
5 = I absolutely trust

How to specify a user ID in GPG
You can specify the user ID in various ways:
  • full name and surname
  • full email address
  • key fingerprint
  • full user ID (first and last name, as well as email address, for example Heinrich Heine <[email protected]>)
  • partial indication of the user's name or email address
  • other methods (in general, there are quite a lot of them, and many comparison operators and condition testing are supported)

GPG Files
There are several configuration files available to control certain aspects of gpg operations. Unless otherwise specified, they are expected to be placed in the current user's home directory.

gpg.conf

A standard configuration file that gpg reads at startup. It can contain any number of valid long options; you don't need to enter the initial two dashes, and you can't use a short option entry. You can change the default value on the command line. You should make a backup copy of this file.

~/.gnupg

This is the default home folder, which is used unless otherwise set in the GNUPGHOME environment variable or the --homedir option.

~/.gnupg/pubring.gpg

Public keyring. You should have a backup copy of this file.

~/.gnupg/pubring.gpg.lock

A lock file for public monitoring.

~/.gnupg/pubring.kbx

Public kiring uses various formats. This file is shared with gpgsm. You should have a backup copy of this file. In fact, it is a database where all the keys are stored. You can view the structure of this file using the command:
kbxutil ~/.gnupg/pubring.kbx

~/.gnupg/pubring.kbx.lock

The lock file for 'pubring. kbx'.

~/.gnupg/secring.gpg

Secret kearing used by GnuPG version prior to 2.1. It is not used by GnuPG 2.1 and later.

~/.gnupg/secring.gpg.lock

A lock file for secret monitoring.

~/.gnupg/.gpg-v21-migrated

A file showing that the upgrade to GnuPG 2.1 has been made.

~/.gnupg/trustdb.gpg

Trusted database. There is no need to make a backup copy of this file; it is better to make a backup copy of the ownertrust values, see the --export-ownertrust option.

~/.gnupg/trustdb.gpg.lock

A lock file for a trusted database.

~/.gnupg/random_seed

A file used to save the state of the internal random number pool.

~/.gnupg/openpgp-revocs.d/

Directory where pre-generated revocation certificates are stored. The file name corresponds to the fingerprint of the OpenPGP key for which this certificate is used. Anyone who has access to these files can revoke your keys. Therefore, these files must be kept secret and backed up.
 
Top