Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three problems with the code you are using:</p> <ol> <li><p>As others have mentioned, your PHP code is currently using <code>MCRYPT_RIJNDAEL_256</code> whereas, as documented under <a href="https://dev.mysql.com/doc/en/encryption-functions.html#function_aes-encrypt" rel="noreferrer"><code>AES_ENCRYPT()</code></a>:</p> <blockquote> <p>Encoding with a 128-bit key length is used, but you can extend it up to 256 bits by modifying the source. We chose 128 bits because it is much faster and it is secure enough for most purposes.</p> </blockquote></li> <li><p>As others have mentioned, you are applying <code>base64_encode()</code> to convert PHP's binary result to text, whereas the MySQL result appears merely to be a hexadecimal representation of its binary result. You can either use <a href="http://dev.mysql.com/doc/en/string-functions.html#function_to-base64" rel="noreferrer"><code>TO_BASE64()</code></a> in MySQL since v5.6.1 or else <a href="http://php.net/manual/en/function.bin2hex.php" rel="noreferrer"><code>bin2hex()</code></a> in PHP.</p></li> <li><p>As documented under <a href="http://php.net/manual/en/function.mcrypt-encrypt.php" rel="noreferrer"><code>mcrypt_encrypt()</code></a>:</p> <blockquote> <p>If the size of the data is not n * blocksize, the data will be padded with '<strong><em>\0</em></strong>'.</p> </blockquote> <p>Whereas MySQL uses <a href="http://en.wikipedia.org/wiki/Padding_%28cryptography%29#PKCS7" rel="noreferrer">PKCS7 padding</a>.</p></li> </ol> <p>Therefore, to obtain the same results in PHP as you currently show for MySQL:</p> <pre><code>&lt;?php class MySQL_Function { const PKCS7 = 1; private static function pad($string, $mode, $blocksize = 16) { $len = $blocksize - (strlen($string) % $blocksize); switch ($mode) { case self::PKCS7: $padding = str_repeat(chr($len), $len); break; default: throw new Exception(); } return $string.$padding; } public static function AES_ENCRYPT($str, $key_str) { return mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $key_str, self::pad($str, self::PKCS7), MCRYPT_MODE_ECB ); } } echo bin2hex(MySQL_Function::AES_encrypt( "Hello World", "password" )); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload