Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: AES Interoperability between Perl and C#

by ikegami (Patriarch)
on Nov 17, 2005 at 07:39 UTC ( [id://509327]=note: print w/replies, xml ) Need Help??

in reply to Re: AES Interoperability between Perl and C#
in thread AES Interoperability between Perl and C#

Your code doesn't work if the cleartext is not an exact multiple of 16 bytes in length (and maybe even if it is), because Crypt::Rijndael doesn't handle padding. Crypt::CBC handles that.

Your Perl code manually prepends/extracts the IV to/from the encrypted text. Crypt::CBC handles that.

Crypt::CBC has other features too, such as generating a random IV when so desired.

That's why Crypt::Rijndael should be in conjunction with Crypt::CBC instead of using Crypt::Rijndael's CBC mode.

The fix (and simplification) is:

use strict; use warnings; use MIME::Base64; use Crypt::CBC; my $key = pack("H*", "01020304050607080910111213141516"); my $cipher = Crypt::CBC->new(-cipher => 'Rijndael'); my $in = decode_base64(<>); print "out = '", unpack("N/A", $cipher->decrypt($in)), "'\n";

By the way, I removed use bytes since it was useless since you didn't use any numbers except the constants 0 and 16.

Also, I don't think Crypt::Rijndael's new sets $! (or ever returns false).

(Untested. I don't have these modules.)

Replies are listed 'Best First'.
Re^3: AES Interoperability between Perl and C#
by Thelonius (Priest) on Nov 17, 2005 at 10:23 UTC
    My program works fine for strings that are not multiples of 16 bytes.

    I could not get Crypt:CBC to work in a compatible way. I might have gotten in to work by experimenting with padding and the IV on both sides, but since my method works, there didn't seem to be any advantage.

      Oh I see! You (redundantly) include the length of the plaintext, and require extra code on the decryption side to remove the padding. If you used Crypt::CBC, you wouldn't need to include the length of the plaintext in the ciphertext.

      Caveat: I'm assuming FlushFinalBlock uses a padding method compatible with Crypt::CBC's. Well, Crypt::CBC since Crypt::CBC can handle any padding, so I guess I meant "compatible with the default Crypt::CBC padding method".

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://509327]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-07-14 13:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.