1

我遇到了 CryptoStream 解密器的问题。我正在解密另一个线程上的文件。所以这里是问题:

当我让文件解密到最后时,一切都很好,但是当我停止线程,使用全局变量并退出 while 循环时

while( !stopThread && ( nBRead = csFile.Read(readbuf, 0, 8192)) != 0)

我在cryptostream Dispose().

我做错了什么??

这是我的代码:

            string ext; 
            //textBox1.Text = "";

            OpenFileDialog ofdFile = new OpenFileDialog();
            if (textMode)
                ofdFile.Title = "Please select text to decrypt.";
            else
                ofdFile.Title = "Please select file to decrypt.";

            OpenFileDialog ofdKey = new OpenFileDialog();
            ofdKey.Title = "Please select KEY.";


            // mora ovo ili invoke koga nema dialog :)
            originalContex.Send(delegate
            {
                dialogRes    = ofdFile.ShowDialog();
                dialogResKey = ofdKey.ShowDialog();
            }, null);


            if ( dialogRes == DialogResult.OK && dialogResKey == DialogResult.OK )
            {
                FileStream fsKey = new FileStream(ofdKey.FileName, FileMode.Open);
                byte[] iv = new byte[8];
                byte[] key = new byte[24];
                byte[] extB = new byte[20]; 
                fsKey.Read(iv, 0, 8);
                fsKey.Read(key, 0, 24);
                fsKey.Read(extB, 0, 20); 
                fsKey.Dispose();

                FileStream fsOpenedFile = new FileStream(ofdFile.FileName, FileMode.Open, FileAccess.Read);
                CryptoStream csFile = new CryptoStream(fsOpenedFile, new TripleDESCryptoServiceProvider().CreateDecryptor(key, iv), CryptoStreamMode.Read);


                if (textMode)
                {
                    int readbuf;
                    List<byte> lb = new List<byte>();

                    while ((readbuf = csFile.ReadByte()) != -1)
                        lb.Add((byte)readbuf);

                    textBox1.Invoke( new MethodInvoker(() => { textBox1.Text = Encoding.UTF8.GetString(lb.ToArray()); }) );

                    prog.Invoke(new MethodInvoker(() => { prog.Value = 100; }));
                }                   
                else // filemode
                {
                    byte[] readbuf = new byte[8192];
                    ext = Encoding.UTF8.GetString(extB).Trim('\0'); 


                    string saveDir = Path.GetDirectoryName(ofdFile.FileName) + "\\" + Path.GetFileNameWithoutExtension(ofdFile.FileName) + "_DECRYPTED";
                    Directory.CreateDirectory( saveDir );
                    Directory.SetCurrentDirectory( saveDir );



                    FileStream fsDecrFile = new FileStream(Path.GetFileNameWithoutExtension(ofdFile.FileName)  + ext, FileMode.Create, FileAccess.Write);

                    FileInfo fi = new FileInfo(ofdFile.FileName);
                    long oneProc = fi.Length / 100;
                    int nBRead = 0;
                    long nBReadacc = 0;

                    while (   !stopThread  &&  ( nBRead = csFile.Read(readbuf, 0, 8192)) != 0  )
                    {
                        nBReadacc += nBRead;
                        fsDecrFile.Write(readbuf, 0, nBRead);

                        if (nBReadacc >= oneProc)
                        {
                            nBReadacc = 0;
                            prog.Invoke(new MethodInvoker(() => { prog.Value +=1; }));
                        }
                    }

                    try
                    {
                        csFile.Dispose();
                    }
                    catch (CryptographicException e)
                    {                        
                        MessageBox.Show(e.Message);
                    }

                   // MessageBox.Show(nBReadacc.ToString());

                    fsDecrFile.Flush();
                    fsDecrFile.Dispose();   

                    prog.Invoke(new MethodInvoker(() => { prog.Value = 100; }));                   
                }



                fsOpenedFile.Dispose(); 
                // csFile.CopyTo



            }

            ofdFile.Dispose();
            ofdKey.Dispose();            
        }
4

0 回答 0