[Turn] use GnuPG Introduction

   First, what is GPG

Getting Started Tutorial GPG encryption tool

        To understand what is GPG, we must first understand  PGP .

        In 1991, a programmer  Phil Zimmermann  in order to avoid government surveillance, developed encryption software PGP. The software is very easy to use, rapid spread, it has become an indispensable tool for many programmers. However, it is commercial software, not free to use. So, the Free Software Foundation decided to develop a replacement for the PGP, named GnuPG. This is the origin of GPG.

        GPG has many uses, this article introduces file encryption. As encrypted messages, different mail clients have different settings, please refer to the Ubuntu website presentation .

        Use of this environment for the Linux command line. If you master the command line, Windows  or  Mac OS  client, it is very easy to grasp. GPG is not difficult to learn, learn it, you can easily transmit encrypted information from. Readers are advised to follow a step by step tutorial to do, for each command are self-testing.

Getting Started Tutorial GPG encryption tool

        Second, the installation

        There are two ways to install GPG. You can download the source code , compile it yourself installation.

./configure

make

make install

        You can install the compiled binary packages.

# Debian / Ubuntu environment

sudo apt-get install gnupg

# Fedora environment

yum install gnupg

        After installation is complete, type the following command:

gpg --help

        If the screen displays help GPG, it means the installation was successful.

        Third, generate keys

        After successful installation, use the gen-ken parameters to generate your own key.

gpg --gen-key

        回车以后,会跳出一大段文字:

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

请选择您要使用的密钥种类:

(1) RSA and RSA (default)

(2) DSA and Elgamal

(3) DSA (仅用于签名)

(4) RSA (仅用于签名)

您的选择?

        第一段是版权声明,然后让用户自己选择加密算法。默认选择第一个选项,表示加密和签名都使用 RSA 算法。

        然后,系统就会问你密钥的长度。

RSA 密钥长度应在 1024 位与 4096 位之间。

您想要用多大的密钥尺寸?(2048)

        密钥越长越安全,默认是 2048 位。

        接着,设定密钥的有效期。

请设定这把密钥的有效期限。

0 = 密钥永不过期

<n> = 密钥在 n 天后过期

<n>w = 密钥在 n 周后过期

<n>m = 密钥在 n 月后过期

<n>y = 密钥在 n 年后过期

密钥的有效期限是?(0)

        如果密钥只是个人使用,并且你很确定可以有效保管私钥,建议选择第一个选项,即永不过期。回答完上面三个问题以后,系统让你确认。

以上正确吗?(y/n)

        输入y,系统就要求你提供个人信息。

您需要一个用户标识来辨识您的密钥;本软件会用真实姓名、注释和电子邮件地址组合成用户标识,如下所示:

"Heinrich Heine (Der Dichter) <[email protected]>"

真实姓名:

电子邮件地址:

注释:

        "真实姓名"填入你姓名的英文写法,"电子邮件地址"填入你的邮件地址,"注释"这一栏可以空着。

        然后,你的"用户 ID"生成了。

您选定了这个用户标识:

"Ruan YiFeng <[email protected]>"

        我的"真实姓名"是 Ruan YiFeng,"电子邮件地址"是 [email protected],所以我的"用户 ID"就是"Ruan YiFeng <[email protected]>"。系统会让你最后确认一次。

更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)?

        输入O表示"确定"。

        接着,系统会让你设定一个私钥的密码。这是为了防止误操作,或者系统被侵入时有人擅自动用私钥。

您需要一个密码来保护您的私钥:

        然后,系统就开始生成密钥了,这时会要求你做一些随机的举动,以生成一个随机数。

我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。

        几分钟以后,系统提示密钥已经生成了。

gpg: 密钥 EDDD6D76 被标记为绝对信任

公钥和私钥已经生成并经签名。

        请注意上面的字符串"EDDD6D76",这是"用户 ID"的 Hash 字符串,可以用来替代"用户 ID"。

        这时,最好再生成一张"撤销证书",以备以后密钥作废时,可以请求外部的公钥服务器撤销你的公钥。

gpg --gen-revoke [用户 ID]

        上面的"用户 ID"部分,可以填入你的邮件地址或者 Hash 字符串(以下同)。

        四、密钥管理

        4. 1 列出密钥

        list-keys 参数列出系统中已有的密钥.

gpg --list-keys

        显示结果如下:

/home/ruanyf/.gnupg/pubring.gpg

-------------------------------

pub 4096R/EDDD6D76 2013-07-11

uid Ruan YiFeng <[email protected]>

sub 4096R/3FA69BE4 2013-07-11

        第一行显示公钥文件名(pubring.gpg),第二行显示公钥特征(4096 位,Hash 字符串和生成时间),第三行显示"用户 ID",第四行显示私钥特征。

        如果你要从密钥列表中删除某个密钥,可以使用 delete-key 参数。

