gpg加解密教程

我们输入下列命令查看本机是否安装

  Linux$rpm-qa│grep gnupg
  gnupg-1.4.5-11
  说明已经安装
接下来生成密钥
 

[root@localhost tmp]# gpg --gen-key
gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

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) DSA and Elgamal (default)
   (2) DSA (sign only)
   (5) RSA (sign only)
Your selection? 1
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (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)
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: kingsoft
Email address: zhangkai2@kingsoft.com
Comment: kingsoft
You selected this USER-ID:
    "kingsoft (kingsoft) <zhangkai2@kingsoft.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.
+++++++++++++++++++++++++.++++++++++++++++++++++++++++++..++++++++++..+++++++++++++++.++++++++++.+

+++++++++.++++++++++.++++++++++..++++++++++>++++++++++........................................>+++

++.......<+++++..>+++++<.+++++.>+++++..+++++
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 ECA840EB 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:   signed:   trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   1024D/ECA840EB 2012-07-02
      Key fingerprint = 8090 3DC7 C054 ABD0 C51F  25DC 5FB6 882D ECA8 40EB
uid                  kingsoft (kingsoft) <zhangkai2@kingsoft.com>
sub   2048g/3DF27DB5 2012-07-02

[root@localhost tmp]# ls
cront.1427  kfs_result

导出公钥
[root@localhost tmp]# gpg --export --armor kingsoft > gpgkey.pub.asc  (kingsoft根据自己前面取的real name 修改)
[root@localhost tmp]# ls
cront.1427  gpgkey.pub.asc  kfs_result

导出私钥

[root@localhost tmp]# gpg -o seckey.asc --export-secret-keys kingsoft  (kingsoft根据自己前面取的real name 修改)

 

 

在其他机器上面,上传gpgkey.pub.asc  与 seckey.asc

[root@localhost tmp]# gpg --import gpgkey.pub.asc

添加信任kingsoft名字

[root@localhost tmp]# gpg --edit-key kingsoft

gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Secret key is available.

pub  1024D/ECA840EB  created: 2012-07-02  expires: never       usage: SC 
                     trust: ultimate      validity: ultimate
sub  2048g/3DF27DB5  created: 2012-07-02  expires: never       usage: E  
[ultimate] (1). kingsoft (kingsoft) <zhangkai2@kingsoft.com>

Command> trust
pub  1024D/ECA840EB  created: 2012-07-02  expires: never       usage: SC 
                     trust: ultimate      validity: ultimate
sub  2048g/3DF27DB5  created: 2012-07-02  expires: never       usage: E  
[ultimate] (1). kingsoft (kingsoft) <zhangkai2@kingsoft.com>

Please decide how far you trust this user to correctly verify other users' keys
(by looking at passports, checking fingerprints from different sources, etc.)

  1 = I don't know or won't say
  2 = I do NOT trust
  3 = I trust marginally
  4 = I trust fully
  5 = I trust ultimately
  m = back to the main menu

Your decision? 5
Do you really want to set this key to ultimate trust? (y/N) y

pub  1024D/ECA840EB  created: 2012-07-02  expires: never       usage: SC 
                     trust: ultimate      validity: ultimate
sub  2048g/3DF27DB5  created: 2012-07-02  expires: never       usage: E  
[ultimate] (1). kingsoft (kingsoft) <zhangkai2@kingsoft.com>

Command> quit

 

现在我们在这台机器上进行加密文件操作

[root@localhost tmp]# cat zk.txt
test
zhangkai

[root@localhost tmp]# ls
zk.txt
[root@localhost tmp]# gpg -e -r kingsoft zk.txt
[root@localhost tmp]# ls
zk.txt  zk.txt.gpg

zk.txt.gpg即是加密后的文件

 

现在我们在这台机器上进行解密文件操作

[root@localhost tmp]# gpg --import seckey.asc

[root@localhost tmp]# gpg -o 123.txt -d zk.txt.gpg (-o 指定解密后的文件名)

You need a passphrase to unlock the secret key for
user: "kingsoft (kingsoft) <zhangkai2@kingsoft.com>"
2048-bit ELG-E key, ID 3DF27DB5, created 2012-07-02 (main key ID ECA840EB)

Enter passphrase:

输入自己的密语就可以解密了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值