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

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.
  • Comment on Re: decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
  • Download Code

Replies are listed 'Best First'.
Re^2: decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
by ikegami (Patriarch) on Sep 29, 2008 at 19:12 UTC

    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.

Re^2: decrypt problems with Crypt::Rijndael (datasize not multiple of blocksize)
by Illuminatus (Curate) on Sep 28, 2008 at 19:16 UTC
    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

        Use placeholders!

        $sql_update = 'UPDATE accounts SET data = ?, encrypted = 1 WHERE accou +nt_id = ?'; $qry_update = $dbh->prepare($sql_update); $qry_update->execute($data, $account_id);