如何在Linux中安装和使用GPG加密以加密和解密文件和文件夹?

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.

对于基于rpmyum的发行版,我们可以使用以下命令安装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]
Create Private and Public Keys
Create Private and Public Keys
创建私钥和公钥

列出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
List Keys
List Keys
列表键

注册到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'
Export Public Key In ASCII Format
Export Public Key In ASCII Format
以ASCII格式导出公钥

使用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.

请记住,加密文件的大小将大于原始文件的大小。

LEARN MORE  How To Install and Configure Openssl Suite On Windows
了解更多信息如何在Windows上安装和配置Openssl Suite

使用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
List Installed Keys
List Installed 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]'
Delete Key with Secret Keys
Delete Key with Secret Keys
使用秘密密钥删除密钥

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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值