4 min read

GPG-GNU Privacy Guard

敏感文件进行加密可以使用GPG,在安装的时候会发现有gpg和gpg2两个版本。在桌面发行版或者macOS上使用,使用gpg2。

gpg2安装后依然使用gpg命令进行使用,而不是gpg2。

如果要对文件加密,首先要生成密钥对,然后再使用密钥对对文件进行加密。

生成密钥对

gpg --full-gen-key

用户名或邮箱只是作为密钥对的识别符,并不需要真实的邮箱地址。

对文件进行加密

gpg -r <密钥识别符> -e file-need-to-encrypted

然后会生成一个.gpg后缀的加密文件,那个就是加密成功后的文件。

方面记忆命令:

-r:recipe(配方,理解为用什么东西,哪一个密钥进行加密)

对加密文件进行解密

gpg -d encypted.gpg

执行后会提示你输入相应密钥的密码。这里不需要指定用什么密钥进行解密。

查看和删除

如果想要看自己生成了哪些密钥对,使用命令

gpg --list-keys

要删除密钥对需要执行两步操作,分别用来删除公钥和私钥。

gpg --delete-keys <密钥对的识别符> #删除公钥
gpg --delete-secret-keys <密钥对的识别符> #删除私钥

备份私钥

综合了下面链接的内容,这里对命令做一下简单的解释,方便记忆.

gpg -o backuped-file-name  --export-secret-keys --armor --export-options backup key-name

backuped-file-name, 指的是备份后的私钥文件名称.

key-name,是你创建密钥对时指定的名字, 可以用名字或者是邮箱. gpg --list-keys 可以看到密钥对的名称和邮箱地址.

其他的参数都可以通过 man gpg 查到, 注意 --export-option backup 是组合命令,不要把backup改为其他值.(man 手册里的除外)

举例个例子,假如我当初创建密钥对时指定的密钥对名称为luozx,那么我备份的命令是

gpg -o luozx.pgp  --export-secret-keys --armor --export-options backup luozx

luozx.pgp就是备份后的私钥.

How to Backup and Restore Your GPG Key
A couple of weeks ago I upgraded my MacBook to Catalina—the latest version of macOS. Once the upgrade was completed, I couldn’t sign my Git commits because my GPG key was missing. Somehow the upgrade wiped out my entire .gnupg directory where I stored all my GPG keys. Luckily I’ve made a backup of m…
Backup and Restore a GPG Key
Gnu Privacy Guard, also known as GnuPG and GPG, is a handy tool for managing OpenPGP keys. While don’t use PGP keys for email encryption as many people do, I do use a PGP key to sign my work when using Git. PGP keys can be a bit nastier to replace than an SSH key, so making backups is critical for r…
How to export a GPG private key and public key to a file
I have generated keys using GPG, by executing the following command gpg --gen-key Now I need to export the key pair to a file;i.e., private and public keys to private.pgp and public.pgp, respect...

导入私钥

命令行导入可以参阅上面的链接.GPG提供了Windows客户端,相比于命令行来来说非常直观,可以在官方网站找到下载地址.

The GNU Privacy Guard

打开后点击导入,选择备份的私钥就可以导入了.