Security is an important part of today’s IT operations. There are a lot of tools that provide some part of the security operations. GNU Privacy Guard or simply GPG is one of them. It provides encryption, decryption, digital signatures, and signing.
安全性是当今IT运营的重要组成部分。 有很多工具可提供部分安全操作。 GNU Privacy Guard或GPG就是其中之一。 它提供加密,解密,数字签名和签名。
将GPG安装到Ubuntu,Debian,Mint和Kali (Install GPG To Ubuntu, Debian, Mint and Kali)
For deb or apt based distributions we can install GPG with the following command for gnupg
package.
对于基于deb或apt的发行版,我们可以使用gnupg
软件包的以下命令安装GPG。
$ sudo apt install gnupg
将GPG安装到CentOS,Fedora,RHEL (Install GPG To CentOS, Fedora, RHEL)
For rpm
or yum
based distributions we can install GPG with the following command.
对于基于rpm
或yum
的发行版,我们可以使用以下命令安装GPG。
$ sudo yum install gnupg
创建私钥和公钥 (Create Private and Public Keys)
Now we will start with creating Private and Public Keys. As we know Public Cryptography provides two keys where the private key is a secret key which should be protected accordingly. The public key is as its name suggests open to everyone we want to collaborate with. So we need these keys. we can create them with the --gen-key
option like below.
现在,我们将从创建私钥和公钥开始。 众所周知,公共密码学提供了两个密钥,其中私有密钥是秘密密钥,应相应地对其进行保护。 顾名思义,公钥向我们要与之合作的所有人开放。 因此,我们需要这些键。 我们可以使用--gen-key
选项创建它们,如下所示。
$ gpg --gen-key
This command will ask us some questions like below.
该命令将向我们询问一些类似以下的问题。
Real name: İsmail Baydan
Email address: [email protected]

列出GPG键(List GPG Keys)
We can list-keys created and saved in the current system with the --fingerprint
option. We should provide some search terms to this option like the email address or individual name etc. In this example, we will search with the email address ibaydan
.
我们可以使用--fingerprint
选项列出在当前系统中创建并保存的键。 我们应该为此选项提供一些搜索条件,例如电子邮件地址或个人名称等。在此示例中,我们将使用电子邮件地址ibaydan
搜索。
$ gpg --fingerprint ibaydan

注册到GPG密钥服务器(Register To GPG Keyserver)
In Public Key Cryptography key distribution is important. We need to provide our Public Key to the others in an open and trusted way. So Keyservers are designed to hold public keys or individuals and corporate. There are different Keyserver’s but pgp.mit.edu is the most popular one. We will use --keyserver
option to specify the key server and --send-keys
to provide a fingerprint of the key we want to register.
在公共密钥密码学中,密钥分配很重要。 我们需要以开放和可信赖的方式将我们的公钥提供给其他人。 因此,密钥服务器旨在容纳公共密钥或个人和公司。 有不同的密钥服务器,但是pgp.mit.edu是最受欢迎的密钥服务器。 我们将使用--keyserver
选项指定密钥服务器,并使用--send-keys
提供我们要注册的密钥的指纹。
$ gpg --keyserver gpg.mit.edu --send-keys 003D114F
以ASCII格式导出公钥 (Export Public Key In ASCII Format)
In some cases, we may want to print the key in a more readable format like ASCII. This will be useful if we want to distribute our public key with email or similar ways. We will use --armor
option with output
which gets the file name we want to save and --export
the key search term.
在某些情况下,我们可能希望以更具可读性的格式(例如ASCII)打印密钥。 如果我们想通过电子邮件或类似方式分发公钥,这将很有用。 我们将在output
使用--armor
选项,该选项将获取我们要保存的文件名,并--export
关键搜索词。
$ gpg --armor --output pubkey.txt --export 'ibaydan'

使用GPG加密文件(Encrypt A File with GPG)
We will use our Private Key in order to encrypt given data like a text file. We will use --encrypt
with --receipent
which will set private key and the last one the file we want to encrypt. We can also use --output
option to specify the file name of the encrypted file.
我们将使用我们的私钥来加密给定的数据,例如文本文件。 我们将--encrypt
与--receipent
一起使用,这将设置私钥以及我们要加密的最后一个文件。 我们也可以使用--output
选项来指定加密文件的文件名。
$ gpg --encrypt --recipient 'ibaydan' --output ServerPass.txt.enc ServerPass.txt
Keep in mind that encrypted file size will be bigger than original file size.
请记住,加密文件的大小将大于原始文件的大小。
使用GPG解密文件 (Decrypt A File with GPG)
Now if we received a file that is encrypted by our Public Keys we need to decrypt it with our Private Key. We will use --decrypt
option. We can also optionally specify the output file with --output
option like below.
现在,如果我们收到了用公钥加密的文件,则需要使用私钥对其解密。 我们将使用--decrypt
选项。 我们还可以选择使用--output
选项指定输出文件,如下所示。
$ gpg --output foo.txt --decrypt ServerPass.txt.enc
列出已安装的GPG密钥 (List Installed GPG Keys)
We can list existing keys with the --list-keys
option. This will provide information like path, public key algorithm, user id, etc.
我们可以使用--list-keys
选项列出现有的密钥。 这将提供诸如路径,公共密钥算法,用户ID等信息。
$ gpg --list-keys

删除GPG密钥(Delete GPG Key)
As we see in the previous example the Keys are stored in a database format named kbx
. If we need to remove keys we should use --delete-key
with the related term like email.
正如我们在前面的示例中看到的那样,密钥以名为kbx
的数据库格式存储。 如果需要删除密钥,则应将--delete-key
与相关术语(例如电子邮件)一起使用。
$ gpg --delete-key ibaydan
使用秘密密钥删除密钥 (Delete Key with Secret Keys)
While deleting keys if there is related secret we should provide --delete-secret-keys
option too. If not we will get error like below.
当删除密钥时,如果有相关的秘密,我们也应该提供--delete-secret-keys
选项。 如果没有,我们将得到如下错误。
gpg: there is a secret key for public key "ibaydan"!
$ gpg --delete-secret-keys '[email protected]'

翻译自: https://www.poftut.com/install-use-gpg-encrytion-linux-order-encrypt-decrypt-files-folder/