快轉到主要內容

How to use GnuPG to generate PGP key?

·1393 字
Computer-Science Cryptography PGP
目錄
GnuPG - 本文屬於一個選集。
§ : 本文

怎麼使用 GnuPG 生成 PGP 金鑰?


What is GnuPG?
#

GNU Privacy Guard 簡稱 GnuPGGPG,主要是用於數位加密簽章的軟體。(Asymmetric cryptography 金鑰的管理)

更詳細介紹請見《Wiki 維基百科 - GnuPG


Install
#

Ubuntu

1apt-get install gnupg2
2# OR
3apt-get install gnupg

Mac

1brew install gnupg

Generating a new keypair
#

  • 如果你想簡單快速生成金鑰,可以使用指令:
1gpg --gen-key
  • 手動選擇更完整的選項可以使用指令:
1gpg --full-gen-key

如果是 --full-gen-key 則會顯示:

1Please select what kind of key you want:
2   (1) RSA and RSA (default)
3   (2) DSA and Elgamal
4   (3) DSA (sign only)
5   (4) RSA (sign only)
6  (14) Existing key from card
7Your selection?

輸入數字選擇自己要的加密演算法類型,或者直接按下 Enter 使用預設。

接著是輸入金鑰的長度金鑰時效設定:

1RSA keys may be between 1024 and 4096 bits long.
2What keysize do you want? (3072) # 輸入金鑰位元長度,1024 ~ 4096 之間
3Requested keysize is 3072 bits
1Please specify how long the key should be valid.
2         0 = key does not expire
3      <n>  = key expires in n days
4      <n>w = key expires in n weeks
5      <n>m = key expires in n months
6      <n>y = key expires in n years
7Key is valid for? (0) # 選擇金鑰過期設定
8Key does not expire at all
9Is this correct? (y/N) y # 輸入「y」確認

接著就是輸入主要的資訊了 (使用 --gen-key 參數會直接跳到這)

1# Terminal
2Real name: Alpaca0x0 # 你的名稱
3Email address: alpaca0x0@example.com # 你的 Email
4Comment: My First PGP Key. # 註釋 (使用 full-gen-key 參數才會顯示)

最後確認資訊
若有誤可以輸入「N」修改名稱,輸入「E」修改 Email

輸入「O」進行確認

1You selected this USER-ID:
2    "Alpaca0x0 <alpaca0x0@example.com>"
3
4Change (N)ame, (E)mail, or (O)kay/(Q)uit? O

之後會要求你輸入密碼,可以設定也可以留空。

完成後會顯示該金鑰的資訊:

1pub   rsa3072 2021-07-16 [SC] [expires: 2023-07-16]
2      36EECBC209A936F5AF9B25A469219C5CB53AF7C3
3uid                      Alpaca0x0 <alpaca0x0@example.com>
4sub   rsa3072 2021-07-16 [E] [expires: 2023-07-16]
  • rsa3072」指的是「RSA」算法,金鑰長度「3072」位元
  • 36EECBC209A936F5AF9B25A469219C5CB53AF7C3」就是 Key ID

Displaying keys
#

Displaying public keys
#

查看已有的公鑰,使用指令:

1gpg --list-public-keys
2# OR
3gpg --list-keys
4# OR
5gpg -k

顯示已有的公鑰

1pub   rsa3072 2021-07-16 [SC] [expires: 2023-07-16]
2      36EECBC209A936F5AF9B25A469219C5CB53AF7C3
3uid           [ultimate] Alpaca0x0 <alpaca0x0@example.com>
4sub   rsa3072 2021-07-16 [E] [expires: 2023-07-16]

也可以在後方加上關鍵字 (名稱Email):

1# gpg --list-keys <Keyword>
2gpg --list-keys alpaca0x0
3# OR
4gpg --list-keys alpaca
5# OR
6gpg --list-keys example.com

Displaying secret keys
#

查看已有的私鑰,使用指令:

1gpg --list-secret-keys 

--list-public-keys 用法類似,一樣可以加入關鍵字查詢:

1# gpg --list-secret-keys <Keyword>
2gpg --list-secret-keys alpaca0x0

Exporting key
#

Exporting public key
#

若要導出公鑰,則使用指令:

1gpg --export <Key-ID>

但這樣導出的密鑰是 binary format,所以使用參數「--armor, -a」來使其輸出為 ASCII-Code format
使用「--output, -o」或者「>」將輸出結果寫入檔案:

1# gpg --export --armor <Key-ID> > <Output-File-Name>
2# gpg --export --armor --output <Output-File-Name> <Key-ID>
3gpg --export --armor --output public_key.pub 36EECBC209A936F5AF9B25A469219C5CB53AF7C3
4# OR
5gpg --export --armor 36EECBC209A936F5AF9B25A469219C5CB53AF7C3 > public_key.pub

