生成密鑰對的三種方式,用於key驗證


一.本文環境

  CentOS 8

公鑰加密,私鑰簽名是非常常見的安全方式。基本三種生成密鑰對的方式。

公鑰交換的原理圖

 

 

 1.客戶端發起連接請求

 2.服務端返回給客戶端自己的公鑰,以及一個會話ID

 3.客戶端用自己的公鑰異或會話ID,計算一個res值,並將服務器的公鑰加密

 4.服務器使用自己的私鑰解密,得到res值,並用res值異或會話ID計算,從而得到客戶端的公鑰

5.雙方完成公鑰交換。之后的所有通訊都會被加密。

 

二.生成密鑰對

  1.使用 gpg 生成密鑰對

1 [root@CentOS-8-LinuxIV ~]# gpg --gen-key

輸入此命令后,會出現一些需要你輸入的信息,如郵箱,名字,對密鑰的保護密碼等信息。

完成之后會在家目錄中有一個.gnupg/的目錄,其中公鑰私鑰還有配置都在此目錄中。

由於gpg生成的公鑰是二進制的不能直接查看,因此我們需要使用ascii導出來。

gpg --export -a -o pub.key 

這樣生成的文件就可以直接查看了

2 使用openssl生成密鑰對

  openssl生成的私鑰可以直接查看,需要借助私鑰生成公鑰,簡單快捷

1 [root@CentOS-8-LinuxIV .gnupg]# openssl genrsa -out file.key 2 Generating RSA private key, 2048 bit long modulus (2 primes) 3 .........................................................................................................................+++++
4 ...........+++++
5 e is 65537 (0x010001)

  接下來使用私鑰生成私鑰

[root@CentOS-8-LinuxIV .gnupg]# openssl rsa -in file.key -pubout -out file.key.pub
writing RSA key

查看公鑰

 1 [root@CentOS-8-LinuxIV .gnupg]# cat file.key.pub  2 -----BEGIN PUBLIC KEY-----
 3 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0GPV7g2KwLCcRnfQmhk6  4 q5MqKq3t5HzD6pYSSzbNmhinalSSjRPUiyX0jDQvrJpARhFw7PGFHCUXfyagM7A6  5 Z9emM0M4tPLZCx01Cswm0A2pCkQAlfFhZi+PGvD+HJ1DfxKZ4j78TbthAPxr1XOd  6 Ro6Qhyjw7L/g7KlrygHZZOBxXbveheMueLnWJN4Zmw8wACGvSzuFgkvbIib6SYlf  7 aLbON8shsFalK4RPRFiykiTx+PncbT8Eu/2MMs5dHVPiq9GWDh5vwClWiWmk9Tyc  8 OLTPYwf6TJAZ0LyUMJIjasvrgpWs1h+jo9puy5bbgBCp/arPi24a/9M0kGibpc4o  9 hQIDAQAB 10 -----END PUBLIC KEY-----

3 使用ssh生成密鑰對

 1 [root@CentOS-8-LinuxIV script39]# ssh-keygen  2 Generating public/private rsa key pair.  3 Enter file in which to save the key (/root/.ssh/id_rsa):  4 Enter passphrase (empty for no passphrase):  5 Enter same passphrase again:  6 Your identification has been saved in /root/.ssh/id_rsa.  7 Your public key has been saved in /root/.ssh/id_rsa.pub.  8 The key fingerprint is:  9 SHA256:6oA+lLQqwc6e7leucpILW+YtZ3v6SOsF0zpt8AOQeOU root@CentOS-8-LinuxIV 10 The key's randomart image is:
11 +---[RSA 2048]----+
12 |    .            |
13 | . +             |
14 |. + E            |
15 | ... .           |
16 |.. o= . S        |
17 |..+. X .         |
18 |++= B B          |
19 |+%+=oX..         |
20 |**OBO=o          |
21 +----[SHA256]-----+
22 [root@CentOS-8-LinuxIV script39]# 
使用ssh-keygen生成的密鑰文件會在家目錄的.ssh文件夾。進入此文件夾
1 [root@CentOS-8-LinuxIV .ssh]# ls 2 id_rsa id_rsa.pub known_hosts 3 [root@CentOS-8-LinuxIV .ssh]# 

三.使用key登錄驗證

  1.首先在客戶端生成一對密鑰

  2.將生成公鑰拷貝到服務器端

  3.當客戶端發起請求時,服務器端會在相對應的家目錄中uthorized_keys中查找,如果有響應的IP和用戶,就會隨機生成一個字符串

  4.服務端將使用客戶端拷貝過來的公鑰進行加密,然后發送給客戶端

  5.得到服務端發來的消息后,客戶端會使用私鑰進行解密,然后將解密后的字符串發送給服務端

  6.服務端接受到客戶端發來的字符串后,跟之前的字符串進行對比,如果一致,就允許免密碼登錄

  操作時間key登錄

使用ssh-copy-id 192.168.55.6

 1 [root@CentOS-8-LinuxIV .ssh]# ssh-copy-id 192.168.55.6
 2 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
 3 The authenticity of host '192.168.55.6 (192.168.55.6)' can't be established.
 4 RSA key fingerprint is SHA256:4AeVnKTLZZgRG1j4uDeLUlG8TItb44VrcwUQW8SYsKU.
 5 Are you sure you want to continue connecting (yes/no)? yes
 6 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
 7 /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
 8 root@192.168.55.6's password: 
 9 
10 Number of key(s) added: 1
11 
12 Now try logging into the machine, with:   "ssh '192.168.55.6'"
13 and check to make sure that only the key(s) you wanted were added.
14 -------------------------------------------------------------------------
15 [root@CentOS-8-LinuxIV .ssh]# ssh 192.168.55.6
16 Last login: Sat Nov  9 11:55:02 2019 from 192.168.55.25

如上使用ssh-copy-id 到目標服務器,第一次需要密碼,並在目標家目錄中(默認root用戶,可以使用其他用戶user@192.168.55.6),創建。ssh/authorized_keys. 里面存放192.168.55.25的公鑰。所有第15行,再次連接時不需要密碼了。

 

四 總結

  基於key登錄的方法還有很多,比如使用key登錄還可以加密碼  使用key的密碼。每次使用KEY都需要密碼,但是每次需要密碼不又回到需要密碼的原點了嗎?其實是不一樣的。這個是key的密碼而不是其他主機的密碼。意義不同。

  key的密碼可以使用代理,就不用每次輸入key的密碼,但是退出終端代理就失效。

  使用key的代理

1 [root@CentOS-8-LinuxIV ~]# ssh-agent bash
2 [root@CentOS-8-LinuxIV ~]# ssh-a
3 ssh-add    ssh-agent  
4 [root@CentOS-8-LinuxIV ~]# ssh-add 
5 Enter passphrase for /root/.ssh/id_rsa: 
6 Identity added: /root/.ssh/id_rsa (root@CentOS-8-LinuxIV)
7 [root@CentOS-8-LinuxIV ~]# ssh 192.168.39.6
8 Last login: Sat Nov  9 18:26:58 2019 from 192.168.39.3
9 [root@CentOS-6 ~]$

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM