title: Encrypting in PHP (mcrypt), Decrypting in Ruby (OpenSSL::Cipher)
body: <p>I'm working on a cross language project wrapping a ruby/Sinatra API in PHP to be consumed by another team. None of the information exposed by the API is sensitive, but we would prefer it not be easily accessible to a casual observer guessing the URL. </p>
  
  <pre><code>    private function generateSliceIDToken($key){
      $currentEpoch = time();
      $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
      $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
      $encryptedBytes = mcrypt_encrypt(
          MCRYPT_RIJNDAEL_128,
          $key,
          $currentEpoch.**Passcode**,
          MCRYPT_MODE_CBC, $iv
      );
      $ivAndEncryptedBytes = $iv . $encryptedBytes;
  
      return urlencode(urlencode(base64_encode($ivAndEncryptedBytes)));
  </code></pre>
  
  <p>The code above Encrypts a password and time stamp using mcrypt's RIJNDAEL implementation and encodes it to send off to the ruby API </p>
  
  <pre><code>if identifier.validate_token Base64.decode64(URI.unescape( URI.unescape(params[:token])))
  </code></pre>
  
  <p>Sinatra grabs it and decodes it</p>
  
  <pre><code>def validate_token(token)
    cipher = OpenSSL::Cipher::AES.new(128, 'CBC')
    cipher.decrypt
    cipher.key = **key**
    cipher.iv = token[0,16]
  
    plain = cipher.update(token[16..-1]) + cipher.final
    return plain[10,8] == **Passcode**
  end
  </code></pre>
  
  <p>and passes it along to be decrypted</p>
  
  <p>The problem is, the decryption fails with a 'Bad Decrypt' Error </p>
  
  <p>I was lead to believe Mcrypt's RIJNDAEL and Cipher's AES were compatible, but is this assumption incorrect? Any help I can get one this would be most helpful. </p>