一、数据的加密方式

数据加密有三种方式:


1、对称加密(算法有:DES、AES、3DES、)加密和解密使用同一个密钥


2、非对称加密(RSA、DSA、ELGamal等等)一共四把钥匙,用公钥加密数据,只能使用与之配对的私钥解密;反之亦然


3、单项加密(md5 sha1 sha2 sha128 sha256 sha512等)算出数据的hash值,当数据发生微弱的变化都会产生雪崩效应,所生成的新的hash值将变得完全不一样,主要用于数据校验。

二、几种加密方式的特点

1、对称加密:


优点:加密、解密使用同一个密钥,效率高,将原始数据分割成固定大小的块,逐个进行加密
缺点:密钥过多,密钥分发,最主要的:数据来源无法确认


2、非对称秘钥加密:


优点:用公钥加密数据,只能使用与之配对的私钥解密;反之亦然,加密系数高
缺点:密钥长,加密解密效率低下,尤其解密效率极其低下。
适用场景:


数字签名:主要在于让接收方确认发送方身份


3、单项加密:


类似于指纹,将任意数据缩小成固定大小的“指纹”,任意长度的数据都可以,但是以固定长度输出,如果修改数据,则会产生雪崩效应,生成新的hash值完全不同。
但是无法从指纹反推回原始数据。
适用场景:


数据完整性校验,数据可靠性校验。

三、gpg加密工具

1、实现对称和非对称加密


1)、对称加密演示

  1. [root@newhostname app]# echo Ok ok ok ok ok > fist_encrypt
    [root@newhostname app]# ls
    fist_encrypt
    [root@newhostname app]# gpg -c fist_encrypt #对文件进行加密
    gpg: 已创建目录‘/root/.gnupg
    gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
    gpg: 警告:在‘/root/.gnupg/gpg.conf’里的选项于此次运行期间未被使用
    gpg: 钥匙环‘/root/.gnupg/pubring.gpg’已建立
    [root@newhostname app]# ls
    fist_encrypt fist_encrypt.gpg #fist_encrypt.gpg这个就是加密过的文件

执行 gpg -c fist_encrypt会出现两次如下图



输入一个密码,加密就完成了,我们来看一下这个文件fist_encrypt.gpg

  1. [root@newhostname app]# cat fistencrypt.gpg
    gKf
    );root@newhostname:/app[root@newhostname app]#

已经实现了加密


