linux下gpg的使用

最后更新:2019-03-21

root@iZuf6fbha9mr8eysoj3mabZ:~# gpg --gen-key
gpg (GnuPG) 1.4.16; Copyright (C) 2013 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 `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' 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)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 2048
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) 0
Key does not expire at all
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"

Real name: edgar615
Email address: xxx@qq.com
Comment: 
You selected this USER-ID:
    "edgar615 <xxx@qq.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

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.

Not enough random bytes available.  Please do some other work to give
the OS a chance to collect more entropy! (Need 231 more bytes)

卡在最后一步,好慢,搜了下

sudo apt-get install rng-tools

生成结果


..........+++++
...+++++
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: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key B228CACD 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/B228CACD 2019-03-21
      Key fingerprint = 8DBE 3BC5 34A6 1DC1 6942  4404 A63C C0D7 B228 CACD
uid                  edgar615 <xxx@qq.com>
sub   2048R/29514C52 2019-03-21

查看公钥

gpg --list-key

查看私钥

gpg --list-secret-keys

提取公钥

gpg -a --export newkey > newkey.asc

提取私钥

gpg -a --export-secret-keys newkey > newkey_pirv.asc

导入密钥

gpg --import newkey

使用公钥加密文件,生成filename.asc的加密文件

gpg -ea -r newkey filename

使用私钥解密文件,把filename.asc的加密文件解密成filename文件

gpg -o filename -d filename.asc

删除私钥

gpg --delete-secret-key newkey

删除公钥

gpg --delete-key newkey
Edgar

Edgar
一个略懂Java的小菜比