crep has asked for the wisdom of the Perl Monks concerning the following question:
Thanks VERY much, -Jack C jack@crepinc.comuse Crypt::Blowfish; #The key size can range from 32 bits (4 characters) to 448 bits (56 ch +aracters). 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 = <STDIN>; chomp $key_t; my $key = pack("H16", $key_t); print "Encrypt or decrypt? ('e' or 'd'): "; my $func = <STDIN>; chomp $func; if ($func eq "e") { print "Enter file to encrypt: "; my $file = <STDIN>; chomp $file; open(fileIN, $file) or die("Can't open $file: $!"); @inFile = <fileIN>; 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 writi +ng: $!"); 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 = <STDIN>; chomp $file; open(fileIN, $file) or die("Can't open $file: $!"); @inFile = <fileIN>; 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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Blowfish Encryption problem
by beable (Friar) on Jul 14, 2004 at 02:15 UTC | |
|
Re: Blowfish Encryption problem
by pboin (Deacon) on Jul 14, 2004 at 14:52 UTC | |
by crep (Novice) on Jul 14, 2004 at 23:59 UTC | |
by thor (Priest) on Jul 15, 2004 at 04:22 UTC |