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

Hello wise ones!

I'm in a fix with decrypting a string.
I have a string gave to me with the assuration it was encrypted with the Rijndael algorithm, i also have the key to decrypt it but I ever get differents errors which every try.

It seems to me the string is packed (because my tests gave many odd char..) so I tried to unpack ("H*") it before decrypt.. with no chances.
my $key = 'marco' my $str = 'e50b672aed16125ec7c66a92dd862d758580ab3ea2b8fd1092d824db07b +db812c5f7e3831e0b9bf5c1796eac20a70aa5814a6bdab7254b954de49475db42b98b +';
Any suggestion welcome

Lor*

Replies are listed 'Best First'.
Re: about crypt/decrypt strings with Rijndael
by almut (Canon) on Mar 02, 2009 at 15:29 UTC

    I think you're missing essential information, i.e. the salt that had been used to initialize the encryption.

    There are two modes to store the salt / initialisation vector (IV) together with the data:

    • OpenSSL compatible, in which case the string would begin with "Salted__"
    • RandomIV mode (legacy), in which case the string would begin with "RandomIV"

    As your data doesn't begin with either of those headers, it's presumably without a header (i.e., you should've been told what the salt/IV is, so you can specify it explicitly), or it's in some other unknown/incompatible header format...

    (BTW, there's also a decrypt_hex() convenience method, which you could use to pass your hex data directly — once you've figured out the salt issue, that is...)

Re: about crypt/decrypt strings with Rijndael
by shmem (Chancellor) on Mar 02, 2009 at 13:26 UTC
    so I tried to unpack ("H*")

    try

    $str = join '', map { chr } unpack( "(H2)*", $str );

    update: or, shorter

    $str = pack "C*", unpack "(H2)*", $str;
Re: about crypt/decrypt strings with Rijndael
by bellaire (Hermit) on Mar 02, 2009 at 13:17 UTC
    Well, not sure about the encryption algorithm, but I don't think you want to unpack the thing as one giant hex number, which is what that unpack statement will do.

    If these are 1-byte characters, the following will split up the string into chunks, pass them into unpack, then get the character represented:
    print map { chr($_) } map {unpack("H2",$_) } $str =~ m/../g;
    For me, this yields the following string:
    A$ A#??$'@& %&&=!=>B'@ @>>?BA&'B?%$= ==&"$@> "'"A'%@">&
    Is that closer to what you were looking for, or am I way off? :)
Re: about crypt/decrypt strings with Rijndael
by zentara (Cardinal) on Mar 02, 2009 at 14:29 UTC
    You didn't show your full working example, but my notes show that you may need Crypt::Rijndael::MODE_CBC. Googling it will undoubtedly show some code on how to use it.

    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: about crypt/decrypt strings with Rijndael
by Discipulus (Canon) on Mar 02, 2009 at 13:42 UTC
    thanks for your suggestions but I'm still getting errors when I try to decrypt thah string:
    ù my $cipher = Crypt::CBC->new( -key=>'marco', -cipher => 'Rijndael',#-header=>'none', ); my $str= 'e50b672aed16125ec7c66a92dd862d758580ab3ea2b8fd1092d824db07bd +b812c5f7e3831e0b9bf5c1796eac20a70aa5814a6bdab7254b954de49475db42b98b' +; $str = pack "C*", unpack "(H2)*", $str; my $decrypted = $cipher->decrypt( $str );
    I get:
    Ciphertext does not begin with a valid header for 'salt' header mode at..
      You might remove the '#' inside the argument list of the new() call. The default header mode seems to be salted which obviously leads to this error message