linux系統環境下,對文件進行加密

引用自:http://blog.chinaunix.net/u/19895/showart_232861.html

Linux關於文件加密的兩種方法和詳解:
一、用GnuPG加密文件。
GnuPG軟件包(Gnu Privacy Guard,Gnu隱私保鏢),軟件包的名稱是gpg。
gpg在加密文件時使用的是公共密鑰加密方法。
1.第一步是要創建一個將來用來發送加密數據和進行解密數據的密鑰。我們執行一下gpg命令,就會在你的主目錄下創建一個.gnupg子目錄。
(如果它不存在的話,有時已經存在了)。在該子目錄裏面有一個gpg.conf的配置文件,它裏面是gpg工具的各種配置選項及其默認設置值。
接下來,我們來進行第一項,生成密鑰:
# gpg --gen-key >>這個命令生成密鑰
gpg (GnuPG) 1.2.4; Copyright (C) 2003 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)
(4) RSA (sign only)
Your selection? 1
DSA keypair will have 1024 bits.
About to generate a new ELG-E keypair. >>選擇密鑰長度
minimum keysize is 768 bits
default keysize is 1024 bits
highest suggested keysize is 2048 bits
What keysize do you want? (1024) 768
Requested keysize is 768 bits
Please specify how long the key should be valid. >>選擇密鑰有效期,0代表沒有期限
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) 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 Real Name, Comment and Email Address in this form:
"Heinrich Heine (Der Dichter) <[email protected]>"
Real name: test201 >>輸入基本信息,真實名字
Email address: [email protected] >>輸入郵件地址
Comment: this is 201 key >>其他相關注釋信息
You selected this USER-ID:
"test201 (this is 201 key) <[email protected]>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O >>確認OK
You need a Passphrase to protect your secret key.
Enter passphrase: >>輸入密鑰口令
Repeat passphrase:
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 215 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. >>這段話表明提示我們在系統上創建一些隨機的活動,如果沒有足夠的活動,他會停下來提示我們繼續這樣做。(比如查看一下cpu,隨意敲打一下鍵盤都可以)
++++++++++.+++++++++++++++.++++++++++++++++++++.++++++++++.+++++++++++++++++++++++++.+++++.+++++.+++++++++++++++.+++++.++++++++++++++++++++....>+++++..+++++^^^^^
gpg: /root/.gnupg/trustdb.gpg: trustdb created
public and secret key created and signed.
key marked as ultimately trusted.
pub 1024D/BA56DDDA 2007-01-16 test201 (this is 201 key) <[email protected]> 這行裏面的BA56DDDA是生成的公共密鑰的標識,我們在後面還要使用,記住它吧。
Key fingerprint = 98E8 0A56 9E16 F61B 379D 2F53 D5DF 4117 BA56 DDDA
sub 768g/8F754496 2007-01-16
>>成功
#
現在我們已經生成了一對密鑰。查看.gnupg目錄:
# cd .gnupg/
# ll
total 24
-rw------- 1 root root 8075 Jan 16 11:10 gpg.conf
-rw------- 1 root root 856 Jan 16 11:30 pubring.gpg 存放別人公共密鑰的“鑰匙環”文件。
-rw------- 1 root root 0 Jan 16 11:10 pubring.gpg~
-rw------- 1 root root 600 Jan 16 11:30 random_seed
-rw------- 1 root root 991 Jan 16 11:30 secring.gpg
-rw------- 1 root root 1240 Jan 16 11:30 trustdb.gpg
#
又新生成了幾個相關的文件。
2.爲了把剛纔生成的公共密鑰發送給對方,我們需要先用命令把它提取出來:
# gpg --armor --export BA56DDDA > 201.key 把公共密鑰提取到文件201.key中。
其中:
--armor是讓gpg生成ASCII格式的輸出,這樣適合電子郵件來發送。如果可以使用ssh等支持二進制文件傳輸的工具。可以不使用這個選項。
--export 就不用多說了,就是導出的意思。

