首页 / 操作系统 / Linux / Linux下使用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-keygpg (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? EnterRSA keys may be between 1024 and 4096 bits long.What keysize do you want? (2048) EnterRequested keysize is 2048 bitsPlease 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 yearsKey is valid for? (0) EnterKey does not expire at allIs this correct? (y/N) yGnuPG needs to construct a user ID to identify your key.Real name: shangjxEmail address: shangjx13@gmail.comComment: EnterYou selected this USER-ID:"xiyou<sjx@server1.example.com>"Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? oYou need a Passphrase to protect your secret key.can"t connect to `/home/linuxidc/.gnupg/S.gpg-agent": No such file or directorygpg-agent[2008]: directory `/home/linuxidc/.gnupg/private-keys-v1.d" created(此时会弹出图形应用程序,输入并验证密钥)We need to generate a lot of random bytes. It is a good idea to performsome other action (type on the keyboard, move the mouse, utilize thedisks) during the prime generation; this gives the random numbergenerator a better chance to gain enough entropy.We need to generate a lot of random bytes. It is a good idea to performsome other action (type on the keyboard, move the mouse, utilize thedisks) during the prime generation; this gives the random numbergenerator a better chance to gain enough entropy.gpg: key CA83F5AF marked as ultimately trustedpublic and secret key created and signed.gpg: checking the trustdbgpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust modelgpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1upub 2048R/CA83F5AF 2011-08-15Key fingerprint = F886 17A2 F832 B545 6E27 B424 E539 26BF CA83 F5AFuidxiyou<sjx@server1.example.com>sub 2048R/DB58BFCE 2011-08-15若要导出密钥,请在上面的输出中查找密钥ID 。可以在上面的pub 2048R/ 输出的后面找到。在给示例中密钥ID 是CA83F5AF 。以下示例将显示使用该密钥ID 的命令。●导出您的公钥,与合作伙伴共享。[linuxidc@server1 ~]$ gpg -a -o ~/pub.key --export CA83F5AF●将导出的公钥复制到合作伙伴的server2 上。[linuxidc@server1 ~]$ scp pub.key server2.example.com:~●导入合作伙伴的公钥。[linuxidc@server2 ~]$ gpg --import pub.keygpg: directory `/home/linuxidc/.gnupg" createdgpg: new configuration file `/home/linuxidc/.gnupg/gpg.conf" createdgpg: WARNING: options in `/home/linuxidc/.gnupg/gpg.conf" are not yet active during this rungpg: keyring `/home/linuxidc/.gnupg/secring.gpg" createdgpg: keyring `/home/linuxidc/.gnupg/pubring.gpg" createdgpg: /home/linuxidc/.gnupg/trustdb.gpg: trustdb createdgpg: key CA83F5AF: public key "westos <linuxidc@server1.example.com>" importedgpg: Total number processed: 1gpg:imported: 1 (RSA: 1)●创建一个文本文件,内含供合作伙伴阅读的消息。[linuxidc@server2 ~]$ echo "xi"an university of posts and telecommunications" > encrypt.txt●用合作伙伴的公钥加密文件。www.2cto.com[linuxidc@server2 ~]$ gpg --encrypt --armor -r CA83F5AF encrypt.txtgpg: CA83F5AF: There is no assurance this key belongs to the named userpub 2048R/CA83F5AF 2011-08-15 xiyou <sjx@server1.example.com>Primary key fingerprint: 7F44 7AE0 A7C2 6E89 6C68 6FE2 5572 8249 3F7B CEB5Subkey fingerprint: 8FCB BF3E 2D51 563F 1C3F 2440 FC81 0D73 A353 A3BFIt is NOT certain that the key belongs to the person namedin 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●将加密文件复制合作伙伴。[linuxidc@server2 ~]$ scp encrypt.txt.asc server1.example.com:~●解密合作伙伴已加密的文件并验证您可以查看他们所发送的消息。[linuxidc@server1 ~]$ gpg --decrypt encrypt.txt.ascYou need a passphrase to unlock the secret key foruser: "xiyou <sjx@localhost>"2048-bit RSA key, ID DB58BFCE, created 2011-08-15 (main key ID CA83F5AF)can"t connect to `/home/linuxidc/.gnupg/S.gpg-agent": No such file or directorygpg: encrypted with 2048-bit RSA key, ID DB58BFCE, created 2011-08-15"xiyou<sjx@server1.example.com>"xi"an university of posts and telecommunicationsopenSUSE下玩转GPG图文详解 http://www.linuxidc.com/Linux/2015-01/112425.htmLinux下gpg的简单应用 http://www.linuxidc.com/Linux/2013-06/85291.htmGPG作者差点破产,危机已过 http://www.linuxidc.com/Linux/2015-02/113004.htm本文永久更新链接地址