Exporting secret key
#

導出私鑰使用指令:

gpg --export-secret-keys <Key-ID>

--export 用法相同
p.s. 若是當初生成鑰匙時有輸入密碼,則導出時也需要輸入驗證。

1# e.g.
2gpg --export-secret-keys --armor 36EECBC209A936F5AF9B25A469219C5CB53AF7C3 > secret_key.asc

Sending the key to the key server
#

公鑰可以上傳至公開的 Keyserver 上讓他人能方便下載你的公鑰。
上傳的指令是:

1gpg --send-keys 0x<Key-ID>
2# 可以使用「--keyserver」參數指定上傳的伺服器
3gpg --send-keys --keyserver <Server> 0x<Key-ID>

這邊要特別注意的是,大多數時候,在 <Key-ID> 的前方要加上「0x」(Hexadecimal)。

1# e.g.
2gpg --send-keys --keyserver pgp.mit.edu 0x36EECBC209A936F5AF9B25A469219C5CB53AF7C3

Deleting key
#

刪除本地的公鑰,使用指令:

1# gpg --delete-key <Key-ID>
2gpg --delete-keys 36EECBC209A936F5AF9B25A469219C5CB53AF7C3

但當我們執行時,會發現終端返回這則訊息:

1gpg: there is a secret key for public key "36EECBC209A936F5AF9B25A469219C5CB53AF7C3"!
2gpg: use option "--delete-secret-keys" to delete it first.

這是因為你還留有該公鑰私鑰,所以先刪除私鑰,再刪除公鑰

1# Delete secret keys
2gpg --delete-secret-keys <Key-ID>
3# Delete public keys
4gpg --delete-keys <Key-ID>
1# e.g.
2gpg --delete-secret-keys 36EECBC209A936F5AF9B25A469219C5CB53AF7C3
3gpg --delete-keys 36EECBC209A936F5AF9B25A469219C5CB53AF7C3

或者使用「--delete-secret-and-public-keys」一次刪除公私鑰

1# gpg --delete-secret-and-public-keys <Key-ID>
2gpg --delete-secret-and-public-keys 36EECBC209A936F5AF9B25A469219C5CB53AF7C3

Importing key
#

要匯入金鑰主要有兩種方式

從檔案匯入(本地端匯入):

1# gpg --import <Key-File>
2gpg --import Private_OR_Public.key

從 Key-Server 上匯入(遠端匯入):

1# gpg --keyserver <Server> --recv-key 0x<Key-ID>
2gpg --keyserver keyserver.ubuntu.com --recv-key 0xA91A9E8A0EE6BAB3742464861DF369D42499F3F1

如果要查詢該 Key-Server 上的鑰匙資訊,可以使用指令:

1# gpg --keyserver <Server> --search-key <Key-ID>
2gpg --keyserver keyserver.ubuntu.com --search-key 0xA91A9E8A0EE6BAB3742464861DF369D42499F3F1

References
#

《GnuPG documentation》https://gnupg.org/documentation/manpage.html

Alpaca
作者
Alpaca
No one can stop my feet.
GnuPG - 本文屬於一個選集。
§ : 本文

相關文章

Encode、Encrypt and Hash are different!
·5521 字
Computer-Science Cryptography Digital Signature Cyber Security

Encode (編碼)、Encrypt (加密)、Hash (雜湊) 一直令許多新手們搞混,雖然說可能運算後的結果看起來都是一串亂碼,但原理及使用場合卻天差地遠,千萬不要再分不清處了!

非對稱加密 (Asymmetric Cryptography)
·1223 字
Computer-Science Cryptography Digital Signature

這次提到的概念是比上次的「 Symmetric Cryptography 」來的更進階的「 Asymmetric Cryptography (非對稱加密) 」,其概念被廣泛運用在數位簽章

對稱加密 (Symmetric Cryptography)
·1399 字
Computer-Science Cryptography

大家對於「密碼」並不陌生,生活中總是遇到許多場合需要用到密碼,然而「加密」的原理似乎相對的卻不是那麼的備受關注。

《SQL Basics-03》UPDATE、ALTER、DROP
·907 字
Computer-Science SQL Database

一些基礎常見的 SQL 語法。

《SQL Basics-02》SELECT、LIKE
·962 字
Computer-Science SQL Database

一些基礎常見的 SQL 語法。

《SQL Basics-01》CREATE、INSERT
·1421 字
Computer-Science SQL Database

一些基礎常見的 SQL 語法。