3.在收到別人傳過來的公共密鑰後,需要把這個公共密鑰放到“鑰匙環”文件裏:
比如,我們在另一臺計算機上收到了剛纔201.key這個公共密鑰,然後我們執行:
# gpg --import 201.key
gpg: key BA56DDDA: public key "test201 (this is 201 key) <[email protected]>" imported
gpg: Total number processed: 1
gpg: imported: 1
通過這條命令,可以把剛纔在201機器上生成的公共密鑰導入到161機器的“鑰匙環”文件(~/.gnupg/pubring.gpg)中。
可以通過gpg -kv命令查看161機器上當前存放多少個別人的公共密鑰:
# gpg -kv
/root/.gnupg/pubring.gpg
------------------------
pub 1024D/1C05EC6B 2007-01-15
uid Paolo (this test destination 213) <[email protected]>
sub 1024g/A16A8685 2007-01-15
pub 1024D/BC3AA97D 2007-01-15
uid Wangqi (test to 161) <[email protected]>
sub 1024g/33A9764D 2007-01-15
pub 1024D/BA56DDDA 2007-01-16
uid test201 (this is 201 key) <[email protected]>
sub 768g/8F754496 2007-01-16
#
4.接下來,我們在161機器上用201的公共密鑰加密一個文件
# gpg -ea -r BA56DDDA install.log >>通過這個命令對install.log文件進行加密。
gpg: 8F754496: There is no assurance this key belongs to the named user
pub 768g/8F754496 2007-01-16 test201 (this is 201 key) <[email protected]>
Primary key fingerprint: 98E8 0A56 9E16 F61B 379D 2F53 D5DF 4117 BA56 DDDA
Subkey fingerprint: DC76 48E6 70C0 CD36 F671 D2D3 AEC5 02A2 8F75 4496
It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes.
Use this key anyway? (y/N) y
# ls
-e 代表加密
-a 代表ASCII格式,如果不是通過電子郵件傳輸,可不加此參數
-r 後面是密鑰的標識。可以使用多個-r參數,加多個標識,這樣就可以把它發給多個需要該文件的人。
這個命令執行之後,在當前目錄下查看,生成了一個同名的install.log.asc的文件,這個文件就是加密後的文件。

5.最後我們把install.log.asc文件傳回到201機器上,進行解密查看:
# ls
201.key install.log.asc
# gpg -o install.log -d install.log.asc >>這個命令進行解密,-o爲輸出到一個文件中,-d表示解密。
You need a passphrase to unlock the secret key for
user: "test201 (this is 201 key) <[email protected]>"
768-bit ELG-E key, ID 8F754496, created 2007-01-16 (main key ID BA56DDDA)
gpg: encrypted with 768-bit ELG-E key, ID 8F754496, created 2007-01-16
"test201 (this is 201 key) <[email protected]>"
# ls
201.key install.log install.log.asc
#
我們可以看到在當前目錄下生成了一個install.log文件,這個文件就是解密後的文件,可以直接查看。


二、用openssl加密文件
openssl也可以進行文件的加密。方法比上面的gpg簡單很多,沒有創建密鑰的過程,也沒有相關的配置文件,只要執行一條命令就可以對文件進行加密。
把加密的文件傳給需要的人後,只要他知道加密方式和加密口令,就可以解密查看文件。
openssl支持的加密算法很多,包括:bf,cast,des,des3,idea,rc2,rc5等及以上各種的變體,具體可參閱相關文檔。
具體的方法如下:
1.加密一個文件:
# openssl enc -des -e -a -in install.log -out install.log.des
enter des-cbc encryption password:
Verifying - enter des-cbc encryption password:
輸入密碼之後,就會生成install.log.des文件,這個文件名是自己指定的,可以隨意寫。
其中:
enc表明你打算使用某個算法
-des是具體使用的某個算法
-e 表明要加密
-a 同樣是使用ASCII進行編碼
-in 要加密的文件名字
-out 加密後的文件名字

把生成的文件傳到另一臺機器後,執行如下命令進行解密
# openssl enc -des -d -a -in install.log.des -out install.log
enter des-cbc decryption password:
輸入口令後,就可以得到解密後的文件了。
其中
-d表明要進行解密
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章