title: Rijndael/AES decryption C# to PHP conversion
body: <p>I have the following code in C#</p>
  
  <pre><code>string s = "hellowld";
  byte[] bytes = new UnicodeEncoding().GetBytes(s);
  FileStream stream = new FileStream(inputFile, FileMode.Open);
  RijndaelManaged managed = new RijndaelManaged();
  CryptoStream stream2 = new CryptoStream(stream, managed.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
  FileStream stream3 = new FileStream(outputFile, FileMode.Create);
  try
  {
      int num;
      while ((num = stream2.ReadByte()) != -1)
      {
          stream3.WriteByte((byte) num);
      }
   [....]
  </code></pre>
  
  <p>This code snippet decrypts a certain file and outputs a decrypted version. In the CreateDecryptor method from RijndaelManaged im using the password as KEY and also as IV.</p>
  
  <p>I found some code here on stackoverflow for PHP but if I try to give the key and iv the same Array of Bytes like in C# nothing happens.</p>
  
  <pre><code>$Pass = "hellowld";
  $Clear = file_get_contents('./file.dat', FILE_USE_INCLUDE_PATH);
  
  $bytePass=array();
  $i = 0;
  foreach (str_split($Pass) as $value) {
      $bytePass[$i]=ord($value);
      $i++;
  }
  
  echo decryptAES($Clear,$bytePass,$bytePass);
  
  function decryptAES($content,$iv, $key,$aes) {
  
  // Setzt den Algorithmus
  switch ($aes) {
      case 128:
         $rijndael = 'rijndael-128';
         break;
      case 192:
         $rijndael = 'rijndael-192';
         break;
      default:
         $rijndael = 'rijndael-256';
  }
  
  // Setzt den Verschlüsselungsalgorithmus
  // und setzt den Output Feedback (OFB) Modus
  $cp = mcrypt_module_open($rijndael, '', 'ofb', '');
  
   // Ermittelt die Anzahl der Bits, welche die Schlüssellänge des Keys festlegen
  $ks = mcrypt_enc_get_key_size($cp);
  
  // Erstellt den Schlüssel, der für die Verschlüsselung genutzt wird
  $key = substr(md5($key), 0, $ks);
  
  // Initialisiert die Verschlüsselung
  mcrypt_generic_init($cp, $key, $iv);
  
  // Entschlüsselt die Daten
  $decrypted = mdecrypt_generic($cp, $content);
  
  // Beendet die Verschlüsselung
  mcrypt_generic_deinit($cp);
  
  // Schließt das Modul
  mcrypt_module_close($cp);
  
  return trim($decrypted);
  
  }
  </code></pre>
  
  <p>I really need some help how to properly create the code in PHP. Its not necessary that I output a file in PHP, a string would be sufficient.</p>
  
  <p><strong>UPDATE</strong>:
  The default C# RijndaelManaged cipher method is AES-128-CBC. I changed my PHP code to that mcrypt module (<a href="https://stackoverflow.com/questions/2044517/how-to-decode-rijndael-in-ruby-encoded-in-vb-net">default C# cipher method</a>)</p>
  
  <p><strong>UPDATE 2:</strong>
  I did manage to create a Java Decryptor which lead me to another thing. PHP has to use the PKCS7 Padding.</p>