Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

cbc

by jettero (Monsignor)
on Nov 16, 2000 at 18:25 UTC ( [id://41966]=sourcecode: print w/replies, xml ) Need Help??
Category: Cryptography
Author/Contact Info jettero @ cpan dot org
Description: A niffty utility to crypt text files using a password. It uses blowfish to do it. Sadly, because I used stty, I suspect that this program will not work under windows.
bash$ cbc some_text_file
bash$ cbc some_text_file.cbc
bash$ cbc -c some_text_file.cbc
#!/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;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://41966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-16 17:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found