After not being able to find any suitable code that would do blowfish for me, I wrote one. It’s a simple script that uses built-in perl peices, but works great nonetheless. It’s fast, simple, and handy! You can get it here, or view the source below (albeit with wierd formatting):
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::CBC;
run();
sub run {
#system("clear"); #*nix
system("cls"); #win
print("Blowfish encryption by Jack Carrozzo (jack [at] crepinc.com)\n");
print("Enter key (8 to 56 charactors): ");
my $key =
chomp $key;
print(”Encrypt or Decrypt? (’e’ or ‘d’): “);
my $mode =
chomp $mode;
my $cipher = Crypt::CBC->new( {’key’ => $key,
‘cipher’ => ‘Blowfish’,
‘iv’ => ‘$KJh#(}q’,
‘regenerate_key’ => 0,
‘padding’ => ’space’,
‘prepend_iv’ => 0,
‘pcbc’ => 1
});
if ($mode eq “e”) {
encrypt_file($cipher);
} elsif ($mode eq “d”) {
decrypt_file($cipher);
} else {
print(”\nYou must enter ‘e’ or ‘d’!\n”);
}
}
sub encrypt_file
{
my $cipher = shift;
print “Enter name of file to encrypt: “;
my $plain_file =
chomp $plain_file;
print “Enter output filename [$plain_file.enc]: “;
my $enc_file =
chomp $enc_file;
if ($enc_file eq “”) {
$enc_file = “$plain_file.enc”;
}
open OUTFILE, “>$enc_file” or die “can’t open $enc_file: $!”;
my $buffer;
print “\nEncrypting…”;
$cipher->start(’encrypting’);
open(F,”$plain_file”) or die “can’t open $plain_file: $!”;
while (read(F, $buffer, 1024))
{
print OUTFILE $cipher->crypt($buffer);
}
print OUTFILE $cipher->finish;
close F;
close OUTFILE;
print “Done.\n”;
}
sub decrypt_file
{
my $cipher = shift;
print “Enter filename to decrypt: “;
my $enc_file =
chomp $enc_file;
my $len = length $enc_file;
my $ext = substr($enc_file,$len-3,3);
my $dec_file_o = “”;
if ($ext eq “enc”) {
$dec_file_o = substr($enc_file,0,$len-4);
} else {
$dec_file_o = $enc_file.”.dec”;
}
print “Enter filename to output [$dec_file_o]: “;
my $dec_file =
chomp $dec_file;
if ($dec_file eq “”) {
$dec_file = $dec_file_o;
}
open INFILE, “< $enc_file" or die "can't open $enc_file: $!";
open DECFILE, ">$dec_file” or die “can’t open $dec_file: $!”;
print “\nDecrypting…”;
$cipher->start(’decrypting’);
my $buffer;
while (read(INFILE, $buffer, 1024))
{
print DECFILE $cipher->crypt($buffer);
}
print DECFILE $cipher->finish;
close DECFILE;
close INFILE;
print “Done.\n”;
}