Linux下建立加密的壓縮文件算法
假設你想建立一個zip歸檔文件,而且具備密碼保護,這樣無論是誰試圖解壓這個zip文件時候,都必須知道正確的密碼。在Linux上,有幾種方法能夠加密ZIP文件,或者對zip文件進行密碼保護.安全
下面咱們來介紹經常使用的3種加密方式:markdown
方法一:
zip命令行工具提供了一個加密選項。
zip命令所使用的是PKZIP加密算法。
PKZIP算法被稱爲是不安全的。
此外,設置的密碼,被以純文本顯示,使得它更加脆弱。工具
1.使用ZIP命令建立一個加密的ZIP文件:
$ zip --password mypasscode all.zip 1.txt 2.txt adding: 1.txt (stored 0%) adding: 2.txt (stored 0%)
2.解壓縮加密文件時,會提示要求輸入密碼:
$unzip all.zip Archive: all.zip [all.zip] 1.txt password:
方法二:
使用7z進行文件歸檔,能夠建立更加安全的加密zip文件,7z使用AES-256加密算法,SHA-256散列算法生成密鑰。this
1.使用7z建立一個zip文件:
$ 7z a -t7z -p doc_folder.7z doc_folder 7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,4 CPUs) Scanning Creating archive doc_folder.7z Enter password (will not be echoed) : Verify password (will not be echoed) : Compressing doc_folder/.7z Compressing doc_folder/test.txt Everything is Ok
2.解壓縮加密文件:
$ 7z x doc_folder.7z 7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,4 CPUs) Processing archive: doc_folder.7z Enter password (will not be echoed) : Extracting doc_folder/.7z Extracting doc_folder/test.txt Extracting doc_folder Everything is Ok Folders: 1 Files: 2 Size: 37 Compressed: 252
3.使用限制
注意:若是要備份文件及文件owner/group,要先用tar打包加密
Backup and limitations DO NOT USE the 7-zip format for backup purpose on Linux/Unix because : - 7-zip does not store the owner/group of the file. On Linux/Unix, in order to backup directories you must use tar : - to backup a directory : tar cf - directory | 7za a -si directory.tar.7z - to restore your backup : 7za x -so directory.tar.7z | tar xf - If you want to send files and directories (not the owner of file) to others Unix/MacOS/Windows users, you can use the 7-zip format. example : 7za a directory.7z directory Do not use "-r" because this flag does not do what you think. Do not use directory/* because of ".*" files (example : "directory/*" does not match "directory/.profile")
方法三:
還有一種建立加密壓縮包的方式,就是採用GnuPG的對稱密鑰加密spa
1.To create an encrypted compressed tar archive with GnuPG:
$ tar czvpf – doc.pdf doc2.pdf doc3.pdf | gpg --symmetric --cipher-algo aes256 -o secure.tar.gz.gpg
2.To uncompress an archive file encrypted with GnuPG:
$ gpg -d secure.tar.gz.gpg | tar xzvf -