现在我们将这个文件拷贝到其他主机,并进行解密

  1. [root@joker-6-01 ~]# cd /app
    [root@joker-6-01 app]# rm -rf fist
    [root@joker-6-01 app]# gpg -d fist_encrypt.gpg >fist
    gpg: CAST5 encrypted data
    cant connect to `/root/.gnupg/S.gpg-agent’: No such file or directory
    gpg: encrypted with 1 passphrase
    Ok ok ok ok ok
    gpg: WARNING: message was not integrity protected
    [root@joker-6-01 app]# ls
    fist_encrypt.gpg
    [root@joker-6-01 app]#

解密需要输入密码,同上图


现在我们看一下生成的新文件

  1. [root@joker-6-01 app]# cat fist
    Ok ok ok ok ok

总结: 通过gpg -c file 进行文件加密,并生成一个 file.gpg的新文件 ,通过 gpg -d file.gpg >file 可以进行解密


 

2)通过gpg工具进行非对称的文件加密(我们需要两对秘钥)


hostA上操作:

  1. [root@newhostname app]# gpg gen-key #创建密钥对
    gpg (GnuPG) 2.0.22; 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.

  2. gpg: 钥匙环‘/root/.gnupg/secring.gpg’已建立
    请选择您要使用的密钥种类:
    (1) RSA and RSA (default)
    (2) DSA and Elgamal
    (3) DSA (仅用于签名)
    (4) RSA (仅用于签名)
    您的选择? 1
    RSA 密钥长度应在 1024 位与 4096 位之间。
    您想要用多大的密钥尺寸?(2048)1024
    您所要求的密钥尺寸是 1024
    请设定这把密钥的有效期限。
    0 = 密钥永不过期
    <n> = 密钥在 n 天后过期
    <n>w = 密钥在 n 周后过期
    <n>m = 密钥在 n 月后过期
    <n>y = 密钥在 n 年后过期
    密钥的有效期限是?(0) 0
    密钥永远不会过期
    以上正确吗?(y/n)y

1到这里会出现下面四个图,意思是让我们再对私钥进行加密,我们一路回车,因为只是测试,所以就不再加密了。






一路回车过后,秘钥生成完毕

  1. 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>”

  2. 真实姓名:zhang
    电子邮件地址:765030447@qq.com
    注释:test
    您选定了这个用户标识:
    zhang (test) <765030447@qq.com>”

  3. 更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)?o
    您需要一个密码来保护您的私钥。

  4. 您不想要有密码――这个想法实在是遭透了!
    不过,我仍然会照您想的去做。您任何时候都可以变更您的密码,仅需要
    再次执行这个程序,并且使用“—edit-key”选项即可。

  5. 我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
    鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。
    我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
    鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。
    gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
    gpg: 密钥 11F74DDB 被标记为绝对信任
    公钥和私钥已经生成并经签名。

  6. gpg: 正在检查信任度数据库
    gpg: 需要 3 份勉强信任和 1 份完全信任,PGP 信任模型
    gpg: 深度:0 有效性: 1 已签名: 0 信任度:0-,0q0n0m0f1u
    pub 1024R/11F74DDB 2018-01-06
    密钥指纹 = E1F8 DE1D B979 48F4 A216 DCB7 D5B9 E6A3 11F7 4DDB
    uid zhang (test) <765030447@qq.com>
    sub 1024R/710D443B 2018-01-06

我们看一下我们生成的秘钥,在自己的家目录下的.gnupg目录下

  1. [root@newhostname .gnupg]# pwd
    /root/.gnupg
    [root@newhostname .gnupg]# ll
    总用量 28
    -rw———-. 1 root root 7680 1 6 19:45 gpg.conf
    drwx———. 2 root root 6 1 6 19:45 private-keys-v1.d
    -rw———-. 1 root root 675 1 6 20:12 pubring.gpg
    -rw———-. 1 root root 675 1 6 20:12 pubring.gpg~
    -rw———-. 1 root root 600 1 6 20:12 random_seed
    -rw———-. 1 root root 1338 1 6 20:12 secring.gpg
    srwxr-xr-x. 1 root root 0 1 6 20:12 S.gpg-agent
    -rw———-. 1 root root 1280 1 6 20:12 trustdb.gpg

  2. pubring.gpg 这个是公钥
    secring.gpg 这个是私钥

我们导出公钥

  1. [root@newhostname .gnupg]# gpg list-key #可以查看公钥

  2. /root/.gnupg/pubring.gpg

    pub 1024R/11F74DDB 2018-01-06
    uid zhang (test) <765030447@qq.com>
    sub 1024R/710D443B 2018-01-06

  3. [root@newhostname .gnupg]# gpg -a export -o zhang.pubkey #导出公钥并生成可视的字符
    [root@newhostname .gnupg]# ls
    gpg.conf private-keys-v1.d pubring.gpg pubring.gpg~ random_seed secring.gpg S.gpg-agent trustdb.gpg zhang.pubkey
    [root@newhostname .gnupg]# ll
    总用量 32
    -rw———-. 1 root root 7680 1 6 19:45 gpg.conf
    drwx———. 2 root root 6 1 6 19:45 private-keys-v1.d
    -rw———-. 1 root root 675 1 6 20:12 pubring.gpg
    -rw———-. 1 root root 675 1 6 20:12 pubring.gpg~
    -rw———-. 1 root root 600 1 6 20:12 random_seed
    -rw———-. 1 root root 1338 1 6 20:12 secring.gpg
    srwxr-xr-x. 1 root root 0 1 6 20:12 S.gpg-agent
    -rw———-. 1 root root 1280 1 6 20:12 trustdb.gpg
    -rw-rr—. 1 root root 1020 1 6 20:37 zhang.pubkey
    [root@newhostname .gnupg]# cat zhang.pubkey #查看导出的公钥
    ——-BEGIN PGP PUBLIC KEY BLOCK——-
    Version: GnuPG v2.0.22 (GNU/Linux)

  4. mI0EWlC9RgEEAMaYQLcdCujOTwdKiBLPfSrhwceFqt7FGT/xBggSNHs8c9EyQ/8T
    ls7PiiaK4drm0mdPtIPr/pa6m2T20mFB0DMpeB9SEI+z7v/jWB/Y1xxINjGvFtKP
    JSwFSa8qYUNLBYpBIPoa5IlXpzrkG+gRvHh++7yu8e4e06oA9lAzWT2rABEBAAG0
    H3poYW5nICh0ZXN0KSA8NzY1MDMwNDQ3QHFxLmNvbT6IuQQTAQIAIwUCWlC9RgIb
    AwcLCQgHAwIBBhUIAgkKCwQWAgMBAh4BAheAAAoJENW55qMR903btFMEAIm/9HWT
    ofXbTTVUbjFMV8MSYU+TRj1n2xuPKDaWLgJyb2cH9c/57RMS5bHqvqEBxOzrtnXB
    2FPYiS8VHgLhvnUkMqer1WA7RjVESAcp52UKvoI+7yD5K7vABMg9yDPnNag2EKLr
    H7db0wspYo5ox3w8AZsJj3dUMLgipAQDUpckuI0EWlC9RgEEAK0PxCRuHLL3XUC6
    wDr4gSRj5TVcwVuSOE0ECBZakbDeuljhq9Fn0UR8FfdiPOw4Cakt0RWiYh9oxdsF
    R3Y6DL6fzBAwozxz1I5NXtTj68HY+/6bEiHtz4xvFPR9YEwrqzPZWaWMaTRgtAL6
    n/ONYQKPHa82p2qIdzv7+y59u9zRABEBAAGInwQYAQIACQUCWlC9RgIbDAAKCRDV
    ueajEfdN204xA/9ZVVqFAnFti6vogSCzPMFDj6s4M66EBGEUIKkK9cnw1vzXzsv3
    v8YfevZsWGMX7Cag89a3ox+qIVvHhsEkL3TXEAmrYLZn9/T/E1yRQnYpQcI9uvMg
    /UAy3j0HtOzui4ofMuGoNJ1V7hOMt8Jlzt2+WQVvVogHcNWmEUSWcmIp8w==
    =o0NK
    ——-END PGP PUBLIC KEY BLOCK——-
    [root@newhostname .gnupg]#

将公钥文件拷贝到hostB机器

  1. [root@newhostname .gnupg]# scp zhang.pubkey 172.18.30.254:~
    root@172.18.30.254s password:
    zhang.pubkey 100% 1020 2.9MB/s 00:00

2)、在hostB上操作:


同样的方法生成自己的密钥对

  1. [root@joker-6-01 ~]# 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.

  2. 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?
    RSA keys may be between 1024 and 4096 bits long.
    What keysize do you want? (2048) 1024
    Requested keysize is 1024 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)
    Key is valid for? (0)
    Key does not expire at all
    Is this correct? (y/N) y

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

  4. Real name: shang
    Email address: 765030447@qq.com
    Comment: test
    You selected this USER-ID:
    shang (test) <765030447@qq.com>”

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

查看秘钥,并导出公钥

  1. [root@joker-6-01 .gnupg]# gpg list-key

  2. /root/.gnupg/pubring.gpg

    pub 1024R/7FFA70D9 2018-01-06
    uid shang (test) <765030447@qq.com>
    sub 1024R/E774B778 2018-01-06
    [root@joker-6-01 .gnupg]# gpg -a export -o shang.pubkey
    [root@joker-6-01 .gnupg]# ls
    gpg.conf private-keys-v1.d pubring.gpg pubring.gpg~ random_seed secring.gpg shang.pubkey trustdb.gpg
    [root@joker-6-01 .gnupg]# ll
    total 36
    -rw———-. 1 root root 7856 Nov 7 16:31 gpg.conf
    drwx——— 2 root root 4096 Jan 6 20:02 private-keys-v1.d
    -rw———- 1 root root 674 Jan 6 21:02 pubring.gpg
    -rw———- 1 root root 674 Jan 6 21:02 pubring.gpg~
    -rw———- 1 root root 600 Jan 6 21:02 random_seed
    -rw———- 1 root root 1338 Jan 6 21:02 secring.gpg
    -rw-rr 1 root root 1016 Jan 6 21:05 shang.pubkey
    -rw———-. 1 root root 1280 Jan 6 21:02 trustdb.gpg
    [root@joker-6-01 .gnupg]# cat shang.pubkey
    ——-BEGIN PGP PUBLIC KEY BLOCK——-
    Version: GnuPG v2.0.14 (GNU/Linux)

  3. mI0EWlDI3QEEANDr6fIRum7F1tdaJH6TI+O5QpKf3f1zEacmqqH3iQ+eIJkUZDTG
    yC9k87zKQVaDgbZlcn38Lf/u7X3pRemsGa8ZMkmviHCc7gfW5C0NmMse7/dKGUmt
    6xOnVRsgk+WYDGwpxI0rhRxzDg0AIpNH20wZpK6bgNwoC8i5zIO605tRABEBAAG0
    H3NoYW5nICh0ZXN0KSA8NzY1MDMwNDQ3QHFxLmNvbT6IuAQTAQIAIgUCWlDI3QIb
    AwYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQNG0fpn/6cNkZ7wP+Oy6X24Tt
    WZQ5T4LYc9go7RuU8vDIMHQzEC0CXO9pi3ZU7quEYDfd5N9WD61+jKY9s79L09tK
    14RQJByWaFFXJwWyrjzu4BzMZiAmjDf9PA5hpUVFOAdQbMKENlXOh3Rxsi65EsAH
    Hem/Plbr75dEN+CQV8emlZ2tDDBYhmbbkzC4jQRaUMjdAQQAzg1FrRac6KyerWt+
    oJ83B9eDLn6Yq9xBA6W1MdRTZKOEKygkbGP7Slr7e7lWHKinlfRL7+9+2IjzurQZ
    ndWO/msAKxdnqIMbg7Xx1eXWr0VwvFAz2AHDQz+Ls3mkJ40ZCPbHKXRb5+7USWKB
    VHCubgMJ49VfTrxBwzCo2M5K3+MAEQEAAYifBBgBAgAJBQJaUMjdAhsMAAoJEDRt
    H6Z/+nDZLW0EAJ3oHrPWIGczgzSZ+ociwsmaVggqAky3BCfWhbUgJBEOAEh+SMSw
    WueVIKNT/bA3SVUak0PmNz1IkXLRYmiPA/81+I9ezUx0ac1noYO2MzYfdmaCFSBM
    sfnfvpbk6/L0FY0aIhcuufCztIs+bsxpsXXYfq4C5hyMI1S1S8YS0pDX
    =DHkG
    ——-END PGP PUBLIC KEY BLOCK——-
    [root@joker-6-01 .gnupg]#

将hostB的公钥shang.pubkey拷贝到hostA

  1. [root@joker-6-01 .gnupg]# scp shang.pubkey 172.18.30.253:~
    The authenticity of host 172.18.30.253 (172.18.30.253)’ cant be established.
    RSA key fingerprint is a4:04:ea:5b:ce:18:0c:e4:e8:b4:51:47:ea:87:0b:2b.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 172.18.30.253 (RSA) to the list of known hosts.
    root@172.18.30.253s password:
    shang.pubkey 100% 1016 1.0KB/s 00:00

A机器拿到了B的公钥:shang.pubkey
B机器拿到了A的公钥:zhang.pubkey


 

现在在各个机器上导入新拿到的公钥

  1. hostA
    [root@newhostname app]# gpg import ~/shang.pubkey
    gpg: 密钥 7FFA70D9:公钥“shang (test) <765030447@qq.com>”已导入
    gpg: 合计被处理的数量:1
    gpg: 已导入:1 (RSA: 1)

  2. hostB
    [root@joker-6-01 ~]# gpg import zhang.pubkey
    gpg: key 11F74DDB: public key zhang (test) <765030447@qq.com>” imported
    gpg: Total number processed: 1
    gpg: imported: 1 (RSA: 1)


 

 

现在我们在A机器使用B的公钥加密/app/fist_encrypt,并将生成的文件拷贝到B

  1. [root@newhostname app]# gpg -e -r shang fist_encrypt
    gpg: E774B778:没有证据表明这把密钥真的属于它所声称的持有者

  2. pub 1024R/E774B778 2018-01-06 shang (test) <765030447@qq.com>
    主钥指纹: 5042 4F44 8DE2 F9EA 1A02 BBC8 346D 1FA6 7FFA 70D9
    子钥指纹: BB56 2ED6 84DA 2DB7 A6A6 AFF2 122E ED1C E774 B778

  3. 这把密钥并不一定属于用户标识声称的那个人。如果您真的知道自
    己在做什么,您可以在下一个问题回答 yes

  4. 无论如何还是使用这把密钥吗?(y/N)y
    [root@newhostname app]# ls
    fist_encrypt fist_encrypt.gpg


 

将新生成的加密文件拷贝到hostB上

  1. [root@newhostname app]# ls
    fist_encrypt fist_encrypt.gpg
    [root@newhostname app]# scp fist_encrypt.gpg root@172.18.30.254:/app
    root@172.18.30.254s password:
    fist_encrypt.gpg 100% 222 377.5KB/s 00:00
    [root@newhostname app]#

到B机器上,使用B的私钥进行解密

  1. [root@joker-6-01 app]# ls
    fist_encrypt.gpg

  2. [root@joker-6-01 app]# cat fist_encrypt.gpg

  3. %qXd46-}a>yݸ;n$
    jtyY T>AWK}@jBRW1
    ~0>[root@joker-6-01 app]#
    [root@joker-6-01 app]#

  4. [root@joker-6-01 app]# gpg -d fist_encrypt.gpg > fist_encrypt #对文件进行解密,并输出到fistt_encrypt内
    gpg: encrypted with 1024-bit RSA key, ID E774B778, created 2018-01-06
    shang (test) <765030447@qq.com>”

  5. [root@joker-6-01 app]# ls
    fist_encrypt fist_encrypt.gpg

  6. [root@joker-6-01 app]# cat fist_encrypt #解密完成
    Ok ok ok ok ok

对称的加密解密完成了,现在我们删除秘钥


hostA上操作:

  1. [root@newhostname app]# gpg delete-keys shang #在A上删除B的公钥
    gpg (GnuPG) 2.0.22; 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.

  2. pub 1024R/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>

  3. 要从钥匙环里删除这把密钥吗?(y/N)y
    [root@newhostname app]#

  1. [root@newhostname app]# gpg delete-secret-keys zhang #在A机器上删除自己的私钥
    gpg (GnuPG) 2.0.22; 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.

  2. sec 1024R/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>

  3. 要从钥匙环里删除这把密钥吗?(y/N)y
    这是一把私钥!――真的要删除吗?(y/N)y

  4. [root@newhostname app]# gpg delete-keys zhang #再删除自己的公钥
    gpg (GnuPG) 2.0.22; 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.

  5. pub 1024R/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>

  6. 要从钥匙环里删除这把密钥吗?(y/N)y

  7. 注意:必须先删除私钥再删除公钥


 

hostB上操作:

  1. [root@joker-6-01 app]# gpg delete-keys zhang #在B上删除A的公钥
    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.

  2. pub 1024R/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>

  3. Delete this key from the keyring? (y/N) y
    [root@joker-6-01 app]#

  1. [root@joker-6-01 app]# gpg delete-secret-keys shang #删除自己的私钥
    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.

  2. sec 1024R/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>

  3. Delete this key from the keyring? (y/N) y
    This is a secret key! - really delete? (y/N) y

  4. [root@joker-6-01 app]# gpg delete-keys shang #删除自己的公钥
    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.

  5. pub 1024R/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>

  6. Delete this key from the keyring? (y/N) y


 

命令总结:


对称加密方法:


1、gpg -c file 生成一个file.gpg的加密文件
2、gpg -d file.gpg > file 解密file.gpg并将输出存入file文件


非对称加密方法:


第一步:


A机器:


1、gpg —gen-key 生成公私钥对
2、gpg -a —export Akey_name.pubkey
3、scp Akey_name.pubkey B机器:~/


B机器:


1、gpg—gen-key 生成公私钥对
2、gpg -a —export Bkey_name.pubkey
3、scp Bkey_name.pubkey A机器:~/


第二步:


1、gpg —import ~/Bkey_name 在A机器上导入B机器的公钥
2、gpg —import ~/Akey_name 在B机器上导入A机器的公钥


第三步:


1、gpg -e -r Bkey_name file1 在A机器使用B的公钥对文件进行加密
2、gpg -e -r Akey_name file2 在B机器使用A的公钥对文件进行加密


第四步


1、scp file1.gpg B机器:~ 在A机器,将用B公钥加密的文件传输给B机器
2、scp file2.gpg A机器:~ 在B机器,将用A公钥加密的文件传输给A机器


第五步


1、gpg -d file2.gpg >file2 在A机器,将B传送来的文件解密(默认会使用A自己的私钥来解密)
2、gpg -d file1.gpg > file1 在B机器,将A传送来的文件解密(默认会使用B自己的私钥来解密)

文档更新时间: 2018-12-19 15:14   作者:张尚