关于PGP & GPG文件加密有这篇就够了 您所在的位置:网站首页 解密php加密的文件是什么意思 关于PGP & GPG文件加密有这篇就够了

关于PGP & GPG文件加密有这篇就够了

2024-06-09 20:28| 来源: 网络整理| 查看: 265

关于PGP & GPG文件加密有这篇就够了 前言什么是PGP 和 GPG?快速开始一、安装二、秘钥管理2.1 生成秘钥对2.2 生成撤销证书2.3 上传到GPG公共服务2.4.导出公私钥2.5.导入公钥 三、加解密及签名3.1 加密文件3.2 解密文件

前言

最近要对接亚马逊PSP,需要用到GPG对文件进行加密,记录一下,共勉。 另外说一点,GPG是PGP的升级款,支持的加密方式更多,GPG是开源的,而PGP是Symantec公司的版权。 所以,我们常说的PGP文件加密,就可以使用GPG方式来搞就可以。

什么是PGP 和 GPG?

PGP =Pretty Good Privacy。就是一套用于加密,解密或者签名的应用程序。 而GPG就是 GnuPG 也就是 GNU Privacy Guard 。一般用来加密文件:可以生成密钥对、签名/验签、加密/解密等操作。 GPG官网为:https://www.gnupg.org/ 官方操作文档:https://www.gnupg.org/gph/en/manual.html 基础入门可参考阮一峰老师的博客:http://www.ruanyifeng.com/blog/2013/07/gpg.html 不过文中有几处错误,不知道是版本更新了还是笔误,大部分还是没问题的。

快速开始 一、安装

直接访问选择安装包下载:https://sourceforge.net/p/gpgosx/docu/Download/

二、秘钥管理 2.1 生成秘钥对

执行命令:gpg --full-gen-key 会出现如下结果。

gpg (GnuPG) 2.2.27; Copyright (C) 2021 Free Software Foundation, Inc. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. gpg: directory '/Users/yunnasheng/.gnupg' created gpg: keybox '/Users/yunnasheng/.gnupg/pubring.kbx' created Please select what kind of key you want: (1) RSA and RSA (default) (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) (14) Existing key from card

默认选择第一个选项,表示加密和签名都使用RSA算法,我们可以输入序号:1,它会提示密钥的长度是否想要3072,一般超过2048即可,我们直接敲回车。

Your selection? 1 RSA keys may be between 1024 and 4096 bits long. What keysize do you want? (3072)

直接回车后会有如下提示:需要你设置有效期,我这里选择了5y(5年),当然你也可以输入0永久有效。

Requested keysize is 3072 bits Please specify how long the key should be valid. 0 = key does not expire = key expires in n days w = key expires in n weeks m = key expires in n months y = key expires in n years Key is valid for? (0) 5y Key expires at 日 4/26 11:26:14 2026 CST Is this correct? (y/N) y

接着它会提示你是否要这么设置,我们输入y同意,系统就要求你提供个人信息。

GnuPG needs to construct a user ID to identify your key. Real name: yunnasheng Email address: [email protected] Comment: this is remark You selected this USER-ID: "yunnasheng (yunnasheng) [email protected]" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?

它会再次确认是否要修改以上的任何信息,如果不修改输入O(这个不是零,是O) 接着,系统会让你设定一个私钥的密码,一定要牢记这个密码!!!之后想对秘钥做修改必须要有这个密码。

## We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy. #然后,系统就开始生成密钥了,这时会要求你做一些随机的举动,以生成一个随机数。(晃晃鼠标即可) gpg: /Users/yunnasheng/.gnupg/trustdb.gpg: trustdb created gpg: key 1DBE410655341854 marked as ultimately trusted gpg: directory '/Users/yunnasheng/.gnupg/openpgp-revocs.d' created gpg: revocation certificate stored as '/Users/yunnasheng/.gnupg/openpgp-revocs.d/6C8750D530B7BEF0BE86D7E81DBE410655341854.rev' public and secret key created and signed. pub rsa3072 2021-04-27 [SC] [expires: 2026-04-26] 6C8750D530B7BEF0BE86D7E81DBE410655341854 uid xxx (xxx) [email protected] sub rsa3072 2021-04-27 [E] [expires: 2026-04-26]

请注意上面的字符串1DBE410655341854,这是USER-ID的Hash字符串,可以用来替代USER-ID。

2.2 生成撤销证书

生成"撤销证书"是为了以后密钥作废时,可以请求外部的公钥服务器撤销你的公钥。(可选,你也可以不操作这一步) 命令为:gpg --gen-revoke [用户ID]

