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

hi, i'll make the same function on perl who based on this function on php
function encrypt_text($plain_text, $key){ bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plain_text, MCRYPT_ +MODE_ECB)); return $encrypt_text; }
I wrote this but this is not the same result
use Crypt::Rijndael; use Crypt::CBC; sub chiffrer_string($plain_text, $key){ $key =~ pack('H*', $key); my $cipher = Crypt::CBC->new( -key => $key, -algorithm => 'RIJNDAEL_256', -mode => 'MODE_ECB', -padding => 'null', ) || die "Couldn't create CBC object"; my $cipher_text = $cipher->encrypt($plain_text); my $cipher_block = unpack ("H*", $cipher_text); return $cipher_block;
have you got any idea ?

Replies are listed 'Best First'.
Re: same encrypt word ?
by Loops (Curate) on Oct 30, 2014 at 14:29 UTC

    Okay, had zero luck making Crypt::MCrypt behave, and could not convince Crypt::Rijndael to do anything but RIJNDAEL-128. But after much casting about, success with Mcrypt:

    use Mcrypt; my $key = 'test-math' . "\0" x 7; # Must be in blocks of 16, nul +l padded my $plaintext = "test-math" . "\0" x 23; # Must be in blocks of 32, n +ull padded my $td = Mcrypt->new( algorithm => 'rijndael-256', mode => 'ecb' ); $td->init($key, ' 'x32); # Second parameter is discarded, but warns +without my $encrypted = $td->encrypt($plaintext); print unpack('H*', $encrypted), $/; $td->end();

    It prints the glorious output that matches what you're getting in PHP:

    c2dbe4b6fec504f3249e2866dacc2a000964136de054865d407321433c001f98
      hi it works fine !!
      I am make a little adaptation of your code to use with variable
      sub chiffrer_string { my ($a_chiffrer,$cle) = @_; my $key = $cle; $key .= "\0" x (16 - length($key)); my $plain_text = $a_chiffrer; $plain_text .= "\0" x (32 - length($a_chiffrer)); my $cipher = Mcrypt->new( algorithm => 'rijndael-256', mode => + 'ecb' ); $cipher->init($key, ' 'x32); my $encrypted = $cipher->encrypt($plain_text); print unpack('H*', $encrypted), $/; $cipher->end(); }
      Very thank for your help :)
Re: same encrypt word ?
by perlron (Pilgrim) on Oct 30, 2014 at 11:24 UTC
    Hi
    Here is a working copy of code to encrypt / decrypt text based on Crypt::CBC using Rijndael Cipher. This is just to demonstrate that this is possible in perl.
    Argument 1 to program is your key eg : 1234 and Argument 2 is a file to store the encrypted text
    $perl crypt.pl 1234 crypt.txt
    #! /Users/XXX/perl5/perlbrew/perls/perl-5.20.0/bin/perl use strict; use lib "/Users/XXX/perl5/perlbrew/perls/perl-5.20.0/lib/site_perl/5.2 +0.0/darwin-2level/Crypt/"; use Crypt::CBC; #use Crypt::Rijndael; my $cipher = Crypt::CBC->new( -key => $ARGV[0], -cipher => 'Rijndael' ); my $ciphertext = $cipher->encrypt("texttoencrypt"); open(my $fh,">",$ARGV[1]); print $fh $ciphertext; close $fh; my $plaintext = $cipher->decrypt($ciphertext); print "ciphertext " , $ciphertext,"\n"; print "plaintext " , $plaintext,"\n";

    In order to get a better understanding of how the Crypt::CBC module works , the options and how to invoke the different methods look no further that metacpan.org where the module is documented.
    If you are new to perl ,do skim through perldoc perlintro first, to understand basics about perl and pointers to the larger documentation set.

    The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
      #! /Users/XXX/perl5/perlbrew/perls/perl-5.20.0/bin/perl use strict; use lib "/Users/XXX/perl5/perlbrew/perls/perl-5.20.0/lib/site_perl/5.2 +0.0/darwin-2level/Crypt/";

      I'm curious as to why you are using perlbrew like this, and not simply switching to perl 5.20 via perlbrew switch perl-5.20.0


        i pulled this from a webserver i had uploaded the file too for my personal use.

        The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
      hi,
      i see this on the man page, but the result are
      U2FsdGVkX18c3I2mq/3SdvcofHkCyUhmdd0wS5dDtRw=
      i understand why this is not the same result with php function and the encrypt word is not the same if i reexecute the script.
      maybe i have a missing argument

        Hi, For an understanding, maybe this link https://metacpan.org/pod/MCryptwill help on the different constants u are using. But i think u need a basic understanding of cipher based encryption first and foremost. From my experience, Rijndael is the place to end your study,not start it, as it is one of the most current and IMHO complex ciphers around.. :D All the best! CBC (cipher block chaining) is especially suitable for encrypting files where the security is increased over ECB significantly.

        The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
Re: same encrypt word ?
by Loops (Curate) on Oct 30, 2014 at 10:56 UTC

    Hi and welcome to the monastery

    It seems that the algorithm includes a random salt so that every run produces a different result. Is that what you're seeing? Does the Php version return the same result every time? Could you maybe give example input and output you're using?

      hi, yes i can make an example of result

      with the php function :
      key : test-math
      encrypted text :
      c2dbe4b6fec504f3249e2866dacc2a000964136de054865d407321433c001f98
      decrypted text: test-math

      and it's the same result every time, i don't give another argument on the php function.
      I can decrypt the text with all online tool, if i choose the RIJNDAEL_256 and ECB mode.
      I have no idea to have the same result with perl function

        Okay, this algorithm requires both the key and the data to be in blocks of 16. So you may be running into differences in the way they're being automatically padded for you.

        If you set both the plain text and the key to "1234567890123456", you'll see it's encrypted as: "757ccd0cdc5c90eadbeeecf638dd0000". You can verify this yourself by using this online tool.

        use Crypt::Rijndael; my $plaintext = my $key = '1234567890123456'; my $cipher = Crypt::Rijndael->new( $key, Crypt::Rijndael::MODE_ECB ); my $encrypted = $cipher->encrypt($plaintext); printf "%02x", ord $_ for split //, $encrypted;
        Prints: 757ccd0cdc5c90eadbeeecf638dd0000. So hopefully that's what you're getting with your PHP as well.

        Update: Ah, I see that all the above was done using 128-bit ECB, and you're using 256-bit.. hmmm.

        By the way, Crypt::Rijndael says in no uncertain terms that ECB should be avoided