#!/usr/bin/perl -w

####################################################################################
# 
# Copyright (C) 2000 Zorgon <zorgon@linuxstart.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
#
####################################################################################

use strict;
use locale;
use Getopt::Std;
use Crypt::Blowfish;

use vars qw($opt_r $opt_f $opt_e $opt_d $name @files);

&usage unless getopts('r:f:edk:');
&usage unless(defined $opt_r || defined $opt_f); 
&usage if(defined $opt_r && defined $opt_f);  
&usage unless(defined $opt_e || defined $opt_d);
&usage if(defined $opt_e && defined $opt_d);

my $key = "";

print "(cryptfolder.pl) - (c) Zorgon (zorgon\@linuxstart.com) 2000. \n\n";

get_key();
my $key_valid = pack("H16", $key);
my $proc = new Crypt::Blowfish $key_valid;
list_folder();

foreach $name (@files){
    print "### File $name \n";
    encrypt($name) if(defined $opt_e);
    decrypt($name) if(defined $opt_d);
}

print "\n";

####################################################################################
sub get_key {
    my $key_temp = "";

    print "Your key : ";
    system("stty -echo");
    chomp($key=<STDIN>);
    system("stty echo");
 
    print "\nAgain your key : ";
    system("stty -echo");
    chomp($key_temp=<STDIN>);
    system("stty echo");
    print "\n";

    if($key ne $key_temp) { 
	print "Sorry, keys do not match\n"; 
	get_key();
    }
}

####################################################################################
sub list_folder {
    my($name,$ext);
        
    if(defined $opt_r){
	opendir(DIR,$opt_r) || die "Can't open dir $opt_r: $!";
	for(readdir(DIR)){
	    next if($_ eq ".");
	    next if($_ eq "..");
	    next if((-d $_) || (-l $_));
	    push(@files,"$opt_r/$_");
	}
	closedir(DIR);
    }
    else {
	push(@files,$opt_f) if(defined $opt_f);
    }
}

####################################################################################
sub encrypt {
    my @temp_data = ();
    my $encrypt_data = "";

    open(FILE,$name) || die "Can't open file $name: $!";
    @temp_data = <FILE>;
    close(FILE);

    foreach (@temp_data){
	while ( length $_ > 0 ) {
	    while ( length $_ < 8) { $_ .= "\t"; }
	    my $encrypt_temp_data = $proc->encrypt(substr($_,0,8));
	    $encrypt_data .= $encrypt_temp_data;
	    if (length $_ > 8) {$_ = substr($_,8);} else {$_ = "";}
        }
    }

    open(FILE,">$name") || die "Can't open file $name: $!";
    print FILE "$encrypt_data";
    close(FILE);
}
			
####################################################################################
sub decrypt {
    my @temp_data = ();
    my $decrypt_data = "";

    open(FILE,$name) || die "Can't open file $name: $!";
    @temp_data = <FILE>;
    close(FILE);

    foreach (@temp_data){
	while (length $_ > 0)  
	{
	    my $temp2_data = substr($_,0,8);
	    if (length $temp2_data == 8) 
	    {
		my $temp3_data = $proc->decrypt($temp2_data);
		$decrypt_data .= $temp3_data;
	    } 
	    if (length $_ > 8) {$_ = substr($_,8);} else {$_ = "";}
	}
    }
    $decrypt_data =~ s/\t+$//g;

    open(FILE,">$name") || die "Can't open file $name: $!";
    print FILE "$decrypt_data";
    close(FILE);
}

####################################################################################
sub usage {	
	print "(c) Zorgon (zorgon\@linuxstart.com) 2000. \n\n";
	print "Usage: cryptfolder.pl [-rf files...] [-ed] [-k key] \n";
  	print "Script cryptant un fichier ou un repertoire.\n";
  	print "  -r 	   Nom de repertoire fourni\n";
	print "  -f 	   Nom de fichier fourni\n";
	print "  -e        Cryptage\n";
	print "  -d        Decryptage\n";
  	exit(1);
}

exit 0;
