ssh免密登陆和加密解密

一 丶实现无密码的远程管理

  1.生成公钥 私钥
    [root@room9pc14 桌面]# ssh-keygen
    [root@room9pc14 桌面]# ls /root/.ssh/

  2.上传公钥到虚拟机A
    [root@room9pc14 桌面]# ssh-copy-id root@192.168.4.7

  3.上传公钥到虚拟机B
    [root@room9pc14 桌面]# ssh-copy-id root@192.168.4.207

  4.补充在真机上
    [root@room9pc14 桌面]# ssh-add (声明私钥)

 

二、加密与解密

  对称加密:加密/解密用同一个密钥

  非对称加密:加密/解密用不同的密钥 {公钥(加密)、私钥(解密)}  

  口令认证登录   PasswordAuthentication no|yes
  密钥对认证登录

  对称加密
    DES,Data Encryption Standard

    AES,Advanced Encryption Standard

  非对称加密
    RSA,Rivest Shamirh Adleman

    DSA,Digital Signature Algorithm

  Hash散列技术,用于信息摘要(数据完整性)

    MD5,Message Digest Algorithm 5

    SHA,Secure Hash Algorithm
    特点:
      1. 根据输入的文本(长度不限),生成固定长度(比如128位)的摘要文本

      2. 只要输入的文本不同,则生成的摘要文本也不一样

GPG非对称加密/解密:

GPG介绍:

  GNU Privacy Guard

  最流行的数据加密、数字签名工具软件

例题:

  #useradd usera
  #useradd userb
  #echo 123456 | passwd --stdin usera
  #echo 123456 | passwd --stdin userb

+++++++++++++++++++++++++++

gpg 做对称加/解密

  usera ------ > userb
  发                   接
  加                   解

  #ssh -X usera@localhost
  #head -5 /etc/passwd > a.txt
  #cat a.txt
  #gpg -c a.txt  (提示输入密码)
  # ls a.txt.gpg
  # mv a.txt.gpg /tmp/

  #ssh -X userb@localhost
  #gpg -d /tmp/a.txt.gpg > my.txt (提示输入密码)
  #cat my.txt
+++++++++++++++++++++++++++
gpg 做非对称加/解密

  uesra ------- > userb
  发       接
  加       解
  公钥     私钥

userb
  ssh -X userb@localhost
  1 创建秘钥对(公钥 私钥)
    #rm -rf ~/.gnupg
    #gpg --gen-key
    #ls ~/.gnupg

    pubring.gpg(公钥)  secring.gpg(私钥)

  2 导出公钥
    #gpg -a --export  [真实姓名]  > /tmp/userb.pub

usera
  ssh -X usera@localhost
  1 导入userb用户的公钥
    #rm -rf ~/.gnupg
    #gpg  --import  /tmp/userb.pub
    #ls ~/.gnupg
  2 使用公钥加密明文文件,共享给userb用户
    #tail -5 /etc/passwd > test.txt
    #gpg  -e  -r  [真实姓名]  test.txt
    #ls  test.txt.gpg
    #mv test.txt.gpg   /tmp/
  3 userb用户使用私钥解密共享的加密文件
    #gpg -d /tmp/test.txt.gpg > my2.txt  (提示输入密码,调用私钥的密码)
    #cat my2/tmp/test.txt.gpg

+++++++++++++++++++++++++++++++
gpg 做数字签名(私钥签名 公钥验证签名)
  软件签名与验证过程

    软件官方以私钥对软件包执行数字签名

    用户下载软件包、软件官方的公钥

    以官方公钥验证软件包签名,确保软件完整性
userb 
  #head -2 /etc/passwd > my4.txt
  #gpg -b my4.txt (要求输入私钥密码) //数字签名
  #ls my4.txt.sig
  #mv my4.txt* /tmp/
  #ls /tmp/my4.txt*
  #gpg --fingerprint

usera 
  #gpg --verify /tmp/my4.txt.sig

 

 

 

posted @ 2018-05-29 19:31  wwchihiro  阅读(1074)  评论(0编辑  收藏  举报