linux下文件加密方法總結

轉載: https://www.cnblogs.com/kevingrace/p/8194784.html

 

爲了安全考慮,通常會對一些重要文件進行加密備份或加密保存,下面對linux下的文件加密方法做一簡單總結:

方法一:gzexe加密
這種加密方式不是非常保險的方法,但是能夠滿足一般的加密用途,可以隱蔽腳本中的密碼等信息。
它是使用系統自帶的gzexe程序,它不但加密,同時壓縮文件。示例如下:

[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方法會把原來沒有加密的文件a.txt備份爲a.txt~ ,同時a.txt文件變成了加密文件(即變成了密文)
[root@ipsan-node03 ~]# cat a.txt                                                                                          
쏎񹺉螳򻴀p¹\v£󨵂y«0
        Fc񝹑؃ÿE󲳷0´ûm
Ͱ:n$9hss4¢03
   NeAEؚVºY¯׻ѿ¾¹«*霻­+]ᚚaΜ
                               Y$@:Wj%
                                      .iȣ¬Z®:J ¦b¶mC<ӿ8n
[root@ipsan-node03 ~]# cat a.txt~
hahahaha
 
通常使用gzexe加密後,會將備份文件(這裏指a.txt~)刪除
[root@ipsan-node03 ~]# ls
a.txt  a.txt~ 
[root@ipsan-node03 ~]# rm -f a.txt~
[root@ipsan-node03 ~]# ls
a.txt
 
使用-d參數進行解壓操作
[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 <[email protected]>.
 
解壓之後的文件a.txt內容就會還原回來,同時也會將之前的加密文件變成a.txt~,同樣,通常也會刪除這個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~
쏎񹺉螳򻴀p¹\v£󨵂y«0
        Fc񝹑؃ÿE󲳷0´ûm
Ͱ:n$9hss4¢03
   NeAEؚVºY¯׻ѿ¾¹«*霻­+]ᚚaΜ
                               Y$@:Wj%
                                      .iȣ¬Z®:J ¦b¶mC<ӿ8n
 
[root@ipsan-node03 ~]# rm -f a.txt~
[root@ipsan-node03 ~]# ls
a.txt

方法二:用tar命令 對文件加密壓縮和解壓

[root@ipsan-node03 ~]# ls
test.txt
[root@ipsan-node03 ~]# cat test.txt
hahahaha
heiheihei
   
如下命令是對filename文件(test.txt)進行加密壓縮,生成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
 
或者根據匹配規則進行加密壓縮
[root@ipsan-node03 ~]# tar -zcf - /mnt/pass_* |openssl des3 -salt -k 123@123 | dd of=test.des3
---------------------------------------------------------------------------------------------------------
   
通常加密後,會將源文件刪除
[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©|>þHބ*𓑂ܪ³󾯵@ⴹ񑋟𻹾qk)B㲏¡qk;ochl\cz-𤥴/흯
¤ވտ+¾´2AuK𷁆픏̞t悐ah¤ºʀ񧦇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可以沒有,沒有時在解壓時會提示輸入密碼

方法三:結合Tar和OpenSSL給文件和目錄加密及解密

當有重要的敏感數據的時候,給文件和目錄額外加一層保護是至關重要的,特別是當需要通過網絡與他人傳輸數據的時候。基於這個原因,
可以用到tar(Linux 的一個壓縮打包工具)和OpenSSL來解決的方案。藉助這兩個工具,你真的可以毫不費力地創建和加密 tar 歸檔文件。
 
下面介紹使用 OpenSSL創建和加密 tar 或 gz(gzip,另一種壓縮文件)歸檔文件:
牢記使用 OpenSSL 的常規方式是:
# 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
 
現在要加密當前工作目錄的內容(根據文件的大小,這可能需要一點時間)
[root@ipsan-node03 mnt]# tar -czf - * | openssl enc -e -aes256 -out test.tar.gz
enter aes-256-cbc encryption password:                          //假設這裏設置的密碼爲123456
Verifying - enter aes-256-cbc encryption password:
 
上述命令的解釋:
enc 使用加密進行編碼
-e  用來加密輸入文件的 enc 命令選項,這裏是指前一個 tar 命令的輸出
-aes256 加密用的算法
-out 用於指定輸出文件名的 enc 命令選項,這裏文件名是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
 