gpg --delete-key [用户 ID]

        4. 2 输出密钥

        公钥文件(.gnupg/pubring.gpg)以二进制形式储存,armor 参数可以将其转换为 ASCII 码显示。

gpg --armor --output public-key.txt --export [用户 ID]

        "用户 ID"指定哪个用户的公钥,output 参数指定输出文件名(public-key.txt)。

        类似地,export-secret-keys 参数可以转换私钥。

gpg --armor --output private-key.txt --export-secret-keys

        4. 3 上传公钥

        公钥服务器是网络上专门储存用户公钥的服务器。send-keys 参数可以将公钥上传到服务器。

gpg --send-keys [用户 ID] --keyserver hkp://subkeys.pgp.net

        使用上面的命令,你的公钥就被传到了服务器 subkeys.pgp.net,然后通过交换机制,所有的公钥服务器最终都会包含你的公钥。

        由于公钥服务器没有检查机制,任何人都可以用你的名义上传公钥,所以没有办法保证服务器上的公钥的可靠性。通常,你可以在网站上公布一个公钥指纹,让其他人核对下载到的公钥是否为真。fingerprint 参数生成公钥指纹。

gpg --fingerprint [用户 ID]

        4. 4 输入密钥

        除了生成自己的密钥,还需要将他人的公钥或者你的其他密钥输入系统。这时可以使用 import 参数。

gpg --import [密钥文件]

        为了获得他人的公钥,可以让对方直接发给你,或者到公钥服务器上寻找。

gpg --keyserver hkp://subkeys.pgp.net --search-keys [用户 ID]

        正如前面提到的,我们无法保证服务器上的公钥是否可靠,下载后还需要用其他机制验证.

        五、加密和解密

        5. 1 加密

        假定有一个文本文件 demo.txt,怎样对它加密呢?

        encrypt 参数用于加密。

gpg --recipient [用户 ID] --output demo.en.txt --encrypt demo.txt

        recipient 参数指定接收者的公钥,output 参数指定加密后的文件名,encrypt 参数指定源文件。运行上面的命令后,demo.en.txt 就是已加密的文件,可以把它发给对方。

        5. 2 解密

        对方收到加密文件以后,就用自己的私钥解密。

gpg --decrypt demo.en.txt --output demo.de.txt

        decrypt 参数指定需要解密的文件,output 参数指定解密后生成的文件。运行上面的命令,demo.de.txt 就是解密后的文件。

        GPG 允许省略 decrypt 参数。

gpg demo.en.txt

        运行上面的命令以后,解密后的文件内容直接显示在标准输出。

        六、签名

        6. 1 对文件签名

        有时,我们不需要加密文件,只需要对文件签名,表示这个文件确实是我本人发出的。sign 参数用来签名。

gpg --sign demo.txt

        运行上面的命令后,当前目录下生成 demo.txt.gpg 文件,这就是签名后的文件。这个文件默认采用二进制储存,如果想生成 ASCII 码的签名文件,可以使用 clearsign 参数。

gpg --clearsign demo.txt

        运行上面的命令后 ,当前目录下生成 demo.txt.asc 文件,后缀名 asc 表示该文件是 ASCII 码形式的。

        如果想生成单独的签名文件,与文件内容分开存放,可以使用 detach-sign 参数。

gpg --detach-sign demo.txt

        运行上面的命令后,当前目录下生成一个单独的签名文件 demo.txt.sig。该文件是二进制形式的,如果想采用 ASCII 码形式,要加上 armor 参数。

gpg --armor --detach-sign demo.txt

        6. 2 签名+加密

        上一节的参数,都是只签名不加密。如果想同时签名和加密,可以使用下面的命令。

gpg --local-user [发信者 ID] --recipient [接收者 ID] --armor --sign --encrypt demo.txt

        local-user parameter specifies the sender's signature private key, recipient parameters specify the public key encrypted with the recipient, armor parameter indicates the display using the ASCII code form, sign parameter indicates the need to sign, encrypt Indicates that the specified source file.

        6.3 verify signatures

        We received the documents after the signature of someone else, you need to use the other's public key to verify whether the signature is true. parameter is used to verify authentication.

gpg --verify demo.txt.asc demo.txt

        For example, OpenVPN  website provides gpg signature file every download package. You can according to its instructions , verify that the download package is true.

        Seven reference documents

        1. Paul Heinlein, GPG Quick Start

        2. Ubuntu help,GnuPrivacyGuardHowto

        3. KNL, GnuPG Tutorial

        4. Alan Eliasen. GPG Tutorial

        5.  GnuPG Pocket HOWTO (Chinese version)

        6. The GNU Privacy Handbook

Guess you like

Origin www.cnblogs.com/exmyth/p/11567167.html