Encrypt Files and Directories Easily
I was recently looking for an easy way to encrypt individual files and directories (recursively), and I ran across the linux command mcrypt. This nifty little utility does just what I want, but doesn’t do anything fancy – it just does encryption on a single file or standard input.
With a wee bitty script, however, you can encrypt anything you like quite easily. You have to have mcrypt installed (and also tar & bzip2, but you’ve likely got that already). Check this out:
#!/bin/bash
IFS=$’\n’
if [[ -z $3 ]]
then
echo “Use: encrypt [file/directory] [password] [outputname]“
exit
fi
echo “Encrypting $1 with password $2 into file $3″
tar -c $1 | mcrypt -p -q -k $2 > $3
echo “Done with encryption.”
Save it as “encrypt.sh” or whatever other name floats your boat, give it execute permissions, and you’re all set. It will tar, compress, and encrypt your file(s) and directories into whatever output file you specify. Just make sure you don’t forget the password you use to encrypt the file with: there isn’t any easy way to find out what it was if you lose it.
In order to decrypt your data, use this little script:
#!/bin/bash
IFS=$’\n’
if [[ -z $2 ]]
then
echo “Use: decrypt [file/directory] [password]“
exit
fi
echo “Decrypting $1 with password $2″
cat $1 | mdecrypt -q -p -k $2 | tar –x
echo “Done with decryption.”
Save it as “decrypt.sh” and give it execute permissions, and now you can easily decrypt your data as well. It can’t really get much easier than that!