Here is a message encrypted using BlowfishNET. The key used for encryption is "Abcdefghijklmnopqrstuvwxyz1234567890".
IV is not specified when the call is made to BlowfishNET. I store the encrypted message in a file called "BFSTest_encoded.csv".

mGWcw1XJV3ooXbGvzBuShOA2gwcUnW11NZnHm1Nm 8JQktRSoaAKl369jL8ZLnJNF71I5ctZq8wGGtfov iLj6PoXZWlzcasgBBUMefjGa0h+L53/VODmqCDaL 6jnZycB5VTngY5gt/9VpYxVW0KB5yR0TJrhHQa02 Vgn1I9ePtmbJfOkALXZPwY0H8MDULkzIYX409iSm 6zgu76UvW81qpJiakFDi9jmIbL4NVWYRlVR1xLRC HiM8X8q0yCT1ZQO9HJE8vHc/jKdEOi8QGNyAOtdd ku7Ikg4Pzwpk7g+R/TOCx7Q8xDKbV93sFHu1s3co pZrpCcNmd8Oy1CFazZ5FweWwsFqfgxMqopMrtaG6 Qc1J7cB3fYpI2RDN6xbAJ8HPZWGdOQsobopv+Czt GFuzVaVfwz8YXrLEXlbRFfv8Cpt8Q320NAAhwjFj eEUMyPCpzVZaBske4NDJ++7JomijlS8H8cHmmD6P Hbopy41W8BF9tsV9nqXgYeiVna98MNTEkyToyP/Z kBhGYJcutv3s9pG7SydwuKCxCd+Dugusnu8lM10/ 5UmGJ7YTGFCJvtfpXR9XoMCQ++eT4/YN9kZlim+2 bkjh01xPnG9r7PChWAVxpUVjOisGlZKrPV6tQRPU qwQ0die7XienUt5e+jACACGMqXWA4O8P5sg/iT3w Z2+dDIQiUnxCIATKn74/B8V9PDdtHnr5XOP5KPKK aYcz/gXt1Q6WkWawUIoLvCkobkm5o7MXeAiJCtet emfA86bV

When decrypted correctly it should look like this:

blowfishsimple,3030,encrypt,this,then,decrypt,1232,it0
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it1
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it2
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it3
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it4
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it5
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it6
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it7
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it8
blowfishsimple,3030,encrypt,this,then,decrypt,1232,it9

Below is the runnable program with which I am unsuccessfully trying to decrypt the encrypted message above.
#!/usr/bin/perl -w use strict; use warnings; use Crypt::CBC; use Fcntl ':flock'; use MIME::Base64; my $encodedFileName = "BFSTest_encoded.csv"; my $decodedFileName = "BFSTest_decoded.csv"; my $decryptedFileName = "BFSTest_decrypted.csv"; my $cCipher = "Blowfish"; my $cKey = q{Abcdefghijklmnopqrstuvwxyz1234567890}; my $cIV = "randomiv"; my $cObject = Crypt::CBC->new({ key => $cKey, cipher => $cCipher }); croak( "Invalid cipher type for encryption" ) unless $cObject; sub read_bytes { my ($fh, $to_read) = @_; my $buf = ''; while ($to_read) { my $bytes_read = read($fh, $buf, $to_read, length($buf)); die("$!\n") if !defined($bytes_read); die("Unexpected end of file\n") if !$bytes_read; $to_read -= $bytes_read; } return $buf; } sub decryptNETData { my ( $length, $buffer ); print "Now DECRYPTING $decodedFileName for current client ... \n"; open( my $fhREADFROM, "<", $decodedFileName ) || die("Cannot open +encrypted file $decodedFileName"); binmode $fhREADFROM; flock $fhREADFROM, LOCK_SH; open( finalFile, ">", $decryptedFileName ) || die("Cannot open fil +e $decryptedFileName to write decrypted contents"); binmode finalFile; flock finalFile, LOCK_EX; # Tried to slurp, decrypt and decode here, but that did not work e +ither. my $decodedFile = do { local( $/ ) ; <$fhREADFROM> } ; utf8::decode( my $str = $cObject->decrypt( $decodedFile ) ); print finalFile $str; #while (!eof($fhREADFROM)) { # $length = unpack('N', read_bytes($fhREADFROM, 4)); # $buffer = read_bytes($fhREADFROM, $length); # print finalFile $cObject->decrypt( $buffer ); #} close $fhREADFROM; close finalFile; print "Decrypted $decodedFileName to $decryptedFileName. \n"; } sub decodeData { open(my $fhENCODEDFILE, "<", $encodedFileName) || die("Cannot open + file $encodedFileName"); binmode $fhENCODEDFILE; flock $fhENCODEDFILE, LOCK_EX; open(my $fhDECODEDFILE, ">", $decodedFileName) || die("Cannot open + file $decodedFileName"); binmode $fhDECODEDFILE; flock $fhDECODEDFILE, LOCK_EX; my $utf8base64EncodedFile = do { local( $/ ) ; <$fhENCODEDFILE> } +; #utf8::decode( my $str = decode_base64($utf8base64EncodedFile) ); print $fhDECODEDFILE decode_base64($utf8base64EncodedFile); close $fhENCODEDFILE; close $fhDECODEDFILE; print "Decoded $encodedFileName to $decodedFileName. \n"; } decodeData(); decryptNETData();

Also, I am copying below the encrypt subroutine from the BlowfishSimple class. I apologize if this is of no use. I don't understand how it is padding the data. I assume encryption is performed in the following order:

We have taken your input and are encrypting the entire file as once. Therefore, the input to the BlowfishSimple class would be the entire file that needs to be encrypted.
public String Encrypt(String plainText) { int i, origLen, len, mod; byte[] ueData, inBuf, outBuf, iv; ueData = Encoding.UTF8.GetBytes(plainText); origLen = ueData.Length; len = origLen; mod = len % BlowfishCBC.BLOCK_SIZE; len = (len - mod) + BlowfishCBC.BLOCK_SIZE; inBuf = new byte[len]; Array.Copy(ueData, 0, inBuf, 0, origLen); i = len - (BlowfishCBC.BLOCK_SIZE - mod); while (i < len) { inBuf[i++] = (byte)mod; } outBuf = new byte[inBuf.Length + BlowfishCBC.BLOCK_SIZE]; iv = new byte[BlowfishCBC.BLOCK_SIZE]; this.rng.GetBytes(iv); this.bfc.IV = iv; this.bfc.Encrypt( inBuf, 0, outBuf, BlowfishCBC.BLOCK_SIZE, inBuf.Length); Array.Copy(iv, 0, outBuf, 0, BlowfishCBC.BLOCK_SIZE); String sResult = Convert.ToBase64String(outBuf); Array.Clear(inBuf, 0, inBuf.Length); return sResult; }

Thank you for your help.


In reply to Re^2: Decrypting BlowfishNET (was Re^11: line by line Encryption ...) by samip
in thread line by line Encryption fun with Crypt::CBC and Rijndael? File Ownership issues? by hmbscully

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.