新聞中心
為了保證文件的安全,我們可以通過(guò)對(duì)文件加密的方式來(lái)對(duì)自己的數(shù)據(jù)進(jìn)行保護(hù),加密之后的數(shù)據(jù)只有使用正確的加密密碼才能解密,因此大大的增加了文件的保密性,本篇文章為大家詳細(xì)講解一下Linux下文件加密的具體方法。

方法一:gzexe加密
這種加密方式不是非常保險(xiǎn)的方法,但是能夠滿足一般的加密用途,可以隱蔽腳本中的密碼等信息。 它是使用系統(tǒng)自帶的gzexe程序,它不但加密,同時(shí)壓縮文件。示例如下:
[root@ipsan-node03 ~]# echo "hahahaha" > a.txt
[root@ipsan-node03 ~]# cat a.txt
hahahaha
[root@ipsan-node03 ~]# ls a.txt
a.txt
[root@ipsan-node03 ~]# gzexe a.txt
a.txt: 22.2%
[root@ipsan-node03 ~]# ls
a.txt a.txt~
gzexe方法會(huì)把原來(lái)沒(méi)有加密的文件a.txt備份為a.txt~ ,同時(shí)a.txt文件變成了加密文件(即變成了密文)
[root@ipsan-node03 ~]# cat a.txt
??螳?p1\v£?y?0
Fc???E?0′?m
?:n$9hss4¢03
NeAE?VoYˉ???1?*霻?+]?aΜ
Y$@:Wj%
.i??Z?:J |b?mC# cat a.txt~
hahahaha
通常使用gzexe加密后,會(huì)將備份文件(這里指a.txt~)刪除
[root@ipsan-node03 ~]# ls
a.txt a.txt~
[root@ipsan-node03 ~]# rm -f a.txt~
[root@ipsan-node03 ~]# ls
a.txt
使用-d參數(shù)進(jìn)行解壓操作
[root@ipsan-node03 ~]# gzexe --help
Usage: /usr/bin/gzexe [OPTION] FILE...
Rename each FILE with a compressed version of itself, renaming FILE to FILE~.
-d Decompress each FILE instead of compressing it.
--help display this help and exit
--version output version information and exit
Report bugs to
. 解壓之后的文件a.txt內(nèi)容就會(huì)還原回來(lái),同時(shí)也會(huì)將之前的加密文件變成a.txt~,同樣,通常也會(huì)刪除這個(gè)a.txt~的備份文件 [root@ipsan-node03 ~]
# gzexe -d a.txt [root@ipsan-node03 ~]
# ls a.txt a.txt~ [root@ipsan-node03 ~]
# cat a.txt hahahaha [root@ipsan-node03 ~]
# cat a.txt~ ??螳?p1\v£?y?0 Fc???E?0′?m ?:n
$9hss4¢03 NeAE?VoYˉ???1?*霻?+]?aΜ Y
$@:Wj% .i??Z?:J |b?mC# rm -f a.txt~ [root@ipsan-node03 ~]
# ls a.txt
方法二:用tar命令 對(duì)文件加密壓縮和解壓
[root@ipsan-node03 ~]# ls
test.txt
[root@ipsan-node03 ~]# cat test.txt
hahahaha
heiheihei
如下命令是對(duì)filename文件(test.txt)進(jìn)行加密壓縮,生成filename.des3加密壓縮文件,123@123為加密的密碼
[root@ipsan-node03 ~]# tar -zcf - test.txt |openssl des3 -salt -k 123@123 | dd of=test.txt.des3
0+1 records in
0+1 records out
152 bytes (152 B) copied, 0.00333366 s, 45.6 kB/s
---------------------------------------------------------------------------------------------------------
也可以將/mnt目錄下的所有文件全部加密壓縮
[root@ipsan-node03 ~]# tar -zcf - /mnt/* |openssl des3 -salt -k 123@123 | dd of=test.des3
或者根據(jù)匹配規(guī)則進(jìn)行加密壓縮
[root@ipsan-node03 ~]# tar -zcf - /mnt/pass_* |openssl des3 -salt -k 123@123 | dd of=test.des3
---------------------------------------------------------------------------------------------------------
通常加密后,會(huì)將源文件刪除
[root@ipsan-node03 ~]# ls
test.txt test.txt.des3
[root@ipsan-node03 ~]# rm -f test.txt
[root@ipsan-node03 ~]# cat test.txt.des3
Salted__H?+ZCHaW??
\bS?|>tH?*??3?@???qk)B??qk;ochl\cz-?/?
¤??+?′2AuK???t悐ah¤o??d
解壓操作:
[root@ipsan-node03 ~]# dd if=test.txt.des3 |openssl des3 -d -k 123@123 | tar zxf -
0+1 records in
0+1 records out
152 bytes (152 B) copied, 4.5873e-05 s, 3.3 MB/s
[root@ipsan-node03 ~]# ls
test.txt test.txt.des3
[root@ipsan-node03 ~]# cat test.txt
hahahaha
heiheihei
注意命令最后面的"-",它將釋放所有文件,
-k 123@123可以沒(méi)有,沒(méi)有時(shí)在解壓時(shí)會(huì)提示輸入密碼
方法三:結(jié)合Tar和OpenSSL給文件和目錄加密及解密
當(dāng)有重要的敏感數(shù)據(jù)的時(shí)候,給文件和目錄額外加一層保護(hù)是至關(guān)重要的,特別是當(dāng)需要通過(guò)網(wǎng)絡(luò)與他人傳輸數(shù)據(jù)的時(shí)候?;谶@個(gè)原因,
可以用到tar(Linux 的一個(gè)壓縮打包工具)和OpenSSL來(lái)解決的方案。借助這兩個(gè)工具,你真的可以毫不費(fèi)力地創(chuàng)建和加密 tar 歸檔文件。
下面介紹使用 OpenSSL創(chuàng)建和加密 tar 或 gz(gzip,另一種壓縮文件)歸檔文件:
牢記使用 OpenSSL 的常規(guī)方式是:
# openssl command command-options arguments
示例如下:
[root@ipsan-node03 ~]# cd /mnt/
[root@ipsan-node03 mnt]# ls
[root@ipsan-node03 mnt]# echo "123" > a.txt
[root@ipsan-node03 mnt]# echo "456" > b.txt
[root@ipsan-node03 mnt]# echo "789" > c.txt
[root@ipsan-node03 mnt]# ls
a.txt b.txt c.txt
現(xiàn)在要加密當(dāng)前工作目錄的內(nèi)容(根據(jù)文件的大小,這可能需要一點(diǎn)時(shí)間)
[root@ipsan-node03 mnt]# tar -czf - * | openssl enc -e -aes256 -out test.tar.gz
enter aes-256-cbc encryption password: //假設(shè)這里設(shè)置的密碼為123456
Verifying - enter aes-256-cbc encryption password:
上述命令的解釋:
enc 使用加密進(jìn)行編碼
-e 用來(lái)加密輸入文件的 enc 命令選項(xiàng),這里是指前一個(gè) tar 命令的輸出
-aes256 加密用的算法
-out 用于指定輸出文件名的 enc 命令選項(xiàng),這里文件名是test.tar.gz
[root@ipsan-node03 mnt]# ls
a.txt b.txt c.txt test.tar.gz
[root@ipsan-node03 mnt]# rm -rf a.txt
[root@ipsan-node03 mnt]# rm -rf b.txt
[root@ipsan-node03 mnt]# rm -rf c.txt
[root@ipsan-node03 mnt]# ls
test.tar.gz
對(duì)于上面加密后的tar包直接解壓肯定是不行的!
[root@ipsan-node03 mnt]# tar -zvxf test.tar.gz
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
要解密上述tar歸檔內(nèi)容,需要使用以下命令。
[root@ipsan-node03 mnt]# openssl enc -d -aes256 -in test.tar.gz | tar xz -C /mnt/
enter aes-256-cbc decryption password:
[root@ipsan-node03 mnt]# ls
a.txt b.txt c.txt test.tar.gz
上述命令的解釋:
-d 用于解密文件
-C 將加壓后的文件提取到目標(biāo)目錄下
當(dāng)你在本地網(wǎng)絡(luò)或因特網(wǎng)工作的時(shí)候,你可以隨時(shí)通過(guò)加密來(lái)保護(hù)你和他人共享的重要文本或文件,這有助于降低將其暴露給惡意攻擊者的風(fēng)險(xiǎn)。
方法四:shc加密(僅僅對(duì)shell腳本加密)
shc是一個(gè)專業(yè)的加密shell腳本的工具.它的作用是把shell腳本轉(zhuǎn)換為一個(gè)可執(zhí)行的二進(jìn)制文件,這個(gè)辦法很好的解決了腳本中含有IP、
密碼等不希望公開的問(wèn)題。
如果你的shell腳本包含了敏感的口令或者其它重要信息, 而且你不希望用戶通過(guò)ps -ef(查看系統(tǒng)每個(gè)進(jìn)程的狀態(tài))捕獲敏感信息. 你可以
使用shc工具來(lái)給shell腳本增加一層額外的安全保護(hù). shc是一個(gè)腳本編譯工具, 使用RC4加密算法, 它能夠把shell程序轉(zhuǎn)換成二進(jìn)制可執(zhí)
行文件(支持靜態(tài)鏈接和動(dòng)態(tài)鏈接). 該工具能夠很好的支持: 需要加密, 解密, 或者通過(guò)命令參數(shù)傳遞口令的環(huán)境.
shc的官網(wǎng)下載地址:
http://www.datsi.fi.upm.es/~frosal/sources/
安裝方法:
[root@ipsan-node03 ~]# cd /usr/local/src/
[root@ipsan-node03 src]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
[root@ipsan-node03 src]# tar -zvxf shc-3.8.9.tgz
[root@ipsan-node03 src]# cd shc-3.8.9
[root@ipsan-node03 shc-3.8.9]# mkdir -p /usr/local/man/man1
這步是必須的,不然安裝過(guò)程中會(huì)報(bào)錯(cuò),shc將安裝命令到/usr/local/bin/目錄下;
將幫助文檔存放在/usr/local/man/man1/目錄下,如果系統(tǒng)中無(wú)此目錄,安裝時(shí)會(huì)報(bào)錯(cuò),可創(chuàng)建此目錄后再執(zhí)行安裝
[root@ipsan-node03 shc-3.8.9]# make install
這是要回答yes或者y,不能直接回車,否則會(huì)報(bào)錯(cuò)
需要注意的是,sch只能能shell腳本文件進(jìn)行加密,其他文件都不可以!
sch加密使用方法:
"-f"選項(xiàng)指定需要加密的程序
[root@ipsan-node03 ~]# ls
text.sh
[root@ipsan-node03 ~]# cat text.sh
#!/bin/bash
echo "hahaha"
[root@ipsan-node03 ~]# shc -r -f text.sh
[root@ipsan-node03 ~]# ls
text.sh text.sh.x text.sh.x.c
注意:要有-r選項(xiàng), -f 后跟要加密的腳本名。
運(yùn)行后會(huì)生成兩個(gè)文件,script-name.x 和 script-name.x.c
script-name.x是加密后的可執(zhí)行的二進(jìn)制文件.
./script-name.x 即可運(yùn)行.
script-name.x.c是生成script-name.x的原文件(c語(yǔ)言)
[root@ipsan-node03 ~]# ./text.sh
hahaha
[root@ipsan-node03 ~]# ./text.sh.x
hahaha
通常從安全角度考慮:
使用sch命令對(duì)shell腳本文件進(jìn)行加密后,只需保留.x的二進(jìn)制文件即可,其他兩個(gè)文件均可以刪除!
[root@ipsan-node03 ~]# ls
text.sh text.sh.x text.sh.x.c
[root@ipsan-node03 ~]# rm -rf text.sh
[root@ipsan-node03 ~]# rm -rf text.sh.x.c
[root@ipsan-node03 ~]# ls
text.sh.x
[root@ipsan-node03 ~]# ./text.sh.x
hahaha
另外:
shc還提供了一種設(shè)定有效執(zhí)行期限的方法,可以首先使用shc將shell程序轉(zhuǎn)化為二進(jìn)制,并加上過(guò)期時(shí)間,如:
[root@ipsan-node03 ~]# shc -e 28/02/2018 -m "this script file is about to expire" -v -r -f text.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc text.sh.x.c -o text.sh.x
shc: strip text.sh.x
shc: chmod go-r text.sh.x
[root@ipsan-node03 ~]# ls
text.sh text.sh.x text.sh.x.c
解釋:
-e:指定過(guò)期時(shí)間為2018年2月28日
-m:過(guò)期后打印出的信息;
-v: verbose
-r: 可在相同操作系統(tǒng)的不同主機(jī)上執(zhí)行
-f: 指定源shell
如果在過(guò)期后執(zhí)行,則會(huì)有如下提示:
[root@ipsan-node03 ~]# ./text.sh.x
./text.sh.x: this script file is about to expire
使用以上方法要注意,需防止用戶更改系統(tǒng)時(shí)間,可以通過(guò)在程序中加入自動(dòng)更新系統(tǒng)時(shí)間的命令來(lái)解決此問(wèn)題??!
sch的幫助命令:
[root@ipsan-node03 ~]# shc -help
shc Version 3.8.9, Generic Script Compiler
shc Copyright (c) 1994-2012 Francisco Rosales
shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script -e %s Expiration date
in dd/mm/yyyy format [none] (指定過(guò)期日期) -m %s Message to display upon expiration [
"Please contact your provider"] (指定過(guò)期提示的信息) -f %s File name of the script to compile (指定要編譯的shell的路徑及文件名) -i %s Inline option
for the shell interpreter i.e: -e -x %s eXec
command, as a
printf format i.e:
exec(
'%s',@ARGV); -l %s Last shell option i.e: -- -r Relax security. Make a redistributable binary (可以相同操作系統(tǒng)的不同系統(tǒng)中執(zhí)行) -v Verbose compilation (編譯的詳細(xì)情況) -D Switch ON debug
exec calls [OFF] -T Allow binary to be traceable [no] -C Display license and
exit -A Display abstract and
exit -h Display
help and
exit Environment variables used: Name Default Usage CC cc C compiler
command CFLAGS
C compiler flags Please consult the shc(1) man page. 說(shuō)明: 經(jīng)測(cè)試,相同在操作系統(tǒng),shc后的可執(zhí)行二進(jìn)制文件直接可以移植運(yùn)行,但不同操作系統(tǒng)可能會(huì)出現(xiàn)問(wèn)題, 比如將上面的test.sh.x的二進(jìn)制文件在CentOS6.9上加密后移到redhat as5u4上不能運(yùn)行,出現(xiàn)
"Floating point exception"錯(cuò)誤提示, 但移到另一臺(tái)CentOS6.9上直接運(yùn)行沒(méi)問(wèn)題。
方法五: ZIP加密
1)文件加密 使用命令”zip -e filename.zip filename” 即可出現(xiàn)輸入密碼的提示,輸入2次密碼。 此文件即被加密解壓時(shí)候是需要密碼的
下面開始為test.txt文件進(jìn)行加密
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
[root@centos6-vm02 ~]# zip -e test.txt.zip test.txt //如下進(jìn)行加密操作時(shí),需要輸入兩次密碼
Enter password:
Verify password:
adding: test.txt (stored 0%)
[root@centos6-vm02 ~]# ls
test.txt test.txt.zip
進(jìn)行解壓的時(shí)候,需要輸入密碼
[root@centos6-vm02 ~]# rm -f test.txt
[root@centos6-vm02 ~]# unzip test.txt.zip
Archive: test.txt.zip
[test.txt.zip] test.txt password:
extracting: test.txt
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
2)文件夾加密 使用命令”zip -re dirname.zip dirname”即可出現(xiàn)輸入密碼的提示,輸入2次密碼。 此文件即被加密解壓時(shí)候是需要密碼的。
下面開始對(duì)目錄進(jìn)行加密
[root@centos6-vm02 ~]# mkdir dirtest
[root@centos6-vm02 ~]# cat dirtest/haha.txt
this is test of dir!!!
[root@centos6-vm02 ~]# zip -re dirtest.zip dirtest
Enter password:
Verify password:
adding: dirtest/ (stored 0%)
adding: dirtest/haha.txt (stored 0%)
解壓目錄時(shí)需要輸入密碼
[root@centos6-vm02 ~]# rm -rf dirtest
[root@centos6-vm02 ~]# unzip dirtest.zip
Archive: dirtest.zip
creating: dirtest/
[dirtest.zip] dirtest/haha.txt password:
extracting: dirtest/haha.txt
[root@centos6-vm02 ~]# ls dirtest
haha.txt
[root@centos6-vm02 ~]# cat dirtest/haha.txt
this is test of dir!!!
方法六:GnuPG加密
GnuPG的全稱是GNU隱私保護(hù)(GNU Privacy Guard),常常被稱為GPG,它結(jié)合了一組加密軟件。它是由GNU項(xiàng)目用C編程語(yǔ)言編寫的。最新的穩(wěn)定版本是2.0.27。在如今的大多數(shù)Linux發(fā)行版中,gnupg程序包都是默認(rèn)隨帶的,所以萬(wàn)一它沒(méi)有安裝,你可以使用apt或yum從軟件庫(kù)來(lái)安裝它(yum install gnupg)。注意:gpg只能對(duì)文件進(jìn)行加密,對(duì)目錄則無(wú)法完成加密!
下面開始使用GnuPG方式對(duì)test.txt文件進(jìn)行加密
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
[root@centos6-vm02 ~]# gpg -c test.txt
can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory //這個(gè)信息可以忽略
注意:如上加密的時(shí)候,會(huì)彈出來(lái)一個(gè)對(duì)話框,要求Paraphrase輸入兩次密碼,對(duì)這個(gè)特定的文件進(jìn)行加密。
一旦運(yùn)行帶-c選項(xiàng)(完全使用對(duì)稱密碼算法加密)的gpc命令,它會(huì)生成一個(gè)文件.gpg文件。
[root@centos6-vm02 ~]# ll test.txt*
-rw-r--r--. 1 root root 18 Jan 4 10:08 test.txt
-rw-r--r--. 1 root root 61 Jan 4 10:04 test.txt.gpg
對(duì)文件進(jìn)行加密后,最好將源文件刪除!不要再保留源文件了!
[root@centos6-vm02 ~]# rm -f test.txt
文件解密操作。
注意出現(xiàn)Paraphrase提示時(shí),需要提供加密時(shí)輸入的同一個(gè)密碼才能解密
[root@centos6-vm02 ~]# gpg test.txt.gpg
gpg: 3DES encrypted data
can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected
[root@centos6-vm02 ~]# ll test.txt*
-rw-r--r--. 1 root root 18 Jan 4 10:08 test.txt
-rw-r--r--. 1 root root 61 Jan 4 10:04 test.txt.gpg
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
文章名稱:Linux下文件加密的具體方法
地址分享:http://m.fisionsoft.com.cn/article/cogcjcp.html


咨詢
建站咨詢
