`

Linux下使用GPG加密解密的说明及示例

gpg 
阅读更多
1、生成密钥对:gpg --gen-key

为用户生成新密钥对。需提供:密钥类型(默认为RSA/RSA);密钥长度(以位为单位,越长越强);过期时间(以防密钥损坏);(通常我都是一路回车过去)接下来的还是要填一填的:名称、电子邮箱、标识密钥所有者的注释;密码短语(必须提供,如果私钥被盗,将无法使用)。

2、列出公钥:gpg --list-keys

列出所拥有的公钥:他们自己的公钥以及从与之通信的其他人那里导入的任何公钥。

3、导出公钥:gpg --export --armor key-id -o file.key

将公钥导出至文件,以便于其他人使用。--armor选项以文本形式显示输出,而非二进制格式。key-id是电子邮箱地址或在--list-keys的pub行中列出的八位十六进制数。

4、导入公钥:gpg --import file.key

从发送给您的密钥文件中导入其他人的公钥

5、加密文件:gpg --encrypt --armor -r key-id file

用key-id的公钥加密消息。如果未提供-r key-id,命令将提示收件人输入。默认输出文件为file.asc.

6、解密文件:gpg --decrypt file

用您的私钥之一解密用公钥加密的消息。

示例:

创建属于您自己的公钥/ 私钥对。

[sjx@server1 ~]$ gpg --gen-key

gpg (GnuPG) 2.0.14; Copyright (C) 2009 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.

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)

Your selection?    Enter

RSA keys may be between 1024 and 4096 bits long.

What keysize do you want? (2048)    Enter

Requested keysize is 2048 bits

Please specify how long the key should be valid.

0 = key does not expire

<n> = key expires in n days

<n>w = key expires in n weeks

<n>m = key expires in n months

<n>y = key expires in n years

Key is valid for? (0)     Enter

Key does not expire at all

Is this correct? (y/N)   y

GnuPG needs to construct a user ID to identify your key.

Real name: shangjx

Email address: shangjx13@gmail.com

Comment: Enter

You selected this USER-ID:

"xiyou<sjx@server1.example.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?    o

You need a Passphrase to protect your secret key.

can't connect to `/home/wxh/.gnupg/S.gpg-agent': No such file or directory

gpg-agent[2008]: directory `/home/wxh/.gnupg/private-keys-v1.d' created

(此时会弹出图形应用程序,输入并验证密钥)

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.

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: key CA83F5AF marked as ultimately trusted

public and secret key created and signed.

gpg: checking the trustdb

gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model

gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u

pub 2048R/CA83F5AF 2011-08-15

Key fingerprint = F886 17A2 F832 B545 6E27 B424 E539 26BF CA83 F5AF

uid

xiyou<sjx@server1.example.com>

sub 2048R/DB58BFCE 2011-08-15

若要导出密钥,请在上面的输出中查找密钥ID 。可以在上面的pub 2048R/ 输出的后面找到。在给示例中

密钥ID 是CA83F5AF 。以下示例将显示使用该密钥ID 的命令。

●导出您的公钥,与合作伙伴共享。

[wxh@server1 ~]$ gpg -a -o ~/pub.key --export CA83F5AF

●将导出的公钥复制到合作伙伴的server2 上。

[wxh@server1 ~]$ scp pub.key server2.example.com:~

●导入合作伙伴的公钥。

[wxh@server2 ~]$ gpg --import pub.key

gpg: directory `/home/wxh/.gnupg' created

gpg: new configuration file `/home/wxh/.gnupg/gpg.conf' created

gpg: WARNING: options in `/home/wxh/.gnupg/gpg.conf' are not yet active during this run

gpg: keyring `/home/wxh/.gnupg/secring.gpg' created

gpg: keyring `/home/wxh/.gnupg/pubring.gpg' created

gpg: /home/wxh/.gnupg/trustdb.gpg: trustdb created

gpg: key CA83F5AF: public key "westos <wxh@server1.example.com>" imported

gpg: Total number processed: 1

gpg:

imported: 1 (RSA: 1)

●创建一个文本文件,内含供合作伙伴阅读的消息。

[wxh@server2 ~]$ echo "xi'an university of posts and telecommunications" > encrypt.txt

●用合作伙伴的公钥加密文件。www.2cto.com

[wxh@server2 ~]$ gpg --encrypt --armor -r CA83F5AF encrypt.txt

gpg: CA83F5AF: There is no assurance this key belongs to the named user

pub 2048R/CA83F5AF 2011-08-15 xiyou <sjx@server1.example.com>

Primary key fingerprint: 7F44 7AE0 A7C2 6E89 6C68 6FE2 5572 8249 3F7B CEB5

