title: Can I replicate the exact behaviour of PHP's AES encryption in ruby?
body: <p>I'm in the process of rebuilding a PHP web app in Ruby on Rails, and would dearly love to avoid forcing all existing users to reset their encrypted passwords. The PHP site uses <a href="http://php.net/manual/en/function.mcrypt-encrypt.php" rel="nofollow noreferrer">mcrypt_encrypt</a> with AES-256-ECB, and I can't for the life of me get the same cipher text using ruby's <a href="http://www.ruby-doc.org/stdlib/libdoc/openssl/rdoc/index.html" rel="nofollow noreferrer">OpenSSL</a>. I can't decrypt them either (which is good in principle) since what's actually stored in the user DB is an MD5 hash of the AES cipher text.</p>
  
  <p>I've read these previous, closely related questions and the very helpful answers:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1862710/how-to-make-ruby-aes-256-cbc-and-php-mcrypt-rijndael-128-play-well-together">How to make Ruby AES-256-CBC and PHP MCRYPT_RIJNDAEL_128 play well together</a></li>
  <li><a href="https://stackoverflow.com/questions/1864700/part-ii-how-to-make-ruby-aes-256-cbc-and-php-mcrypt-rijndael-128-play-well-toget">Part II: How to make Ruby AES-256-CBC and PHP MCRYPT_RIJNDAEL_128 play well together</a></li>
  </ul>
  
  <p>including the pages referenced there, and if I understand correctly, the PHP and ruby implementations use different padding methods. Since I have to live with how things work on the PHP side, is there any way to force the same padding method on ruby/OpenSSL somehow? I'm using ruby 1.9.2-p180.</p>
  
  <p>Here's the sample code in PHP:</p>
  
  <pre><code>$salt = "12345678901234567890123456789012";
  $plain = "password";
  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $plain, MCRYPT_MODE_ECB, $iv);
  
  echo md5($cipher);
  </code></pre>
  
  <p>Output: <code>6337137fd88148250fd135a43dbeb84a</code></p>
  
  <p>and in ruby:</p>
  
  <pre><code>require 'openssl'
  
  salt = "12345678901234567890123456789012"
  plain = "password";
  
  c = OpenSSL::Cipher.new("AES-256-ECB")
  c.encrypt
  c.key = salt
  cipher = c.update(plain)
  cipher &lt;&lt; c.final
  
  puts Digest::MD5.hexdigest(cipher)
  </code></pre>
  
  <p>Output: <code>18dee36145c07ab83452aefe2590c391</code></p>