## 一、生成 GPG 密钥 Github 在提交命令时并不会去验证邮箱的正确性, 也就是可以邮箱是可以伪造的 ,为了避免这种情况的出现,可以使用 Verified( 认证)。 1. 打开 Git Bash 2. 生成一个密钥(可在 Linux 控制台,Windows 下有 Cygwin64 等) 3. 使用命令开始创建GPG 密钥 * 使用命令:`gpg --full-generate-key` * 提示:Please select what kind of key you want `默认即可,回车` * 提示:Please select which elliptic curve you want `默认即可,回车` * 提示:Please specify how long the key should be valid. `默认即可,回车` * 提示:Is this correct? (y/N) `输入 "y" 然后回车` * 提示:Real name `输入提交时显示的 用户名 看个人喜好` * 提示:Email address `输入在Github使用的主邮箱` * 提示:Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? `输入 "o" 然后回车` ```bash $ gpg --full-generate-key gpg (GnuPG) 2.4.5-unknown; Copyright (C) 2024 g10 Code GmbH This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Please select what kind of key you want: (1) RSA and RSA (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) (9) ECC (sign and encrypt) *default* (10) ECC (sign only) (14) Existing key from card Your selection? Please select which elliptic curve you want: (1) Curve 25519 *default* (4) NIST P-384 (6) Brainpool P-256 Your selection? Please specify how long the key should be valid. 0 = key does not expire = key expires in n days w = key expires in n weeks m = key expires in n months y = key expires in n years Key is valid for? (0) Key does not expire at all Is this correct? (y/N) y GnuPG needs to construct a user ID to identify your key. Real name: test_name Email address: test@test.cn Comment: You selected this USER-ID: "test_name " Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o 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. 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. gpg: revocation certificate stored as '/c/Users/nuoxian/.gnupg/openpgp-revocs.d/EBA06C49B8EBB78E7FF912DAB8FD1BC2A31580CF.rev' public and secret key created and signed. pub ed25519 2025-02-18 [SC] EBA06C49B8EBB78E7FF912DAB8FD1BC2A31580CF uid test_name sub cv25519 2025-02-18 [E ``` #### 列出所有 GPG 密钥 ID 列出所有 GPG 密钥 ID:`gpg --list-secret-keys --keyid-format LONG` ```bash $ gpg --list-secret-keys --keyid-format LONG gpg: checking the trustdb gpg: marginals needed: 3 completes needed: 1 trust model: pgp gpg: depth: 0 valid: 3 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 3u [keyboxd] --------- sec ed25519/B8FD1BC2A31580CF 2025-02-18 [SC] # 这里B8FD1BC2A31580CF是KeyID EBA06C49B8EBB78E7FF912DAB8FD1BC2A31580CF uid [ultimate] test_name ssb cv25519/2E5816E2C83C3FBA 2025-02-18 [E] ``` #### 导出公钥 ```bash # 方法1控制台输出密钥 gpg --armor --export B8FD1BC2A31580CF # 方法2导出密钥 gpg -a -o key.txt --export B8FD1BC2A31580CF ``` #### 删除私钥 列出所有 GPG 私钥:`gpg --list-secret-keys` ``` $ gpg --list-secret-keys gpg: checking the trustdb gpg: marginals needed: 3 completes needed: 1 trust model: pgp gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u [keyboxd] --------- sec ed25519 2025-02-11 [SC] XXXXXXXXXXXXXXXXXXXXXX uid [ultimate] nuoxi4n ssb cv25519 2025-02-11 [E] ``` 执行删除私钥,然后输入 "y" 确认输入密码即可删除 `gpg --delete-secret-keys XXXXXXXXXXXXXXXXXXXXXX(私钥ID)` #### 删除公钥 列出所有 GPG 公钥:`gpg --list-keys` ```bash $ gpg --list-keys [keyboxd] --------- pub ed25519 2025-02-11 [SC] XXXXXXXXXXXXXXXXXXXXXX uid [ultimate] nuoxi4n sub cv25519 2025-02-11 [E] ``` 执行删除公钥,然后输入 "y" 确认即可删除 `gpg --delete-keys XXXXXXXXXXXXXXXXXXXXXX(公钥ID)` [scode type="yellow" size=""]需先删除私钥才能删除公钥![/scode] ## 将 GPG 密钥导入 Github Github ——> Setting ——> SSH and GPG keys ——> New GPG key 点击一键访问 [Add new GPG key](https://github.com/settings/gpg/new) 将导出的公钥导入到Github中 ```bash $ gpg --armor --export B8FD1BC2A31580CF -----BEGIN PGP PUBLIC KEY BLOCK----- mDMEZ7Q3fhYJKwYBBAHaRw8BAQdAkWyoRCmsZ8D7GrfTm3O6/ch9YKNhvAE9QIJY [省略这一部分] gM8EcAD/e8posD63i4kUGYY1EVWtNw4FbkSoOfCzWpTyNZWfR0oA+wWi2BVTJJCK =aj2h -----END PGP PUBLIC KEY BLOCK----- ``` Loading... ## 一、生成 GPG 密钥 Github 在提交命令时并不会去验证邮箱的正确性, 也就是可以邮箱是可以伪造的 ,为了避免这种情况的出现,可以使用 Verified( 认证)。 1. 打开 Git Bash 2. 生成一个密钥(可在 Linux 控制台,Windows 下有 Cygwin64 等) 3. 使用命令开始创建GPG 密钥 * 使用命令:`gpg --full-generate-key` * 提示:Please select what kind of key you want `默认即可,回车` * 提示:Please select which elliptic curve you want `默认即可,回车` * 提示:Please specify how long the key should be valid. `默认即可,回车` * 提示:Is this correct? (y/N) `输入 "y" 然后回车` * 提示:Real name `输入提交时显示的 用户名 看个人喜好` * 提示:Email address `输入在Github使用的主邮箱` * 提示:Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? `输入 "o" 然后回车` ```bash $ gpg --full-generate-key gpg (GnuPG) 2.4.5-unknown; Copyright (C) 2024 g10 Code GmbH This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Please select what kind of key you want: (1) RSA and RSA (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) (9) ECC (sign and encrypt) *default* (10) ECC (sign only) (14) Existing key from card Your selection? Please select which elliptic curve you want: (1) Curve 25519 *default* (4) NIST P-384 (6) Brainpool P-256 Your selection? Please specify how long the key should be valid. 0 = key does not expire <n> = key expires in n days <n>w = key expires in n weeks <n>m = key expires in n months <n>y = key expires in n years Key is valid for? (0) Key does not expire at all Is this correct? (y/N) y GnuPG needs to construct a user ID to identify your key. Real name: test_name Email address: test@test.cn Comment: You selected this USER-ID: "test_name <test@test.cn>" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o 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. 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. gpg: revocation certificate stored as '/c/Users/nuoxian/.gnupg/openpgp-revocs.d/EBA06C49B8EBB78E7FF912DAB8FD1BC2A31580CF.rev' public and secret key created and signed. pub ed25519 2025-02-18 [SC] EBA06C49B8EBB78E7FF912DAB8FD1BC2A31580CF uid test_name <test@test.cn> sub cv25519 2025-02-18 [E ``` #### 列出所有 GPG 密钥 ID 列出所有 GPG 密钥 ID:`gpg --list-secret-keys --keyid-format LONG` ```bash $ gpg --list-secret-keys --keyid-format LONG gpg: checking the trustdb gpg: marginals needed: 3 completes needed: 1 trust model: pgp gpg: depth: 0 valid: 3 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 3u [keyboxd] --------- sec ed25519/B8FD1BC2A31580CF 2025-02-18 [SC] # 这里B8FD1BC2A31580CF是KeyID EBA06C49B8EBB78E7FF912DAB8FD1BC2A31580CF uid [ultimate] test_name <test@test.cn> ssb cv25519/2E5816E2C83C3FBA 2025-02-18 [E] ``` #### 导出公钥 ```bash # 方法1控制台输出密钥 gpg --armor --export B8FD1BC2A31580CF # 方法2导出密钥 gpg -a -o key.txt --export B8FD1BC2A31580CF ``` #### 删除私钥 列出所有 GPG 私钥:`gpg --list-secret-keys` ``` $ gpg --list-secret-keys gpg: checking the trustdb gpg: marginals needed: 3 completes needed: 1 trust model: pgp gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u [keyboxd] --------- sec ed25519 2025-02-11 [SC] XXXXXXXXXXXXXXXXXXXXXX uid [ultimate] nuoxi4n <nuo_xian@qq.com> ssb cv25519 2025-02-11 [E] ``` 执行删除私钥,然后输入 "y" 确认输入密码即可删除 `gpg --delete-secret-keys XXXXXXXXXXXXXXXXXXXXXX(私钥ID)` #### 删除公钥 列出所有 GPG 公钥:`gpg --list-keys` ```bash $ gpg --list-keys [keyboxd] --------- pub ed25519 2025-02-11 [SC] XXXXXXXXXXXXXXXXXXXXXX uid [ultimate] nuoxi4n <nuo_xian@qq.com> sub cv25519 2025-02-11 [E] ``` 执行删除公钥,然后输入 "y" 确认即可删除 `gpg --delete-keys XXXXXXXXXXXXXXXXXXXXXX(公钥ID)` <div class="tip inlineBlock warning"> 需先删除私钥才能删除公钥! </div> ## 将 GPG 密钥导入 Github Github ——> Setting ——> SSH and GPG keys ——> New GPG key 点击一键访问 [Add new GPG key](https://github.com/settings/gpg/new) 将导出的公钥导入到Github中 ```bash $ gpg --armor --export B8FD1BC2A31580CF -----BEGIN PGP PUBLIC KEY BLOCK----- mDMEZ7Q3fhYJKwYBBAHaRw8BAQdAkWyoRCmsZ8D7GrfTm3O6/ch9YKNhvAE9QIJY [省略这一部分] gM8EcAD/e8posD63i4kUGYY1EVWtNw4FbkSoOfCzWpTyNZWfR0oA+wWi2BVTJJCK =aj2h -----END PGP PUBLIC KEY BLOCK----- ``` 最后修改:2025 年 03 月 04 日 © 来自互联网 打赏 赞赏作者 支付宝微信 赞 别忘了点赞或赞赏,让我知道创作的路上有你陪伴。