GPG 基本文件加密不需要公钥/私钥。但为了使加密文件更安全,您可以使用 RSA/DSA 算法。这些算法生成公钥和私钥来加密文件。
本文将帮助您在 Linux 中使用 GPG 密钥对完成以下三项文件加密任务。
- 创建密钥对
- 为同一用户帐户本地加密/解密文件。
- 为其他用户加密文件。
- 解密其他用户的文件。
第1步:创建GPG密钥对
第一步是生成密钥对。使用以下命令生成 gpg 密钥对。
# gpg --gen-key
Output:
gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.
Please select what kind of key you want:
(1) DSA and Elgamal (default)
(2) DSA (sign only)
(5) RSA (sign only)
Your selection?
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
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
You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) "
Real name: Rahul Kumar
Email address: rahul.kumar1099@gmail.com
Comment: Linux System Admin
You selected this USER-ID:
"Rahul Kumar (Linux System Admin) "
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.
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.
++++++++++++++++++++..+++++++++++++++.++++++++++++++++++++.+++++.+++++.+++++++++++++++++++++++++.+++++.++++++++++++++++++++++++++++++...+++++>+++++..+++++>+++++..............................................................+++++
Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 284 more bytes)
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: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 2AE39E50 marked as ultimately trusted
public and secret key created and signed.
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
pub 1024D/2AE39E50 2013-03-14
Key fingerprint = 0D89 4697 E22A A6CC 3017 5EA1 0389 ED6D 2AE3 9E50
uid Rahul Kumar (Linux System Admin)
sub 2048g/9102AC9C 2013-03-14
@gmail.com>@gmail.com>@duesseldorf.de>
步骤2:加密/解密文件当前用户
仅为单个用户加密文件。其他人无法解密该文件。
# gpg --encrypt --recipient 'Rahul Kumar' tecadmin.txt
–recipient name should be same as used in key generation. Above command will automatically generate a encrypted file named tecadmin.txt.gpg
解密您自己的文件 tecadmin.txt.gpg
# gpg --output tecadmin2.txt --decrypt tecadmin.txt.gpg
–output or -o用于指定输出文件名。上述命令将提示输入密钥对中使用的密码。
步骤 3:为其他用户加密文件
使用#1 步骤生成加密文件。最后与其他用户共享您的公钥和加密文件 ( tecadmin.txt.gpg )。要导出您的公钥,请使用以下命令。
# gpg --armor --output pubkey.txt --export 'Rahul Kumar'
检查您的公钥。应该像下面这样
# cat pubkey.txt
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.5 (GNU/Linux)
mQGiBFFBPwsRBADWAxKxCxLsZ1ZJ512auBkEw51HlF5+k18Yp1giOqIYtbRUPVeq
Y5o5knVKjlJDlVA0/rGh18fbKgubjZl1PL1R+tT0bMIDIs5+hg+S60nSlxBGOhYz
8h+nuY4GbOMdG0V4DJwgpOg7Haywljs0epYPtcdroRIrLg8owjcpYtIm1wCgl75F
XP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mqOMwz1J1GUKTOWH5vVCsfYdMmb/
4GnxV2iAKeskPRwZFHujaHA0tvlGtRXjFXqxiBuSZjgv0W93sbZQamArCYaJtwT0
2ZgrrUAVEs8JMTbSfrQEmXpfhk2mFcrf2ocAC6LwxMYZESDW6YhlY+1utEqpN6al
Mefh1g42JK2g4OV42iP7op0JeQp6emJFywumlTrnihycRXEElxur+23NSLQpeNaw
M6gUQPeISQQYEQIACQUCUUE/EAIbDAAKCRADie1tKuOeUB+VAJ9e8PprKcX3qvnW
rU8MhhXMB/G0PQCfehvC8uWR/TLtHyu5pjzCEtcsX/E=
=k3yQ
-----END PGP PUBLIC KEY BLOCK-----
第四步:解密其他用户文件。
要解密其他用户的文件,需要该用户的公钥。使用以下命令将该公钥导入您的帐户。例如其他用户公共文件是 otherpub.txt
# gpg --import otherpub.txt
确保使用以下命令成功导入文件。
# gpg --list-keys
上面的命令将显示您帐户中的所有公钥。确保其他用户的公钥也存在于此。
现在您可以使用以下命令解密其他用户文件
# gpg --output otheruserfile.txt otheruserfile.txt.gpg
Thank You for reading this article. Read our next article to File Encryption using GPG Command Line.