Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A script for compressing and encrypting directories' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script for compressing and encrypting directories
Authored by: ukkarhu on Nov 14, '03 10:44:25AM

I've enhanced this script a bit so it will encrypt files and directories using a command line type thing i.e. crypt <filename> or crypt -d <filename> to decrypt. If you specify crypt -d -r <filename>, it will automtically decrypt and expand your compressed file. If you don't want to use tar, you can replace the 'tar' command with something that is resource fork friendly.

The whole script is availble from my idisk public folder (apadley). Here is the code if you're interested.

[code]
#!/bin/bash
#
#
# Define usage routine
usage()
{
[ "$1" ] && echo "crypt: $*"

cat <<!

usage: crypt [ -d][-r] Filename / Directoryname

options: -d : Decrypt mode
If this parameter is not used, the default
is to encrypt the file or directory.
-r : Recursive Decryption
User this parameter to automatically
expand a decrypted file you know is a dir.
!

exit 8
}
delfile()
{
if [ $? -eq 0 ]
then
rm -R $1
fi
}
#
# Parse the commandline options
#
CRYPT=0
ISDIR=0
while getopts :d:r OPT
do
case $OPT in
"d") CRYPT=1 ;;
"r") ISDIR=1 ;;
"?") usage "Invalid option: -$OPTARG" ;;
":") "Mandatory parameter missing for -$OPTARG option" ;;
esac
done

shift `expr $OPTIND - 1`
if [ "$#" -ne 1 ]
then
usage "You must supply a file or directory name"
exit 1
else
SOURCE="$1"
fi
#
# Encrypt a file or directory
#
echo $SOURCE
echo $CRYPT
if [ -f $SOURCE -a $CRYPT -eq 0 ] # Encrypt File
then
openssl bf -in $SOURCE -out $SOURCE.bf
delfile $SOURCE
elif [ -d $SOURCE -a $CRYPT -eq 0 ] # Encrypt a directory
then
tar czf tmp$$.tar.gz $SOURCE
delfile $SOURCE
openssl bf -in tmp$$.tar.gz -out $SOURCE.tar.gz.bf
delfile tmp$$.tar.gz
#
# Decrypt File or Directory
#
elif [ -f $SOURCE -a $CRYPT -eq 1 ] # Decrypt a file
then
openssl bf -d -in $SOURCE -out `echo $SOURCE | sed 's/...$//'`
delfile $SOURCE
if [ $ISDIR=1 ]
then
tar xzf `echo $SOURCE | sed 's/...$//'`
delfile `echo $SOURCE | sed 's/...$//'`
fi
else
usage "Incorrect parameter combination"
fi
exit 0

[/code]



[ Reply to This | # ]