Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)

by by88 (Initiate)
on Sep 27, 2008 at 16:02 UTC ( #714030=perlquestion: print w/replies, xml ) Need Help??

by88 has asked for the wisdom of the Perl Monks concerning the following question:

I'm encrypting/decrypting data with Crypt::Rijndael. I've encrypted and decrypted many pieces of data without any trouble. Suddenly I've encountered a piece of data that won't decrypt. I get the error:

encrypt: datasize not multiple of blocksize (16 bytes) at z.pl line 25.

I've tested other pieces of data encrypted the same way and it works. Is there anything encrypting would do that would make decrypting impossible (like put bad random characters or spaces or something that would throw the decryption off)?

Here's some code for more detail:

use Crypt::Rijndael; $iv = 'key_that_is_16_characters_long'; $cipher = Crypt::Rijndael->new( "a" x 32, Crypt::Rijndael::MODE_CBC ); $cipher->set_iv($iv);
I do use these characters in $iv: &#$*@ (not sure if that matters). I've been made aware this isn't very secure ("a"x32), but I'm not so much worried about that right now.
  • Comment on decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
  • Download Code

Replies are listed 'Best First'.
Re: decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
by Illuminatus (Curate) on Sep 27, 2008 at 17:04 UTC
    Well, you give us 4 lines of code, and an error message for code at line 25. You say you are getting the error message when decrypting, but the message says it is for encrypt.

    As you probably know, Rijndael is a block-cipher algorithm. Your data must be a multiple of 16 in size. It seems like you know this. Keep in mind also that this is a binary algorithm -- there are no invalid values, either on input or output. One thing to look out for is encrypted data manipulation. Your encrypted data can contain anything. If you inadvertently perform any string-like operation on it, the results are unpredictable.

    Please provide the encrypt/decrypt sample code if you want more feedback
Re: decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
by ikegami (Pope) on Sep 27, 2008 at 19:41 UTC
Re: decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
by zentara (Archbishop) on Sep 27, 2008 at 19:00 UTC
    You are probably looking for this, notice the get16 sub. The module will give a warning if the key length is wrong.
    #!/usr/bin/perl use warnings; use strict; use Crypt::Rijndael; # keys must be 128, 192 or 256 "bits" long, # 8 bits in a letter, so 8 x 16 = 128 #my $key = 'abcdefgghjkloiuy'; #128 bits or 8 x 16 my $key = 'abcdefgghjkloiuyabcdefgghjkloiuy'; #256 bits or 8 x 32 print "key->$key\n"; my $plaintext= "adrqwqqqqqqqqqqqqqqqqqqqqqqwrxcq4gfq3g2q45g2q43g5"; print "plaintext->$plaintext\n"; my $plaintext16= get16($plaintext); print "plaintext16->$plaintext16\n"; my $cbc = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC; my $crypted = $cbc->encrypt($plaintext16); print 'crypted->',"$crypted\n"; #keep encrypted string in different print string #to avoid character corruption of preceding string my $cbc1 = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC; my $decrypted = $cbc1->decrypt($crypted); print "decrypted->$decrypted\n"; #this sub makes all data blocksize of 16 bytes. sub get16 { my $data = shift; return $data . "\0" x ( 16 - length($data)%16 ); } exit; #You can also use this to get data that is in a multiple of 16 bytes: # this requires $username < 16 bytes and prefix packs with spaces # sprintf '%16s', $username; #or you can xor it # $filecrypted = $cipher->encrypt($file^("\0"x16)); #To strip any padding after decryption: #my $plaintext = $cipher->decrypt($secret_stuff); # $plaintext =~ s/^\0+//; # remove leading null bytes, use \s (not \0) for spaces

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
by by88 (Initiate) on Sep 28, 2008 at 14:58 UTC
    Thanks for the replies. I guess this is more of a general question because my encryption/decryption scripts work (they encrypt the data and I'm able to decrypt until this one). But here is the code doing the work:
    ENCRYPT SCRIPT #!/usr/bin/perl use DBI; use Crypt::Rijndael; $iv = 'same_16_character_string'; $cipher = Crypt::Rijndael->new( "a" x 32, Crypt::Rijndael::MODE_CBC ); $cipher->set_iv($iv); $data = $cipher->encrypt($some_data); ** $data is then inserted into a blob field in mysql (data in the acco +unts table) DECRYPT SCRIPT #!/usr/bin/perl use DBI; use Crypt::Rijndael; $iv = 'same_16_character_string'; $cipher = Crypt::Rijndael->new( "a" x 32, Crypt::Rijndael::MODE_CBC ); $cipher->set_iv($iv); $sql_data = "SELECT data FROM accounts WHERE account_id = '237' LIMIT +1"; $qry_data = $dbh->prepare($sql_data); $qry_data->execute(); $data = $qry_data->fetchrow_hashref(); $data = $data->{data}; print $cipher->decrypt($data) . "\n\n";
    So as you can see I do no extra work on the data before it is inserted into the blob field...just a simple encrypt. The problem now is I have a piece of data encrypted this way that won't decrypt. I get that error on the $cipher->decrypt($data) line, but only with this specific piece of data.

      Notice the first line of the documentation?

      "Crypt::CBC compliant Rijndael encryption module"

      That should tell you something!

      use Crypt::CBC; my $key = "a" x 32; my $cipher = Crypt::CBC->new( -cipher => 'Rijndael', -key => $key ); my $ciphertext = $cipher->encrypt($plaintext);
      use Crypt::CBC; my $key = "a" x 32; my $cipher = Crypt::CBC->new( -cipher => 'Rijndael', -key => $key ); my $plaintext = $cipher->decrypt($ciphertext);

      It handles padding, for you.
      It handles salting for you.
      It handles chaining for you.

      And make sure your database field is large enough.

      So close, and yet so far... You have left out the critical (and most likely problematic) piece; the DB insertion. I will bet that if you try to decrypt immediately after you encrypt, it will work fine. The problem is that either your insert or select is munging up the data. I have not used blobs with perl/dbi, so I am not aware of specific issues. Look at http://www.james.rcpt.to/programs/mysql/blob/. Is this the way you are creating the insert?
        I can't imagine that's the problem, but I guess it's possible. Here's the update:
        $sql_update = "UPDATE accounts SET data = '" . $data . "', encrypted = + '1' WHERE account_id = '" . $account_id . "'"; $qry_update = $dbh->prepare($sql_update); $qry_update->execute();
        $data is the encrypted piece

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://714030]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-04-15 02:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?