title: Decrypt C# RIJNDAEL encoded text in PHP
body: <p>I need to decrypt a string as part of an authorization process.</p>
  
  <p>The documentation specifies that the authorization string was encrypted with the following settings:</p>
  
  <ul>
  <li>Padding of input data: <code>PKCS*7</code></li>
  <li>Password byte array is 32 bytes long. Password string is converted to UTF-16 encoded byte array and byte array is then padded with zeroes up to length of 32 bytes. Longer passwords are truncated.</li>
  </ul>
  
  <p>The C# example:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Decrypts a string.
      /// &lt;/summary&gt;
      /// &lt;param name="content"&gt;The string to decrypt.&lt;/param&gt;
      /// &lt;param name="password"&gt;The password to use.&lt;/param&gt;
      /// &lt;returns&gt;The decrypted string.&lt;/returns&gt;
      private static string DecryptString(string content, string password)
      {
          Rijndael aes;
          byte[] retVal = null;
          byte[] contentBytes;
          byte[] passwordBytes;
          byte[] ivBytes;
  
          try
          {
              contentBytes = Convert.FromBase64String(content);
  
              //Create the password and initial vector bytes
              passwordBytes = new byte[32];
              ivBytes = new byte[16];
              Array.Copy(Encoding.Unicode.GetBytes(password), passwordBytes, Encoding.Unicode.GetBytes(password).Length);
              Array.Copy(passwordBytes, ivBytes, 16);
  
              //Create the cryptograpy object
              using (aes = Rijndael.Create())
              {
                  aes.Key = passwordBytes;
                  aes.IV = ivBytes;
                  aes.Padding = PaddingMode.PKCS7;
  
                  //Decrypt
                  retVal = aes.CreateDecryptor().TransformFinalBlock(contentBytes, 0, contentBytes.Length);
              }
          }
          catch
          {
          }
  
          return Encoding.Unicode.GetString(retVal);
      }
  </code></pre>
  
  <p>The same function was discussed here, but for JAVA: <a href="https://stackoverflow.com/questions/25064529/decrypt-c-sharp-rijndael-encoded-text">Decrypt C# RIJNDAEL encoded text</a></p>
  
  <p>I tried to decrypt it with the following function but the result is different than expected:</p>
  
  <pre><code>function decrypt($string, $pass){
      $iv = substr($pass, 0, 16);
      $data =  mcrypt_decrypt(MCRYPT_RIJNDAEL_256,
                          $pass,
                          base64_decode($string),
                          MCRYPT_MODE_CBC,
                          $iv);
      $pad = ord($data[strlen($data) - 1]);
      return substr($data, 0, -$pad);
  }
  </code></pre>
  
  <p>The ecrypted string <code>"7iTdZnp0DtGnIfwwqY4W/glbLLVZ0+asVLAuz13PzrW0wM6HC7rNuQvcG8JDSehyYeBJARdXHgLo9hRL9sBz3fN5LJ8cro3o0kFnAao2YRU="</code></p>
  
  <p>should decrypt to </p>
  
  <pre><code>"ldYWMFlSbcki6LMl3rkNfGavnt8VqmZd" 
  </code></pre>
  
  <p>using the password <code>"GAT"</code></p>
  
  <p>I think it has something to do with the password / iv / encoding</p>