一、非对称加密
因信息安全要求,将保密信息通过数字证书进行加密,传送至对方后,对方进行解密,完成一个安全的通信过程。因加密和解密需要用密钥进行操作,如果使用同样的密钥,被他人获取后,可以代行操作,造成数据的不安全。
非对称加密大概是生产的密钥对=公钥+私钥 两种不同的数字证书,且两者或为配对,一个公钥对应一个私钥。这样的做的好处是服务器拥有私钥,公开发布公钥,所有人获得了公钥,发给服务器公钥加密的报文,服务器收到后拿私钥进行解密,再回传消息说收到了通过私钥加密的报文回复,这样客户端再次收到并解密,就完成了一次身份确认,如果其他人想冒充服务器,是没法解密客户端发来的消息,也就不能正常的回复,避免了钓鱼攻击。
二、SSH-KEYGEN
初步接触,是为了能免密码远程登录linux服务器,通过ssh-keygen生产一对密钥,服务器保留公钥,客户端保留私钥,完成身份确认,免密登录。
场景(1) linux 客户端 ssh linux服务器
步骤:Linux主机A生成密钥对,将公钥传至Linux主机B,配置Linux主机B ssh配置文件,修改登录方式及验证方式,重启服务。
[root@bogon ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Dl204hGMxtsfj3U2ifbVUim0N4BvJ3Bwa+ITylfxTrA root@bogon
The key's randomart image is:
+---[RSA 2048]----+
| . o. ..+* .|
| + .o +.oO..|
| . oo oo+E.B.|
| .oo++ O+X.+|
| . S+ X.+o= |
| o + o . |
| . |
| |
| |
+----[SHA256]-----+
[root@bogon ~]# ssh-copy-id 192.168.137.200 #这一步等于将公钥.pub文件复制到192.168.137.200:/root/.ssh/ 这个目录下; 也等同于scp到该服务器,然后切换root,使用cat xxx.pub >> /root/.ssh/authorized_keys 这个文件当中
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.137.200's password: #输入B主机root密码
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.137.200'"
and check to make sure that only the key(s) you wanted were added.
登录192.168.137.200
vi /etc/ssh/sshd_config
PubkeyAuthentication yes
再修改一下 authorized_keys文件权限为600,和./ssh/ 目录为700 #暂时不知道为什么,可能是为了安全。。
#systemctl restart sshd
[root@bogon ~]# ssh 192.168.137.200
Last login: Sat Feb 20 00:22:33 2021 from 192.168.137.1
场景(2)Windows客户端 SSH Linux主机
首先在需要被登录的linux主机使用ssh-keygen生成公钥私钥,使用 cat ./id_rsa.pub >> authorized_keys 文件当中,这样就给该服务器安装好了公钥,再将私钥id_rsa文件导出到windows客户端
最后用puttykeygen 加载id_rsa 另存为私钥,配置putty使用私钥登录,在SSH—Auth里
最后putty登录输入完用户名就跳过了密码输入,直接成功登录