in reply to Encrypting a Message in C# with Rijndael and Decrypting it on Perl

It seems you don't need the Crypt::CBC module since you can use Crypt::Rijndael directly configured in CBC mode, as the following example (taken from the manual) shows:

use Crypt::Rijndael; # keysize() is 32, but 24 and 16 are also possible # blocksize() is 16 $cipher = Crypt::Rijndael->new( "a" x 32, Crypt::Rijndael::MODE_CBC() + ); # <-- key and CBC-MODE $cipher->set_iv($iv); # <-- here's your I-Vector $crypted = $cipher->encrypt($plaintext); # - OR - $plaintext = $cipher->decrypt($crypted);
If you provide the output of your programs or initialise the variables of your Perl program with meaningful values, more specific help on the Perl part might be possible. I am not sure if most of the Monks here have a C# development environment at hand in order to create their own cipher-texts ... at least I haven't.

  • Comment on Re: Encrypting a Message in C# with Rijndael and Decrypting it on Perl
  • Download Code

Replies are listed 'Best First'.
Re^2: Encrypting a Message in C# with Rijndael and Decrypting it on Perl
by zeussn (Initiate) on Jul 10, 2009 at 20:39 UTC
    Actually I just find the solution. The problem was that the PasswordDeriveByte System from C# don't create the same kind of Key/IV that the Perl CBC system (that uses openssl system). However, here is the C# code to generate de Key and IV
    private void GenerateKeyIV(string strKey, byte[] bSalt, out byte[] bEn +cKey, out byte[] bEncIV) { // Extracted from http://www.jensign.com/JavaScience/dotne +t/DeriveKeyM/ // by Michel Gallant int HASHLENGTH = 16; byte[] bKey = Encoding.UTF8.GetBytes(strKey); int count = 1; int miter = 3; byte[] keymaterial = new byte[HASHLENGTH * miter]; byte[] data00 = new byte[bKey.Length + bSalt.Length]; Array.Copy(bKey, data00, bKey.Length); Array.Copy(bSalt, 0, data00, bKey.Length, bSalt.Length); MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = null; byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; for (int j = 0; j < miter; j++) { if (j == 0) result = data00; else { Array.Copy(result, hashtarget, result.Length); Array.Copy(data00, 0, hashtarget, result.Length, d +ata00.Length); result = hashtarget; } for (int i = 0; i < count; i++) result = md5.ComputeHash(result); Array.Copy(result, 0, keymaterial, j * HASHLENGTH, res +ult.Length); } bEncKey = new byte[32]; bEncIV = new byte[16]; Array.Copy(keymaterial, 0, bEncKey, 0, 32); Array.Copy(keymaterial, 32, bEncIV, 0, 16); }
Re^2: Encrypting a Message in C# with Rijndael and Decrypting it on Perl
by ikegami (Patriarch) on Jul 10, 2009 at 20:42 UTC

    Your snippet doesn't handle padding.

    There's no reason not to use Crypt::CBC.

      There's no reason not to use Crypt::CBC.
      Unless performance is important.
      use strict; use warnings; use Crypt::CBC; use Crypt::Rijndael; use Crypt::GCrypt; my $cbc = Crypt::CBC->new( -key => 'a' x 32, -cipher => 'Rijndael' ); $cbc->start('encrypting'); my $rij = Crypt::Rijndael->new( 'a' x 32, Crypt::Rijndael::MODE_CBC ); my $gcry = Crypt::GCrypt->new( type => 'cipher', algorithm => 'rijndael', mode => 'cbc' ); $gcry->start('encrypting'); $gcry->setkey( 'a' x 32 ); $gcry->setiv( 'b' x 16 ); my $plaintext = 'plain text' x 8192; use Benchmark qw( cmpthese ); cmpthese - 10, { 'Crypt::CBC' => sub { $cbc->crypt($plaintext); }, 'Crypt::Rijndael' => sub { $rij->encrypt($plaintext); }, 'Crypt::GCrypt' => sub { $gcry->encrypt($plaintext); }, }; $cbc->finish; $gcry->finish; __END__ Rate Crypt::CBC Crypt::Rijndael Crypt::GCrypt Crypt::CBC 74.2/s -- -83% -89% Crypt::Rijndael 430/s 479% -- -39% Crypt::GCrypt 704/s 849% 64% --

      Upd: fix to improve Crypt::CBC performance