Subkey fingerprint: 8FCB BF3E 2D51 563F 1C3F 2440 FC81 0D73 A353 A3BF

It is NOT certain that the key belongs to the person named

in the user ID. If you *really* know what you are doing,

you may answer the next question with yes.

Use this key anyway? (y/N) y

●将加密文件复制合作伙伴。

[wxh@server2 ~]$ scp encrypt.txt.asc server1.example.com:~

●解密合作伙伴已加密的文件并验证您可以查看他们所发送的消息。

[wxh@server1 ~]$ gpg --decrypt encrypt.txt.asc

You need a passphrase to unlock the secret key for

user: "xiyou <sjx@localhost>"

2048-bit RSA key, ID DB58BFCE, created 2011-08-15 (main key ID CA83F5AF)

can't connect to `/home/wxh/.gnupg/S.gpg-agent': No such file or directory

gpg: encrypted with 2048-bit RSA key, ID DB58BFCE, created 2011-08-15

"xiyou<sjx@server1.example.com>"

xi'an university of posts and telecommunications
分享到:
评论

相关推荐

    c# 实现对文件的GPG 加密与解密

    c# 使用DidiSoft.Pgp来实现对文件的GPG 加密与解密 。GPG非对称加密需要公钥和私钥 :你的公钥的作用 :别人用来给你发加密的信息&别人验证你的签名,即加密&验证(别人来做) 你的私钥的作用 :你用来创建签名&...

    PGP 加解密及签名验签示例

    PGP 加解密及签名验签示例

    ironhide:轻松加密和解密文件给用户和组的工具。 与GPG类似,但可大规模使用

    用法示例下面的示例演示如何创建新的IronHide组,将文件加密到该组,然后将其他用户添加为该组的成员,以便他们有权解密文件。 $ ironhide group:create engineering$ ironhide file:encrypt -g engine

    bouncy-gpg:再次将Bouncy Castle与OpenPGP结合使用会很有趣!

    只需几行代码即可加密,解密,签名和验证GPG / PGP文件 通过使用读取加密文件来保护所有 您甚至可以-永远不要让纯文本打入您的服务器磁盘! 例子 Bouncy GPG内置了几个。 密钥管理 Bouncy GPG支持读取gpg 密钥环和...

    02.win_bin.rar

    gpg window可执行文件,用于加密解密文件,安全不可破解,精简版,以命令行方式运行。 blat.exe gpg.exe gpgkeys_curl.exe gpgkeys_finger.exe gpgkeys_hkp.exe gpgkeys_ldap.exe gpgsplit.exe gpgv...

    sops-decrypt:GitHub动作来解密sops加密文件

    GitHub动作,可用于解密SOPS加密文件。 输入项 version 必须用于解密文件的SOPS二进制文件的必需版本 file 必须解密的SOPS加密文件的相对路径 gpg_key 必需的可以对文件进行解密的Base64编码的专用GPG密钥 ...

    kubesec:Kubernetes的安全秘密管理(具有gpg,Google Cloud KMS和AWS KMS后端)

    下面显示了一个加密的Secret的示例(请注意,只有“数据”被加密(并且密钥保持不变)): apiVersion : v1kind : Secretmetadata : name : myapp-default-0type : Opaquedata : KEY : TUFkWD1iuKs=.O....D...= ...

    docker-gemstash-aws:适用于AWS的Gemstash映像

    加密供CircleCI使用的GPG密钥: openssl aes-256-cbc \ -e \ -md sha1 \ -in ./config/secrets/ci/gpg.private \ -out ./.circleci/gpg.private.enc \ -k " &lt;passphrase&gt; " 要检查解密是否正常工作: ...

    janus:共享CI部署工具

    gpg也可以用于密钥解密(使用对称密码)。 该解决方案比openssl加密更可移植。 rev , curl和其他一些基本的bash命令是安装程序脚本所必需的。 Travis的标准版,可以按照以下示例添加到AppVeyor的PATH中 安装Janus...

    jpgpj:Java相当不错的隐私夹具

    JPGPJ API仅限于文件加密,签名,解密和验证。 它不具有生成,更新或签署密钥的功能,也不能进行清除签名或分离签名的功能。 这是Alice为Bob加密和签名文件的示例: new Encryptor ( new Key ( new File &#40; " ...

    generate-secure-pillar:盐安全Struts工具

    创建和更新YAML文件中的加密内容或解密加密内容 用法 generate-secure-pillar [全局选项]命令[命令选项] [参数...] 版本1.0.574 作者 埃德·席尔瓦(Ed Silva) 自制安装 brew tap esilva-everbridge/homebrew-...

Global site tag (gtag.js) - Google Analytics