title: Figuring out the exact key created by PHP's mcrypt
body: <p>A PHP application I'm maintaining uses Rijndael_256 with EBC_MODE encryption with mcrypt. Fun has it that the key isn't 256 bits long, but only 160. According to the <a href="http://www.php.net/manual/en/function.mcrypt-encrypt.php" rel="noreferrer"><code>mcrypt_encrypt</code></a> documentation the key is padded with \0 to get the required size if it's too small.</p>
  
  <blockquote>
    <p>The key with which the data will be encrypted. If it's smaller than
    the required keysize, it is padded with '\0'. It is better not to use
    ASCII strings for keys.</p>
  </blockquote>
  
  <p>This seem to happen at around the start of <a href="https://github.com/php/php-src/blob/master/ext/mcrypt/mcrypt.c#L1186" rel="noreferrer">line 1186 in mcrypt.c</a> and modifying the key at line 1213.</p>
  
  <p>So lets say we've got <code>$key = 'abcdefghijkm';</code> which is too short, but PHP's implementation of mcrypt makes sure it's extended to 32 characters (or 256 bit) when using RIJNDAEL_256. What will the final key look like?</p>
  
  <p>I'm asking this because another application is being built that uses the same encrypted data, but is in another language. Perl to be exact and I'm using <a href="https://metacpan.org/module/Crypt::Rijndael" rel="noreferrer"><code>Crypto::Rijndael</code></a>. For the given example key, what is the exact key I would have to feed to <code>Crypto::Rijndael</code> (or any other for that matter) to be able to decrypt the data again?</p>
  
  <p><strong>Update</strong></p>
  
  <p>With Perl I can generate a key that's \0 padded doing <code>pack('a32', 'my secret key');</code> (or <code>Z32</code>), <code>length()</code> will report 32 and the <code>Crypt::Rijndael</code> module accepts the key. Looking at the source of PHP's mcrypt this should be the key that's being generated (\0 padded), but it simply won't take it.</p>
  
  <p>In theory in PHP <code>pack('a32', 'my secret key');</code> should result in the same \0 padded key that PHP's mcrypt generates, but this isn't the case.</p>
  
  <p>I'm very close to just encrypt everything again but with a new key. This is taking too much time.</p>