"emulating" MySQL aes_encrypt() in PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordan

    "emulating" MySQL aes_encrypt() in PHP

    mysql> select aes_encrypt('go rdan', 'abc');
    +------------------------------+
    | aes_encrypt('go rdan', 'abc') |
    +------------------------------+
    | p§Èb9??_é?0ö?LI Ý |
    +------------------------------+

    I would like to "emulate" that function in PHP. I know that MySQL AES algorithm
    is RIJNDAEL_128 so I tried the following code
    <?php
    $iv_size = mcrypt_get_iv_s ize(MCRYPT_RIJN DAEL_128, MCRYPT_MODE_CBC );
    $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
    echo mcrypt_ecb (MCRYPT_RIJNDAE L_128, 'abc', 'gordan', MCRYPT_MODE_CBC ,
    $iv);
    ?>

    but the encrypted string is different from the MySQL one :-(
    and ideas?

    thanks,
    gordan


  • Colin McKinnon

    #2
    Re: &quot;emulating &quot; MySQL aes_encrypt() in PHP

    Gordan wrote:
    [color=blue]
    > mysql> select aes_encrypt('go rdan', 'abc');
    > +------------------------------+
    > | aes_encrypt('go rdan', 'abc') |
    > +------------------------------+
    > | p§Èb9??_é?0ö?LI Ý |
    > +------------------------------+
    >
    > I would like to "emulate" that function in PHP. I know that MySQL AES
    > algorithm is RIJNDAEL_128 so I tried the following code
    > <?php
    > $iv_size = mcrypt_get_iv_s ize(MCRYPT_RIJN DAEL_128, MCRYPT_MODE_CBC );
    > $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
    > echo mcrypt_ecb (MCRYPT_RIJNDAE L_128, 'abc', 'gordan',
    > MCRYPT_MODE_CBC ,
    > $iv);
    > ?>
    >
    > but the encrypted string is different from the MySQL one :-(
    > and ideas?[/color]

    My understanding is that the initialization vector is added to the payload
    before encryption - so I've never quite understood why the examples show:
    $iv = mcrypt_create_i v($iv_size, MCRYPT_RAND);
    So it might be worth trying a null iv.

    Other considerations include:
    1) how is the key padded to the right length (16 chars)
    2) maybe the data is the same - it's just being represented differently
    (since it seems to be stored by MySQL in binary format)

    HTH

    C.

    Comment

    Working...