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

Hello all,
I have been looking all over for a method to take an encrypted string produced by the RijndaelManaged class (in .net framework) and decrypt it in perl.

I have the IV and Key and the method that the string was built, but I don't know (and haven't looked into the .net source as of yet) how the class handles the iv and key.

For Example:
use strict; use Crypt::Rijndael; my $key = "qwertyuiopllkjhgfdsa"; my $iv = “mnbvcxzasdfghjkloiuy"; my $rcipher = Crypt::Rijndael->new ($key, Crypt::Rijndael::MODE_CBC()) +; $rcipher->set_iv($iv); my $encrypt_string = “STRING_PULLED_FROM_FILE”; my $plain_text = $rcipher->decrypt($encrypt_string);
Now,

The above does not work because it is lacking the information that I need, in this case the method to covert the IV to a block size of 16 bytes and the method to covert the Key to the same method utilized by the RijndaelManaged class.

Currently, I use Crypt::Rijndael and/or Crypt::CBC (both decrypt and encrypt) in other perl code with the following (based on my example above).
use strict; use Crypt::Rijndael; use Digest::MD5 qw(md5 md5_hex); my $key = "qwertyuiopllkjhgfdsa"; my $iv = “mnbvcxzasdfghjkloiuy"; $key = md5_hex $key; $iv = md5 $iv; my $rcipher = Crypt::Rijndael->new ($key, Crypt::Rijndael::MODE_CBC()) +; $rcipher->set_iv($iv); my $data = “PLAINTEXT_TOBE_ENC”; my $encrypt_string = $rcipher->encrypt($data); my $plain_text = $rcipher->decrypt($encrypt_string);
Does anyone know the correct method to do the same with a string encrypted with RijndaelManaged?

I can provide more specific code if need, please let me know if you can help.

Replies are listed 'Best First'.
Re: decrypting .net RijndaelManaged with perl
by sflitman (Hermit) on Jun 02, 2009 at 04:19 UTC
    I had a similar problem with Twofish about a decade ago. The biggest problem was that there was a definite difference in implementation, basically like starting with different IVs or maybe endian problems on different platforms. My solution was to get the C++ code and code up an exact workalike XS (Crypt::Twofish). You may need to do the same; look at RijndaelManaged's source (C#?) and get the constants it uses, then see if you can make your own private version of Crypt::Rijndael which handles it. A pure perl version would be fine too, although slow, if you're willing to take the time to decrypt legacy encrypted data, but that wouldn't work for production environment code.

    HTH,
    SSF

      Or write a small encrypter/decrypter in C# and run it as a child (IPC::Open2)
        That is an interesting idea, I'll have to look into it.
      I had a feeling that was what I had to do.

      Yes, it is a C# class being used and with a breif look at teh source it looks like a byte_array (byte[]) for the key and iv are being used.

      Is there are striaght forward way in perl (short of doing a bit by bit translation) to convert into a byte_array?

      Maybe pack / unpack?