gpg --gen-revoke 1DBE410655341854 # 输出结果为: sec rsa3072/1DBE410655341854 2021-04-27 yunnasheng (yunnasheng) # 是否为这个Key生成撤销证书?输入 y 同意 Create a revocation certificate for this key? (y/N) y Please select the reason for the revocation: 0 = No reason specified 1 = Key has been compromised 2 = Key is superseded 3 = Key is no longer used Q = Cancel (Probably you want to select 1 here) # 提示让输入一个原因 Your decision? 0 Your decision? 0 Enter an optional description; end it with an empty line: > Reason for revocation: No reason specified (No description given) Is this okay? (y/N) y

选择没有任何原因,输入y同意

ASCII armored output forced. -----BEGIN PGP PUBLIC KEY BLOCK----- Comment: This is a revocation certificate # 这里我用星号代替了一些敏感数据 iQG2B(********************)CAQFAmCHh1gCHQAACgkQHb5B BlU0GFRLXwv+PBWkET+NHQzybH5XQgo1ml30QqYFqRN9uGci9Py+VJMW5N4zy4dF GqMFyTZ++N+LbckyKLBrF/YionqqCe2wxOVWhcMd+ceFcgNW6mwp3GocFLxC3mc4 RwIezzZNUqee36MOoZoDKAQDdwurhj88+5/XGfaJ6BNvszsn80lBwHPvbQdUH9Uc /bKxOIaxdUbGvDhZ6uqIq9mwqLetT6r0OgPJJQ3/OLJcF7e4K90D/uYRpPAtKuBy ++fOPM4fGifmVMCpBLtn/r8JfxMHcd4uMauscUvI7juv9UWvq0mOrx7SaUPDCXhz FjfVrnDbd5RrxbqmXPd3nh2aTx2+0RAS/bH3YInHtHLsXL8X5S59sUFN8zsV1cqa gEEO/c4NJia1JTbVK2bf5Rz4R9RpbsGuYVR7BQbr3f0IKxXyzRKMhbONqevyiphM 2JNSbeJ0VrpS+TodxLGRFLkwgekj8x0jk6pUDjuPp9qeGdNysSsnX/sTJuLnjCZT zMSN+Vjtgssu =hjYa -----END PGP PUBLIC KEY BLOCK----- Revocation certificate created. Please move it to a medium which you can hide away; if Mallory gets access to this certificate he can use it to make your key unusable. It is smart to print this certificate and store it away, just in case your media become unreadable. But have some caution: The print system of your machine might store the data and make it available to others! 2.3 上传到GPG公共服务

可以使用命令行选项--send-key将一个或多个密钥发送到密钥服务器。(这个是可选的,上传公共服务的好处就是大家都认可这个平台,认为这上面的公钥都是安全的,当然你可以直接导出文件发给对接方) 密钥服务器是通过命令行选项--keyserver指定的。 语法:gpg --keyserver --send-key

在终端执行命令:

gpg --keyserver hkp://pgp.mit.edu --send-key 1DBE410655341854 # 等待数秒结果如下: gpg: sending key 1DBE410655341854 to hkp://pgp.mit.edu

验证 gpg --keyserver --recv-keys

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys F2184XXX4BB5BB gpg: key F21846371E4BB5BB: "yunnasheng " not changed gpg: Total number processed: 1 gpg: unchanged: 1

可选服务器地址: hkp://pool.sks-keyservers.net hkp://pgp.mit.edu certserver.pgp.com // 这个国内比较卡,可能超时。

2.4.导出公私钥

导出公钥:gpg --armor --output --export 简写:gpg -a -o public-file.key --export 终端执行:

gpg --armor --output public.key --export 1DBE410655341854

导出私钥:(注意:导出私钥时需要输入保护私钥的密码)

gpg --armor --output private.key --export-secret-keys 2.5.导入公钥 gpg --import public.key 三、加解密及签名 3.1 加密文件

语法:gpg -r [-o ] [-a] -e [] 参数说明:

-r/--recipient:指定接受者的公钥 ID,消息将会使用这个公钥进行加密,也就是只有拥有这个私钥的人才能解密信息。可以指定多个,则多个接受者都能解密信息。-o/--output:指定加密后的信息输出到哪个文件。-a/--armor:将加密后的信息转为可打印的 ASCII 字符。如果不指定将会输出二进制信息。-e/--encrypt:加密。:要加密的文件。可选,如果不指定将会从标准输入读取。

实际案例:

gpg -r E79FB288FB11D6C25459E631479FE363DC53382F -o test.txt -e DEMO2.txt 3.2 解密文件

语法:gpg [-o ] -d [] 参数说明:

-o/--output:指定解密后的信息输出到哪个文件。可选,如果不指定将输出到标准输出。-d/--decrypt:解密。:待解密的文件。可选,如果不指定将尝试从标准输入读入。 实际案例: gpg -o testdec.txt -d test.txt


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有