密碼學相關

一些有關密碼學的筆記

R4 Cheng
6 min readNov 13, 2020

~~~~~~~不定時更新

Content:

+ De-identification(去識別化)

+ Hash function

+ Message authentication code (訊息鑑別碼)

+ Digital signature (數位簽章)

+ Digital certificate (數位憑證)

+ HTTP Authentication

+ Two Factor Authentication (2FA, 雙重認證)

De-identification(去識別化)

  • Pseudonymisation(擬匿名化) => 真名換成暫時ID
  • K-anonymization(K-匿名化) => 在查詢下,能鎖定的目標至少有有K個個體

Hash function

  • Hash function 是把輸入的數據轉換成「固定長度」且「不規則」的函數
  • 應用:密碼以雜湊碼保存在伺服器、產生 MAC

MAC (message authentication code): 包括 HMAC (常用), OMAC, CMAC

特徵

  • 長度不變
  • 相同的輸入必得相同的輸出
  • 類似的輸入不會產生類似的輸出
  • 可能發生 (低機率) 雜湊碰撞 (hash collision)
  • 輸出無法反推導輸入

代表演算法

MD5 (deprecated), SHA-2

Message authentication code (訊息鑑別碼)

  • 通稱 MAC
  • 目的:身份鑑別和檢查訊息完整性
  • 應用:E.g. 向電商購買商品,傳送商品編號
  • 包括 HMAC (常用), OMAC, CMAC

使用步驟

  1. 客戶先以安全的方式產生兩組密鑰(假設為A和B)給電商,「密鑰A」用於加密訊息 (E.g. 商品編號),「密鑰B」用於產生訊息識別碼。
  2. 客戶將被「密鑰A」加密後的密文再和「密鑰B」 hash 產生 MAC
  3. 將密文(含例如:商品編號)和 MAC 一同交給電商
  4. 電商將收到的密文和之前收到的「密鑰B」也 hash 產生出 MAC
  5. MAC 相同即代表內容未被串改

問題

因為雙方都能產生 MAC 因此會有抵賴問題 => 用數位簽章解決

HMAC (Hash-based Message Authentication Code)

E.g JWT

Digital signature (數位簽章)

  • 目的:產生不可抵賴性
  • 原理:只有私鑰擁有者能產生數位簽章

流程

也使用公鑰和私鑰,但是使用方式和 public key crypto-system 稍微不同

  1. 由訊息傳送者A準備:公鑰P、私鑰S、訊息M (這裡和 public key crypto-system 不同)
  2. A 將「公鑰P」交給 B
  3. A 將「訊息M」用「私鑰S」加密=數位簽章
  4. A 將「訊息M」和「數位簽章」交給 B
  5. B用「公鑰P」解密,比較與收到的訊息是否一致

代表演算法

RSA

RSA 的公鑰、私鑰能對調使用

Digital certificate (數位憑證)

<空>

HTTP Authentication

Traditional approach: cookie-based server-side sessions

Steps:

  1. User visit a website
  2. User submits account/password to a server (first time)
  3. Server creates a session in the DB
  4. Server responses session_id to client by cookie
  5. session_id will be saved in the browser’s cookie jar
  6. User secondly visit the website (這時 request 會夾帶 cookie 傳至 server)
  7. Server 比對 session

Token-based auth

Steps:

  1. User visit a website
  2. User submits account/password to a server (first time)
  3. Server creates a JWT and Server saves only the private key
  4. Server responses JWT to client and JWT is stored in local storage
  5. User secondly visit the website (這時 JWT 會被加到 authorization header <prefix Bearer>)

什麼是 JWT?

此篇:https://chengr4.medium.com/%E4%BB%80%E9%BA%BC%E6%98%AFjwt-e0130cc7ad4f

References

  1. 開發者必備知識 — HTTP認證(HTTP Authentication)
  2. Fireship; Session vs Token Authentication in 100 Seconds (2020.10)

Two Factor Authentication (2FA, 雙重認證)

  • An extra layer of security used when logging into websites or apps.
  • Log in = username and password + provide another form of authentication that only you know or have access to.

Another form of authentication => E.g. TOTP, HOTP, 短信, 電話

TOTP (Time-based One Time Password)

  • E.g. 使用 Google Authenticator
https://images.app.goo.gl/azibmHBGpQppUKxT8
One Time Password = TOTP(K, T) = Truncate(HMAC-SHA-1(K, T))

HMAC-SHA-1: 一種 hash 函數

K (pre-shared key): Client and Server 事先定好的 secret key

T (Time): 現在的時間段,Google Authenticator 每30秒更新一次時間

Truncate: 把太長的值減短

驗證過程

  1. Server 傳送 pre-shared key 至 Client (總之雙方要先有一組相同的 pre-shared key,此過程只有第一次。)
  2. Client 對 pre-shared key 和「時間段」使用 HMAC 產生 One Time Password (in Google Authenticator),並將之傳給 Server,例如傳送 56743,同時 Server 也以相同的方式產生 One Time Password (56743)
  3. 與 Client 的 One Time Password (56743) 在 Server 內進行比較,相同即通過驗證 (response 通過訊息給 client)。
  4. 之後每次驗證重複 2, 3 step

Reference

[1] Citrix (2019). Tech Insight — TOTP

[2] Tao (2011)Google账户两步验证的工作原理

[3] Traversy Media (2020). Two Factor Authentication | Node.js & Speakeasy

Other reference

[1] Jeremy Kithome (2020). Implementing two-factor authentication using Speakeasy

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

--

--

R4 Cheng
R4 Cheng

Written by R4 Cheng

「0」が過去で「1」が未来「今」は何処にもない

No responses yet

Write a response