use Crypt::Blowfish; #The key size can range from 32 bits (4 characters) to 448 bits (56 characters). system("clear"); #*nix system("cls"); #win print "Blowfish in perl. By jack (jack [at] crepinc.com)\n\n"; print "Enter key (4 to 56 characters): "; my $key_t = ; chomp $key_t; my $key = pack("H16", $key_t); print "Encrypt or decrypt? ('e' or 'd'): "; my $func = ; chomp $func; if ($func eq "e") { print "Enter file to encrypt: "; my $file = ; chomp $file; open(fileIN, $file) or die("Can't open $file: $!"); @inFile = ; close(fileIN); my $plaintex = ""; foreach $line (@inFile) { chomp($line); $plaintex = $plaintex.$line."\n"; } open(fileOUT, ">$file.enc") or die("Can't open $file.enc for writing: $!"); my $cipher = new Crypt::Blowfish $key; #chomp $plaintex; my $len = length $plaintex; while ($len > 8) { $len -=8; } my $x = 8-$len; for ($x=0;$x<8-$len;$x++) { $plaintex = $plaintex." "; } my $len = length $plaintex; if ($len == 8) { my $ciphertext = $cipher->encrypt($plaintex); my $ciphertext = unpack("H16", $ciphertext); print fileOUT "$ciphertext"; } if ($len < 8) { $plaintex = $plaintex." "; $plaintex = substr ($plaintex,0,8); my $ciphertext = $cipher->encrypt($plaintex); my $ciphertext = unpack("H16", $ciphertext); print fileOUT "$ciphertext"; } if ($len > 8) { my $itmp = 0; for ($itmp=0; $itmp<$len; $itmp +=8) { $tmp = substr ($plaintex,$itmp,8); my $ciphertext = $cipher->encrypt($tmp); my $ciphertext = unpack("H16", $ciphertext); print fileOUT "$ciphertext"; } } close(fileOUT); print "Done.\n"; } if ($func eq "d") { print "Enter file to decrypt: "; my $file = ; chomp $file; open(fileIN, $file) or die("Can't open $file: $!"); @inFile = ; close(fileIN); my $ciphertext = ""; foreach $line (@inFile) { $ciphertext = "$ciphertext"."$line"."\n"; } my $filelen = length $file; my $ext = substr($file,$filelen-4,4); if ($ext eq ".enc") { $file = substr($file,0,$filelen-4); } open(fileOUT, ">$file") or die("Can't open $file for writing: $!"); $ciphertext = substr($ciphertext,0,$len-1); my $len = length $ciphertext; print "len $len\n"; if ($len > 8) { my $itmp = 0; for ($itmp=0; $itmp<$len; $itmp +=16) { $tmp = substr ($ciphertext,$itmp,8); $tmp = pack("H16", $tmp); my $cipher = new Crypt::Blowfish $key; my $plaintext = $cipher->decrypt($tmp); print fileOUT "$plaintext"; } } if ($len < 9) { my $cipher = new Crypt::Blowfish $key; $ciphertextpack = pack("H16", $ciphertext); my $plaintext = $cipher->decrypt($ciphertextpack); print fileOUT "$plaintext"; } print "Done.\n"; }