I'm currently putting the finishing touch on a massive file transfer utility. The files are ZIPped, encrypted and attached to email then sent. On reception, the files are detached, decrypted then unZIPped.

I'm using the combo Crypt:CBC and Crypt::Twofish for the encryption.

Sending the files works flawlessly. Partway trough reception, I get the following error:
Cipher stream did not contain IV or salt, and you did not specify thes +e values in new() at (...)

The file's beginning looks like this:
RandomIV$gÑ“ç

So, I can decrypt a couple of files and then the programs fails even though the "RandomIV" is there...

The hard part is handled within a custom object.
sub _symmetric { #Applies symmetric cipher caller eq __PACKAGE__ or die "_symmetric is a private method\n"; die "instance method only" unless ref (my $self = shift); my $ifname = shift; my ($tfname, $ofname, $fh_plain, $fh_cipher); my $cipher = Crypt::CBC->new($self->{SYMMKEY}, 'Twofish') # Usi +ng AES-level cipher or die "Unable to create cipher: $@\n"; if ($self->{MODE} eq TRANSMIT) { $tfname = $ifname . ZIP; $ofname = $ifname . ENC; # A - compress $self->_compressor($ifname, $tfname); die "Unable to delete ${ifname}: $!\n" unless (unlink $ifname) +; # B - encrypt $fh_plain = IO::File->new($tfname, O_RDONLY) or die "Unable to + open ${tfname}: $!\n"; $fh_plain->binmode; $fh_cipher = IO::File->new($ofname, O_WRONLY | O_CREAT) or die + "Unable to create ${ofname}: $!\n"; $fh_cipher->binmode; $cipher->start('encrypting'); $fh_cipher->write($cipher->crypt($fh_plain->getline)) until ($ +fh_plain->eof); $fh_cipher->write($cipher->finish); $fh_cipher->flush; $fh_plain->close; $fh_cipher->close; die "Unable to delete ${tfname}: $!\n" unless (unlink $tfname) +; } else { my @name_parts = split(/\./, $ifname); $ofname = shift(@name_parts); $tfname = $ofname . ZIP; # A - decrypt if ( (stat($ifname))[7] ) { $fh_cipher = IO::File->new($ifname, O_RDONLY) or die "Unab +le to open ${ifname}: $!\n"; $fh_cipher->binmode; $fh_plain = IO::File->new($tfname, O_WRONLY | O_CREAT) or +die "Unable to create ${tfname}: $!\n"; $fh_plain->binmode; # # It fails on the following line # $cipher->start('decrypting'); $fh_plain->write($cipher->crypt($fh_cipher->getline)) unti +l ($fh_cipher->eof); $fh_plain->write($cipher->finish); $fh_plain->flush; $fh_cipher->close; $fh_plain->close; # B - restore if ( (stat($tfname))[7] ) { $self->_compressor($tfname, $ofname); } die "Unable to delete ${tfname}: $!\n" unless (unlink $tfn +ame); } die "Unable to delete ${ifname}: $!\n" unless (unlink $ifname) +; } return $ofname; }

Does anyone has an idea why it happens?

Solostian

Readmore tags added by GrandFather


In reply to Crypt::CBC losing its RandomIV by Solostian

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.