Thursday, July 02, 2009

Compress and encrypt directory in Linux

直接進入實作步驟~
Compress tools: tar
Data encrypt tools: AES Crypt (http://www.aescrypt.com/)

Objective-1, To backup /etc and encrypt it:
1. 壓縮並加密/etc:
# tar cvf - /etc | aescrypt -e -p 123456 -> etc.tar.aes
(-e: encrypt, -p password)

2. 測試etc.tar.aes是否有加密:
[root@localhost ~]# file etc.tar.aes
etc.tar.aes: data
[root@localhost ~]# tar tvf etc.tar.aes
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: Read 300 bytes from etc.tar.aes
tar: Error exit delayed from previous errors
測試結果發現到etc.tar.aes已經被AES加密了,故無法直接以tar觀看壓縮檔的內容。

Objective-2 To decrypt and uncompress data:
1. 將壓縮檔解密:
# aescrypt -d -p 123456 etc.tgz.aes 
解密後會新增一檔案為etc.tar
2. 解壓縮:
# tar xvf etc.tar
如此一來便可解開加密過後的壓縮檔了。

其它在Linux上常見的加密工具還有openssl與gpg。

No comments: