溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux系統(tǒng)文件加密是怎么實現(xiàn)的

發(fā)布時間:2022-01-26 10:51:39 來源:億速云 閱讀:120 作者:柒染 欄目:開發(fā)技術(shù)

Linux系統(tǒng)文件加密是怎么實現(xiàn)的,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

Linux系統(tǒng)一切皆為文件而且Linux系統(tǒng)又是一個多用戶系統(tǒng),所以數(shù)據(jù)的安全性非常重要,有些情況需要對文件進(jìn)行加密,那么Linux系統(tǒng)中如何對文件進(jìn)行加密呢?

方法一:gzexe加密 這種加密方式不是非常保險的方法,但是能夠滿足一般的加密用途,可以隱蔽腳本中的密碼等信息。 它是使用系統(tǒng)自帶的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.
 
 
 
 解壓之后的文件a.txt內(nèi)容就會還原回來,同時也會將之前的加密文件變成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方法二:用tar命令 對文件加密壓縮和解壓 [root@ipsan-node03 ~]# ls
 
 test.txt
 
 [root@ipsan-node03 ~]# cat test.txt
 
 hahahaha
 
 heiheihei
 
   
 
 如下命令是對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
 
 ---------------------------------------------------------------------------------------------------------
 
   
 
 通常加密后,會將源文件刪除
 
 [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可以沒有,沒有時在解壓時會提示輸入密碼方法三:結(jié)合Tar和OpenSSL給文件和目錄加密及解密 當(dāng)有重要的敏感數(shù)據(jù)的時候,給文件和目錄額外加一層保護(hù)是至關(guān)重要的,特別是當(dāng)需要通過網(wǎng)絡(luò)與他人傳輸數(shù)據(jù)的時候。基于這個原因,
 
 可以用到tar(Linux 的一個壓縮打包工具)和OpenSSL來解決的方案。借助這兩個工具,你真的可以毫不費(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)時間)
 
 [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  用來加密輸入文件的 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歸檔內(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)工作的時候,你可以隨時通過加密來保護(hù)你和他人共享的重要文本或文件,這有助于降低將其暴露給惡意攻擊者的風(fēng)險。方法四:shc加密(僅僅對shell腳本加密) shc是一個專業(yè)的加密shell腳本的工具.它的作用是把shell腳本轉(zhuǎn)換為一個可執(zhí)行的二進(jìn)制文件,這個辦法很好的解決了腳本中含有IP、
 
 密碼等不希望公開的問題。
 
   
 
 如果你的shell腳本包含了敏感的口令或者其它重要信息, 而且你不希望用戶通過ps -ef(查看系統(tǒng)每個進(jìn)程的狀態(tài))捕獲敏感信息. 你可以
 
 使用shc工具來給shell腳本增加一層額外的安全保護(hù). shc是一個腳本編譯工具, 使用RC4加密算法, 它能夠把shell程序轉(zhuǎn)換成二進(jìn)制可執(zhí)
 
 行文件(支持靜態(tài)鏈接和動態(tài)鏈接). 該工具能夠很好的支持: 需要加密, 解密, 或者通過命令參數(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
 
 這步是必須的,不然安裝過程中會報錯,shc將安裝命令到/usr/local/bin/目錄下;
 
 將幫助文檔存放在/usr/local/man/man1/目錄下,如果系統(tǒng)中無此目錄,安裝時會報錯,可創(chuàng)建此目錄后再執(zhí)行安裝
 
   
 
 [root@ipsan-node03 shc-3.8.9]# make install
 
 這是要回答yes或者y,不能直接回車,否則會報錯
 
   
 
 需要注意的是,sch只能能shell腳本文件進(jìn)行加密,其他文件都不可以!
 
   
 
 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 后跟要加密的腳本名。
 
 運(yùn)行后會生成兩個文件,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語言)
 
 [root@ipsan-node03 ~]# ./text.sh
 
 hahaha
 
 [root@ipsan-node03 ~]# ./text.sh.x
 
 hahaha
 
   
 
 通常從安全角度考慮:
 
 使用sch命令對shell腳本文件進(jìn)行加密后,只需保留.x的二進(jìn)制文件即可,其他兩個文件均可以刪除!
 
 [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)制,并加上過期時間,如:
 
 [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: 可在相同操作系統(tǒng)的不同主機(jī)上執(zhí)行
 
 -f: 指定源shell
 
   
 
 如果在過期后執(zhí)行,則會有如下提示:
 
 [root@ipsan-node03 ~]# ./text.sh.x
 
 ./text.sh.x: this script file is about to expire
 
 使用以上方法要注意,需防止用戶更改系統(tǒng)時間,可以通過在程序中加入自動更新系統(tǒng)時間的命令來解決此問題??!
 
   
 
 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]   (指定過期日期)
 
     -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   (可以相同操作系統(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.
 
   
 
 說明:
 
 經(jīng)測試,相同在操作系統(tǒng),shc后的可執(zhí)行二進(jìn)制文件直接可以移植運(yùn)行,但不同操作系統(tǒng)可能會出現(xiàn)問題,
 
 比如將上面的test.sh.x的二進(jìn)制文件在CentOS6.9上加密后移到redhat as5u4上不能運(yùn)行,出現(xiàn)"Floating point exception"錯誤提示,
 
 但移到另一臺CentOS6.9上直接運(yùn)行沒問題。方法五: ZIP加密1)文件加密 使用命令”zip -e filename.zip filename” 即可出現(xiàn)輸入密碼的提示,輸入2次密碼。 此文件即被加密解壓時候是需要密碼的 下面開始為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)行加密操作時,需要輸入兩次密碼
 
 Enter password:                          
 
 Verify password:
 
   adding: test.txt (stored 0%)
 
 [root@centos6-vm02 ~]# ls
 
 test.txt  test.txt.zip
 
 
 
 進(jìn)行解壓的時候,需要輸入密碼
 
 [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次密碼。 此文件即被加密解壓時候是需要密碼的。 下面開始對目錄進(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%)
 
 
 
 解壓目錄時需要輸入密碼
 
 [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項目用C編程語言編寫的。最新的穩(wěn)定版本是2.0.27。在如今的大多數(shù)Linux發(fā)行版中,gnupg程序包都是默認(rèn)隨帶的,所以萬一它沒有安裝,你可以使用apt或yum從軟件庫來安裝它(yum install gnupg)。注意:gpg只能對文件進(jìn)行加密,對目錄則無法完成加密! 下面開始使用GnuPG方式對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         //這個信息可以忽略
 
 
 
 注意:如上加密的時候,會彈出來一個對話框,要求Paraphrase輸入兩次密碼,對這個特定的文件進(jìn)行加密。
 
 
 
 一旦運(yùn)行帶-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
 
 
 
 對文件進(jìn)行加密后,最好將源文件刪除!不要再保留源文件了!
 
 [root@centos6-vm02 ~]# rm -f test.txt
 
 
 
 文件解密操作。
 
 注意出現(xiàn)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!!

看完上述內(nèi)容,你們掌握Linux系統(tǒng)文件加密是怎么實現(xiàn)的的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI