Error message wrote:System.IO.IOException: An I/O-error occurred,
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at f_in_box__lib.StreamBasedOnIStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Security.Cryptography.CryptoStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at Pausit.FromStreamToStreamWriter.Main()
My guess is that the requested file has not been decrypted enough when my original stream tries to read more, but that’s just my guess.
My question is if there’s any way I can make sure that the crypto stream has buffered up enough before it gets read by the original stream. Or even better if it can decrypt the whole movie before it gets read.
Any help appreciated since I’m a bit stuck on this.
I’m still not all that comfortable when working with streams so please give me descriptive suggestions if you can.
Patrik
My code looks like this:
Code: Select all
private void OnLoadExternalResourceByFullPath(object sender, String URL, System.IO.Stream Stream, ref bool Handled)
{
string tempString = Application.StartupPath + "\\VideoData\" + URL.Substring(11);
System.IO.FileStream FLVStream = System.IO.File.OpenRead(tempString);
FromStreamToStreamWriter writer = new FromStreamToStreamWriter(FLVStream, Stream);
Handled = true;
}
Code: Select all
class FromStreamToStreamWriter
{
internal System.IO.FileStream FromStream;
internal System.IO.Stream ToStream;
internal System.Threading.Thread Thread;
public FromStreamToStreamWriter(System.IO.FileStream FromStream, System.IO.Stream ToStream)
{
this.FromStream = FromStream;
this.ToStream = ToStream;
Thread = new System.Threading.Thread(new System.Threading.ThreadStart(Main));
Thread.Start();
}
private void Main()
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes("Password", new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(ToStream, alg.CreateDecryptor(), CryptoStreamMode.Write);
const int size = 64 * 1024;
byte[] buffer = new byte[size];
int ReadBytes;
while ((ReadBytes = FromStream.Read(buffer, 0, size)) > 0)
{
try
{
cs.Write(buffer, 0, ReadBytes);
}
catch (System.IO.IOException e)
{
MessageBox.Show(e.ToString());
break;
}
}
cs.Close();
FromStream.Close();
}
}