title: MCrypt rijndael-128 to OpenSSL aes-128-ecb conversion
body: <p>Since Mcrypt is deprecated, I want to use OpenSSL instead in my code since we already using <strong>php 7.0.17</strong> in our server and there's no tell when they upgrade it.</p>
  
  <p>Some third party API (hosted on <strong>PHP 5.x</strong> probably and using <strong>mcrypt</strong>), is taking encrypted data. They've provided methods which they are using to encrypt/decrypt strings.</p>
  
  <p>Here are they</p>
  
  <pre><code>$secret = 'a0a7e7997b6d5fcd55f4b5c32611b87c' ;
  
  public function encrypt128($str)
      {
          $block = mcrypt_get_block_size("rijndael_128", "ecb");
          $pad   = $block - (strlen($str) % $block);
          $str .= str_repeat(chr($pad), $pad);
  
          return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret, $str, MCRYPT_MODE_ECB));
      }
  
  public function decrypt128($str)
      {
          $str = base64_decode($str);
          $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secret, $str, MCRYPT_MODE_ECB);
  
          $len = strlen($str);
          $pad = ord($str[$len - 1]);
  
          return substr($str, 0, strlen($str) - $pad);
      }
  </code></pre>
  
  <p>using these methods string <code>small1</code> if encrypted becomes <code>v7IXp5vVaFVXXlt/MN8BVw==</code></p>
  
  <hr>
  
  <p>We want to use <code>openssl_encrypt</code> in our side such that if we encrypt same string with OpenSSL it must give same results as Mcrypt. I've researched that mcrypt using <strong>rijndael-128</strong> Mode <strong>ecb</strong> should be compatible with OpenSSL <code>aes-128-ecb</code>.</p>
  
  <p>For last few hours, I've been trying to make my own method to encrypt strings serving same result by using OpenSSL. So far I've come to this</p>
  
  <pre><code>public function sslEncrypt128($str)
  {
      $secret = 'a0a7e7997b6d5fcd55f4b5c32611b87c';
      return base64_encode(openssl_encrypt($str, 'aes-128-ecb', $secret, OPENSSL_RAW_DATA));
  }
  </code></pre>
  
  <p>But it produces different string <code>SxJ3+EdaeItZx3/EwGTUbw==</code> for same as above input. I don't know if it is flag's problem or padding's, any pointers will be welcome.</p>
  
  <p>I've added the code here to test online <a href="https://3v4l.org/v2J2N" rel="noreferrer">https://3v4l.org/v2J2N</a></p>
  
  <p>Thanks in advance.</p>