#!/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;
}
In reply to cbc
by jettero
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.