How to replicate MySQL's aes-256-cbc in PHP
<p>Using <a href="https://www.smashingmagazine.com/2012/05/replicating-mysql-aes-encryption-methods-with-php/" rel="nofollow">this</a> article as a guide I was able to successfully replicate MySQL's aes-128-ecb in PHP:</p>

<pre><code>final class Encryption
{

  // The key
  const KEY = '36F3D40A7A41A827968BE75A87D60950';

  /**
   * Encrypt a string
   *
   * @access public
   * @static
   * @param string $string
   * @return string
   */
  public static function encrypt($string)
  {
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, self::getMySQLKey(self::KEY), self::getPaddedString($string), MCRYPT_MODE_ECB);
  }

  /**
   * Decrypt a string
   *
   * @access public
   * @static
   * @param string $string
   * @return string
   */
  public static function decrypt($string)
  {
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, self::getMySQLKey(self::KEY), $string, MCRYPT_MODE_ECB), "\x00..\x10");
  }

  /**
   * Get MySQL key
   *
   * @access public
   * @static
   * @param string $key
   * @return string
   */
  public static function getMySQLKey($key)
  {

    // The new key
    $new_key = str_repeat(chr(0), 16);

    // Iterate over the key and XOR
    for ($i = 0, $l = strlen($key); $i &lt; $l; ++$i)
    {
      $new_key[$i % 16] = $new_key[$i % 16] ^ $key[$i];
    }

    // Return the new key
    return $new_key;

  }

  /**
   * Get padded string
   *
   * @access public
   * @static
   * @param string $string
   * @return string
   */
  public static function getPaddedString($string)
  {
    return str_pad($string, (16 * (floor(strlen($string) / 16) + 1)), chr(16 - (strlen($string) % 16)));
  }

}
</code></pre>

<p>As an example:</p>

<pre><code>// PHP, gives CJI+zJyviQI7GgSCLGMNsqsXq2MDKC3a9FIG3wDrE8Y=
base64_encode(Encryption::encrypt('michael@example.com'))

// MySQL, gives CJI+zJyviQI7GgSCLGMNsqsXq2MDKC3a9FIG3wDrE8Y=
SELECT TO_BASE64(AES_ENCRYPT('michael@example.com', '36F3D40A7A41A827968BE75A87D60950'));
</code></pre>

<p>However, I want to update to using aes-256-cbc but am having difficulties. I started by replacing <code>MCRYPT_RIJNDAEL_128</code> with <code>MCRYPT_RIJNDAEL_256</code> and <code>MCRYPT_MODE_ECB</code> with <code>MCRYPT_MODE_CBC</code> and used the <code>KEY</code> constant as the initialization vector:</p>

<pre><code>  /**
   * Encrypt a string
   *
   * @access public
   * @static
   * @param string $string
   * @return string
   */
  public static function encrypt($string)
  {
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, self::getMySQLKey(self::KEY), self::getPaddedString($string), MCRYPT_MODE_CBC, self::KEY);
  }

  /**
   * Decrypt a string
   *
   * @access public
   * @static
   * @param string $string
   * @return string
   */
  public static function decrypt($string)
  {
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, self::getMySQLKey(self::KEY), $string, MCRYPT_MODE_CBC, self::KEY), "\x00..\x10");
  }
</code></pre>

<p>The problem is that I'm now getting different values from PHP and MySQL:</p>

<pre><code>// PHP, gives XSRfnrl05CE7JIHCvfhq6D67O0mAW2ayrFv2YkjFVYI=
base64_encode(Encryption::encrypt('michael@example.com'))

// MySQL, gives lTLT4MRXcHnOAsYjlwUX4WVPHgYvyi6nKC4/3us/VF4=
SELECT TO_BASE64(AES_ENCRYPT('michael@example.com', '36F3D40A7A41A827968BE75A87D60950', '36F3D40A7A41A827968BE75A87D60950'));
</code></pre>

<p>I'm unsure where to proceed from here so any help would be appreciated.</p>

<p>And just to confirm that MySQL is indeed using the correct encryption method:</p>

<pre><code>// aes-256-cbc
SELECT @@session.block_encryption_mode
</code></pre>