title: 32-character PHP AES Key for mcrypt_encrypt
body: <p>Consider the following PHP code:</p>
  
  <pre><code>&lt;?php
  $key = "1234567812345678";
  $iv = "1234567812345678";
  $data = "Test string";
  
  $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
                              $key,
                              $data,
                              MCRYPT_MODE_CBC,
                              $iv);
  
  print "Encoded1: " . base64_encode($encrypted) . "\n";
  
  $key = "12345678123456781234567812345678";
  
  $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
                              $key,
                              $data,
                              MCRYPT_MODE_CBC,
                              $iv);
  
  print "Encoded2: " . base64_encode($encrypted) . "\n";
  </code></pre>
  
  <p>When run, this produces the output:</p>
  
  <pre><code>Encoded1: iz1qFlQJfs6Ycp+gcc2z4w==
  Encoded2: n3D26h/m8CSH0CE+z6okkw==
  </code></pre>
  
  <p>Note that I stole the first bit of code from <a href="https://stackoverflow.com/questions/10842509/php-java-aes-cbc-encryption-different-results">PHP Java AES CBC Encryption Different Results</a></p>
  
  <p>Now - here's the question:</p>
  
  <p>In the first case, the key that was passed in was a string of 16 characters.  If each of the individual characters was interpreted as an 8-bit quantity, this gives the 128-bit key size that one would expect.  Indeed, the Java code that's on the StackOverflow page that I referenced above does exactly that, and obtains the same result as the PHP.</p>
  
  <p>In the second call to <code>mcrypt_encrypt</code> above, I have doubled the length of the key.  <code>mcrypt_encrypt</code> accepts this happily, but produces a different encrypted output than in the first case.  Clearly, therefore, it considers this a different key - it does not, for example take only the first 128 bits and discard any past that.</p>
  
  <p>So, how does <code>mcrypt_encrypt</code> process the input key string to come up with the 128-bit key that the <code>MCRYPT_RIJNDAEL_128</code> algorithm requires?  </p>
  
  <p>If it makes any difference, the case I'm specifically interested in is when a 32-character string is passed in like my second example - I have to create a matching decryption routine (in Java), so I need to figure out how the key is actually generated in this case.  The page I cited has perfectly-good Java code (which works with all my test cases) - I'm just missing the proper set of key bytes.</p>