對於上面加密後的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歸檔內容,需要使用以下命令。
[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  將加壓後的文件提取到目標目錄下
 
當你在本地網絡或因特網工作的時候,你可以隨時通過加密來保護你和他人共享的重要文本或文件,這有助於降低將其暴露給惡意攻擊者的風險。

方法四:shc加密(僅僅對shell腳本加密)

shc是一個專業的加密shell腳本的工具.它的作用是把shell腳本轉換爲一個可執行的二進制文件,這個辦法很好的解決了腳本中含有IP、
密碼等不希望公開的問題。
  
如果你的shell腳本包含了敏感的口令或者其它重要信息, 而且你不希望用戶通過ps -ef(查看系統每個進程的狀態)捕獲敏感信息. 你可以
使用shc工具來給shell腳本增加一層額外的安全保護. shc是一個腳本編譯工具, 使用RC4加密算法, 它能夠把shell程序轉換成二進制可執
行文件(支持靜態鏈接和動態鏈接). 該工具能夠很好的支持: 需要加密, 解密, 或者通過命令參數傳遞口令的環境.
  
shc的官網下載地址: 
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
這步是必須的,不然安裝過程中會報錯,shc將安裝命令到/usr/local/bin/目錄下;
將幫助文檔存放在/usr/local/man/man1/目錄下,如果系統中無此目錄,安裝時會報錯,可創建此目錄後再執行安裝
  
[root@ipsan-node03 shc-3.8.9]# make install
這是要回答yes或者y,不能直接回車,否則會報錯
  
需要注意的是,sch只能能shell腳本文件進行加密,其他文件都不可以!
  
sch加密使用方法:
"-f"選項指定需要加密的程序
[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選項, -f 後跟要加密的腳本名。
運行後會生成兩個文件,script-name.x 和 script-name.x.c
script-name.x是加密後的可執行的二進制文件.
./script-name.x 即可運行.
script-name.x.c是生成script-name.x的原文件(c語言)
[root@ipsan-node03 ~]# ./text.sh
hahaha
[root@ipsan-node03 ~]# ./text.sh.x
hahaha
  
通常從安全角度考慮:
使用sch命令對shell腳本文件進行加密後,只需保留.x的二進制文件即可,其他兩個文件均可以刪除!
[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還提供了一種設定有效執行期限的方法,可以首先使用shc將shell程序轉化爲二進制,並加上過期時間,如:
[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:指定過期時間爲2018年2月28日
-m:過期後打印出的信息;
-v: verbose
-r: 可在相同操作系統的不同主機上執行
-f: 指定源shell
  
如果在過期後執行,則會有如下提示:
[root@ipsan-node03 ~]# ./text.sh.x
./text.sh.x: this script file is about to expire
使用以上方法要注意,需防止用戶更改系統時間,可以通過在程序中加入自動更新系統時間的命令來解決此問題!!
  
sch的幫助命令:
[root@ipsan-node03 ~]# shc -help
shc Version 3.8.9, Generic Script Compiler
shc Copyright (c) 1994-2012 Francisco Rosales <[email protected]>
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]   (指定過期日期)
    -m %s  Message to display upon expiration ["Please contact your provider"]  (指定過期提示的信息)
    -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   (可以相同操作系統的不同系統中執行)
    -v     Verbose compilation    (編譯的詳細情況)
    -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  <none>   C compiler flags
  
    Please consult the shc(1) man page.
  
說明:
經測試,相同在操作系統,shc後的可執行二進制文件直接可以移植運行,但不同操作系統可能會出現問題,
比如將上面的test.sh.x的二進制文件在CentOS6.9上加密後移到redhat as5u4上不能運行,出現"Floating point exception"錯誤提示,
但移到另一臺CentOS6.9上直接運行沒問題。

方法五: ZIP加密

1)文件加密
使用命令"zip -e filename.zip filename" 即可出現輸入密碼的提示,輸入2次密碼。 此文件即被加密解壓時候是需要密碼的

下面開始爲test.txt文件進行加密
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
[root@centos6-vm02 ~]# zip -e test.txt.zip test.txt         //如下進行加密操作時,需要輸入兩次密碼
Enter password:                          
Verify password:
  adding: test.txt (stored 0%)
[root@centos6-vm02 ~]# ls
test.txt  test.txt.zip
 
進行解壓的時候,需要輸入密碼
[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"即可出現輸入密碼的提示,輸入2次密碼。 此文件即被加密解壓時候是需要密碼的。

下面開始對目錄進行加密
[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%)
 
解壓目錄時需要輸入密碼
[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隱私保護(GNU Privacy Guard),常常被稱爲GPG,它結合了一組加密軟件。它是由GNU項目用C編程語言編寫的。最新的穩定版本是2.0.27。在如今的大多數Linux發行版中,gnupg程序包都是默認隨帶的,所以萬一它沒有安裝,你可以使用apt或yum從軟件庫來安裝它(yum install gnupg)。注意:gpg只能對文件進行加密,對目錄則無法完成加密!

下面開始使用GnuPG方式對test.txt文件進行加密
[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         //這個信息可以忽略
 
注意:如上加密的時候,會彈出來一個對話框,要求Paraphrase輸入兩次密碼,對這個特定的文件進行加密。
 
一旦運行帶-c選項(完全使用對稱密碼算法加密)的gpc命令,它會生成一個文件.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
 
對文件進行加密後,最好將源文件刪除!不要再保留源文件了!
[root@centos6-vm02 ~]# rm -f test.txt
 
文件解密操作。
注意出現Paraphrase提示時,需要提供加密時輸入的同一個密碼才能解密
[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!!!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章