#!/usr/bin/perl
#
# For months, there was a notice here
# stating that this was broken.
#
# I no-longer believe that to be the case.
# I explained in node: 6364
#
my $good_password = sub {
return length($_[0])>4 ? 1 : 0;
};
my $double_checks = 1;
use Crypt::CBC;
my $pw_cache = "";
my %todo = ();
my $cat_it = 0;
foreach(@ARGV) {
if(m/^-c$/) {
$cat_it = 1;
next;
}
if(-f $_) {
if(m/[.]cbc$/) {
push @{ $todo{decrypt} }, $_;
} else {
push @{ $todo{encrypt} }, $_;
}
}
}
if (@{ $todo{encrypt} }) {
foreach(@{ $todo{encrypt} }) {
my $cipher = new Crypt::CBC(&pass($double_checks+1), 'Blowfish
+');
my $buffer;
print STDERR "encypting: $_\n";
open IN, "$_" or die "$!";
open OUT, ">$_.cbc" or die "$!";
$cipher->start('encrypting');
while(read(IN, $buffer, 1024)) {
print OUT $cipher->crypt($buffer);
}
print OUT $cipher->finish;
close IN;
close OUT;
unlink $_;
}
} elsif (@{ $todo{decrypt} }) {
foreach(@{ $todo{decrypt} }) {
my $cipher = new Crypt::CBC(&pass(1), 'Blowfish');
my $buffer;
print STDERR "decrypt: $_\n";
open IN, "$_" or die "$!"; s/[.]cbc$//;
if(not $cat_it) {
open OUT, ">$_" or die "$!";
}
$cipher->start('decrypting');
while(read(IN, $buffer, 1024)) {
if($cat_it) {
print $cipher->crypt($buffer);
} else {
print OUT $cipher->crypt($buffer);
}
}
print OUT $cipher->finish;
close IN;
if(not $cat_it) {
close OUT;
}
}
}
sub pass {
return $pw_cache if length($pw_cache);
my @pw = ();
for(1..$_[0]) {
`stty -echo`;
while(not &{ $good_password }($pw[$_]) ) {
print STDERR "pw_cache ($_): ";
$pw[$_] = <STDIN>;
print "\n";
}
`stty echo`;
chomp $pw[$_];
}
for my $i (1..$_[0]) {
for my $j (1..$_[0]) {
if($pw[$i] ne $pw[$j]) {
print STDERR "The pw_caches didn't match.\n";
exit;
}
}
}
$pw_cache = $pw[1];
return $pw_cache;
}
|