筆記 — SSH

Leo Lin
3 min readJun 8, 2018

--

Symmetrical Encryption

<加密端> <解密端> 使用同一支鑰匙 🔑,因此這支鑰匙不該在網路上被傳送,需要使用 Diffie Hellman Key Exchange Algorithm 來產生這支共用的鑰匙。

Diffie Hellman Key Exchange Algorithm

可以先看這兩部影片,然後再看下面的算法。

影片當中提到
Alice — (g ** a) mod n → Bob
Alice ← (g ** b) mod n — Bob
接著 Alice 就能得到 ((g ** a) ** b) mod n
同樣的 Bob 得到 ((g ** b) ** a) mod n
但是並沒有講解為什麼能夠由 (g ** b) mod n 得到 ((g ** b) ** a) mod n

以下來個小小的證明
rb = (g ** b) mod n
g ** b= n * qb + rb
(g ** b) ** a= (n * qb + rb) ** a
[(g ** b) ** a] mod n= [(n * qb + rb) ** a] mod n
g ** (a * b) mod n = (rb ** a) mod n
← Alice 得到的東西
同理
g ** (b * a) mod n = (ra ** b) mod n ← Bob 得到的東西
因此 Alice 和 Bob 得到了相同的鑰匙 🔑

想了一陣子才想出來,看來我的腦已經退化的差不多了 😢

Asymmetrical Encryption

產生鑰匙的時候,產生一組 <公鑰 🔑> + <私鑰 🗝>,將 <公鑰 🔑> 發給想傳訊息給自己的人,讓對方可以使用 <公鑰 🔑> 來將訊息加密,當自己收到加密後的訊息,可以使用 <私鑰 🗝> 來解密。

Hashing

當 Client 與 Host 之間的安全連線建立後,就可以開始傳送訊息,但是要怎麼確保在傳送訊息的途中,訊息內容沒有被惡意竄改呢?這時候就可以
1. hash (<公鑰 🔑><文檔 📄 >)*&$#&^&*!@%#→ Host
2. <文檔 📄 > → sshHost
如此一來,Host 只需要將透過 ssh 傳過來的 <文檔 📄 > 與自己的 <公鑰 🔑> 做 hash,再檢查和傳過來的 *&$#&^&*!@%# 是否相同,就知道 Client 傳來的訊息有沒有被修改過。

Create SSH Key

  1. $ cd ~/.ssh
  2. $ ssh-keygen -t rsa -b 4096 -C <your email>
  3. $ pbcopy < ~/.ssh/<your pub key 🔑 just created>
  4. paste this pub key 🔑 to the server you want to login
  5. $ ssh-add ~/.ssh/<your private key 🗝 just created>
  6. $ ssh <your username>@<host name>

上傳本機端的資料到遠端機器

$ rsync -av <files you want to upload> <your username>@<host name>:<path you want to put your uploaded files>

Conclusion

有了 🔑 🗝 就不用再自己打密碼啦 😃

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Leo Lin
Leo Lin

No responses yet

Write a response