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

Hi everyone!
I'm trying to mimic a bash script in perl and i'm stuck on trying to decode a encode file i previously fetch from mega.co.nz.
That's the bash code with some filled-in variables:

openssl enc -d -aes-128-ctr -K 101488e2533e9a4428afbb2d0055258d -iv 69eec1fb38b11fc30000000000000000 -in asd.enc -out asd

I've tried to decode the same file with the same key/iv without success using Crypt::CBC and Rijndael without luck, that's the snippet i got from another monk i'm actually trying:
my $cipher = Crypt::CBC->new( -cipher => 'Rijndael' , -key => $key , -iv => $iv , -regenerate_key => 0 , -padding => 'space' , -prepend_iv => 0 ); my $buffer; my $tmpfile = 'somefile'; open(FH_crypted, "<$tmpfile"); binmode FH_crypted; open(FH_decrypted, ">$tmpfile.good"); binmode FH_decrypted; $cipher->start('decrypting'); while (read(FH_crypted,$buffer,1024)) { print FH_decrypted $cipher->crypt($buffer); } print FH_decrypted $cipher->finish; $cipher->finish; close FH_crypted; close FH_decrypted;

The resulting file is just garbage, not the decoded file and one byte shorter.
Thank you for your time!

Replies are listed 'Best First'.
Re: Trying to decode aes-128-ctr
by AppleFritter (Vicar) on Sep 03, 2014 at 09:39 UTC

    Howdy starless, welcome to the Monastery!

    I don't know too much about cryptography, but you're telling OpenSSL to use CTR mode, not CBC mode, so that's what you'll have to use in Perl, too.

      Thanks, i'll give it a try!
        That was definitelly the problem, tried using Crypt::Rijndael::MODE_CTR() directly and i was able to see the jpeg header.

        Now the problem is dealing with padding because Rijndael now complains with the "encrypt: datasize not multiple of blocksize (16 bytes)" error.

        I tried Crypt::Ctr to handle all that stuff but it doesn't seem to accept a IV as an argument.
Re: Trying to decode aes-128-ctr
by Anonymous Monk on Sep 03, 2014 at 09:45 UTC
    And putting the key information into a forum post ... or into any script, for that matter ... is usually not a good idea, either.
      The key is from a random mega.co.nz public link of a jpg file. Nothing to worry about.
Re: Trying to decode aes-128-ctr
by flexvault (Monsignor) on Sep 03, 2014 at 18:38 UTC

    Welcome starless,

    If you do your 'while' loop reading 16 bytes at a time it may work, but if the end of the buffer is not exactly 16 bytes, you have to pad with "\0" to get exactly 16 bytes.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin