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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.