title: Cross platform (php to C# .NET) encryption/decryption with Rijndael
body: <p>I'm currently having a bit of problem with decrypting a message encrypted by php mcrypt.
  The php code is as following:</p>
  
  <pre><code>&lt;?php
    //$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
    $iv = "45287112549354892144548565456541";
    $key = "anjueolkdiwpoida";
    $text = "This is my encrypted message";
    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
    $crypttext = urlencode($crypttext);
    $crypttext64=base64_encode($crypttext);
    print($crypttext64) . "\n&lt;br/&gt;";
  ?&gt;
  </code></pre>
  
  <p>The the encrypted message is then sent to a ASP.NET platform (C#). However, I'm having problem retaining the order of decryption (base64 decode to urldecode). The code I had in ASP.NET is as the following (iv and key is the same as in php):</p>
  
  <pre><code>public string Decode(string str)
  {
      byte[] decbuff = Convert.FromBase64String(str);
      return System.Text.Encoding.UTF8.GetString(decbuff);
  }
  
  static public String DecryptRJ256(string cypher, string KeyString, string IVString)
  {
  
      string sRet = "";
      RijndaelManaged rj = new RijndaelManaged();
      UTF8Encoding encoding = new UTF8Encoding();
  
  
      try
      {
          //byte[] message = Convert.FromBase64String(cypher);
          byte[] message = encoding.GetBytes(cypher);
  
          byte[] Key = encoding.GetBytes(KeyString);
          byte[] IV = encoding.GetBytes(IVString);
  
          rj.Padding = PaddingMode.Zeros;
          rj.Mode = CipherMode.CBC;
          rj.KeySize = 256;
          rj.BlockSize = 256;
          rj.Key = Key;
          rj.IV = IV;
          MemoryStream ms = new MemoryStream(message);
  
          using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
          {
              using (StreamReader sr = new StreamReader(cs))
              {
                  sRet = sr.ReadToEnd();
              }
          }
  
      }
      finally
      {
          rj.Clear();
      }
  
      return sRet;
  
  
  }
  
  string temp = DecryptRJ256(Server.UrlDecode(Decode(cypher)), keyString, ivString);
  </code></pre>
  
  <p>The problem I'm having is that after I recieved the encrypted message from php, I converted it into byte[] and then converted back to UTF8 encoded string so I can urldecode it. then I feed the result into the function where I converted the string back to byte[] and ran it through the decryption process. However, I can't get the desired result...any ideas?</p>
  
  <p>Thanks in advance.</p>