How to archive and encrypt a directory with a password

To tar and compress a directory, then encrypt it with the AES algorithm:

tar -czf - [directory] | gpg -c --cipher-algo AES -o [directory].tgz.gpg

You will be prompted to input a password.

To decrypt, decompress and untar:

gpg -d [directory].tgz.gpg | tar -xzf -

To can see a list of available cipher algorithms:

gpg --version

If you do not specify a cipher algorithm that uses MDC by default (like AES), gpg will default to encrypting using the CAST5 algorithm and you will get a warning message when you decrypt:

gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected

Example

steph@slap ~/tmp/gpgtest $ ls -l
total 0
drwxr-xr-x 2 steph users 80 2008-09-27 17:35 testdir
steph@slap ~/tmp/gpgtest $ ls -l testdir/
total 4
-rw-r--r-- 1 steph users 20 2008-09-27 17:34 test1.txt
steph@slap ~/tmp/gpgtest $ cat testdir/test1.txt 
testing testing 123
steph@slap ~/tmp/gpgtest $ tar -czf - testdir | gpg -c --cipher-algo AES -o testdir.tgz.gpg
steph@slap ~/tmp/gpgtest $ rm -rf testdir
steph@slap ~/tmp/gpgtest $ ls -l
total 4
-rw-r--r-- 1 steph users 236 2008-09-27 17:36 testdir.tgz.gpg
steph@slap ~/tmp/gpgtest $ gpg -d testdir.tgz.gpg | tar -xzf -
gpg: AES encrypted data
gpg: encrypted with 1 passphrase
steph@slap ~/tmp/gpgtest $ ls -l
total 4
drwxr-xr-x 2 steph users  80 2008-09-27 17:35 testdir
-rw-r--r-- 1 steph users 236 2008-09-27 17:36 testdir.tgz.gpg
steph@slap ~/tmp/gpgtest $ ls -l testdir
total 4
-rw-r--r-- 1 steph users 20 2008-09-27 17:34 test1.txt
steph@slap ~/tmp/gpgtest $ cat testdir/test1.txt 
testing testing 123

References

Last modified: 27/09/2008 Tags: ,

Related Pages

Other pages possibly of interest:

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top