1. 生成秘鑰
gpg --full-generate-key
等價於 gpg --full-gen-key
Real name: yellowconvict
為該秘鑰的名字,也稱為 USER-ID
在生成秘鑰前最后一步還需要為秘鑰設置密碼
2. 查看秘鑰
2.1 查看公鑰
gpg --list-key
2.2 查看私鑰
gpg --list-secret-key
3. 導出秘鑰
--armor
: 將其轉換為ASCII碼顯示
3.1 導出公鑰
gpg --armor --output 文件名 --export 用戶ID
如:
gpg --armor --output yellowconvict.pub --export yellowconvict
3.2 導出私鑰
gpg --armor --output 輸出文件 --export-secret-keys 用戶ID
如:
gpg --armor --output yellowconvict.pri --export-secret-keys yellowconvict
導出私鑰時需要輸入一開始秘鑰的密碼
4. 刪除秘鑰
必須先刪除私鑰,才能刪除公鑰
4.1 刪除私鑰
gpg --delete-secret-keys yellowconvict
4.2 刪除公鑰
gpg --delete-keys yellowconvict
5. 導入秘鑰
導入公鑰、私鑰均使用gpg --import 公私鑰文件名
5.1 導入私鑰
gpg --import yellowconvict.pri
導入私鑰時同樣需要輸入一開始秘鑰的密碼
5.2 導入公鑰
gpg --import yellowconvict.pub
6. 加解密文件
6.1 使用公鑰加密文件
gpg --recipient 用戶ID --output 輸出文件 --encrypt 原始文件
如:
gpg --recipient yellowconvict --output test.txt.en --encrypt test.txt
6.2 使用私鑰解密文件
gpg --output 輸出文件 --decrypt 已加密文件
如:
gpg --output test.txt.de --decrypt test.txt.en
第一次使用私鑰解密需要輸入秘鑰的密碼,之后就不需要輸入了
查看解密后的文件,發現已經還原內容了
7. 其他
第一次生成秘鑰時,可能出現卡住的問題,類似如下提示:
Not enough random bytes available. Please do some other work to give the OS a chance to collect more
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
解決方法:
不要退出當前終端,新建一個終端,並根據系統執行以下命令,命令執行完后,卡住的界面瞬間完成
Ubuntu:
$ sudo apt install rng-tools
$ rngd -r /dev/urandom
CentOS:
$ yum install rng-tools
$ rngd -r /dev/urandom