title: Encryption/Decryption equivalents in Java for C# constructs
body: <p>I have an encrypted mp4 using Rijndael and I am decrypting in C# in the following manner.</p>
  
  <pre><code>System.Security.Cryptography.Rijndael crypt = System.Security.Cryptography.Rijndael.Create();
  
  crypt.Key = convertedSecureString;
  
  byte[] initializationVectorLength = new byte[sizeof(int)];
  CryptoStream cryptostream = new CryptoStream(inputStream, crypt.CreateDecryptor(), CryptoStreamMode.Read);
  byte[] buffer = new byte[1024];
  int len;
  while ((len = cryptostream.Read(buffer, 0, buffer.Length)) &gt; 0)
  {
      outputStream.Write(buffer, 0, len);
      buffer = new byte[1024];
  }
  
  outputStream.Flush();
  cryptostream.Close();
  inputStream.Close();
  </code></pre>
  
  <p>Now I need to convert this code to Java/Android equivalent. I am not sure where to start frankly. I am confused by so many options - some say use Bouncy Castle, some say Apache Commons, some the native Java lib. How do I do this. And what do I do about CryptoStream etc?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I am using the following the code in C# for assigning the key</p>
  
  <pre><code>byte[] convertedSecureString = new byte[this.key.Length];
  IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.key);
  
  for (int i = 0, j = 0; i &lt; this.key.Length * UnicodeByteLength; i = i + UnicodeByteLength, j++)
  {
      convertedSecureString[j] = System.Runtime.InteropServices.Marshal.ReadByte(ptr, i);
  }
  
  try
  {
      crypt.Key = convertedSecureString;
  }
  </code></pre>
  
  <p>where key is secure. I have the equivalent unsecure key in Java. How do i convert this piece of code to Java</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>  Rfc2898DeriveBytes newKey = new Rfc2898DeriveBytes(crypt.Key.ToString(), crypt.IV);
                  Array.Copy(newKey.GetBytes((int)crypt.KeySize / 8), crypt.Key, (int)crypt.KeySize / 8);
                  Array.Copy(newKey.GetBytes((int)crypt.BlockSize / 8), crypt.IV, (int)crypt.BlockSize / 8);
  </code></pre>
  
  <p>I am using this in C# to mod the key using Rfc 2898 and derive the bytes - I cant find an equivalent in Java - I found here <a href="https://stackoverflow.com/questions/1012363/java-equivalent-of-cs-rfc2898derivedbytes">Java equivalent of C#&#39;s Rfc2898DerivedBytes</a> in the second comment - but what values do I give for iterator and dklen? </p>