Hello masters of the art... I am rather new, sorry for my bad coding. Basically, all I want this script to do is output a blowfish encrypted file, then be able to make it text again. So far, it appears to run, but if I encrypt a file, then imediatly try to decrypt it with the same key, it doesn't work. I think the problem may be in the string parsing. Any ideas? blowfish.pl
use 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"; }
Thanks VERY much, -Jack C jack@crepinc.com

In reply to Blowfish Encryption problem by crep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.