void DecyrptAsyncBackgroundWorker(object sender, DoWorkEventArgs e)
        {
            DataForDec data = (DataForDec)e.Argument;

            byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            FileStream fsCrypt  = new FileStream(data.InputFile, FileMode.Open);
            Int32      position = 0;

            using (BinaryReader reader = new BinaryReader(fsCrypt))
            {
                byte prevByte = 0;
                byte readByte;
                int  delimeterSignsCounter = 0;

                while (true)
                {
                    readByte = reader.ReadByte();
                    position++;
                    if (readByte == 0x3d && prevByte == 0x3d)
                    {
                        delimeterSignsCounter++;
                        if (delimeterSignsCounter >= 52)
                        {
                            fsCrypt.Seek(2, SeekOrigin.Current);
                            break;
                        }
                    }
                    else
                    {
                        delimeterSignsCounter = 0;
                    }

                    prevByte = readByte;
                }

                RijndaelManaged AES = new RijndaelManaged();

                AES.KeySize   = data.KeySize;
                AES.BlockSize = data.BlockSize;

                if (data.AES_KeyBytes == null)
                {
                    throw new Exception("Key bytes are NULL!!!");
                }

                var key = new Rfc2898DeriveBytes(data.AES_KeyBytes, saltBytes, 1000);
                AES.Key     = key.GetBytes(AES.KeySize / 8);
                AES.IV      = key.GetBytes(AES.BlockSize / 8);
                AES.Padding = PaddingMode.Zeros;

                AES.Mode = data.CipherMode != CipherMode.OFB ? data.CipherMode : CipherMode.CBC;

                CryptoStream cs = new CryptoStream(fsCrypt,
                                                   AES.CreateDecryptor(),
                                                   CryptoStreamMode.Read);

                FileStream fsOut = new FileStream(data.OutputFile, FileMode.Create);

                int  encryptedData;
                long lenStream = fsCrypt.Length;

                int prevVal = 0;

                sw.Start();

                for (long i = 0; (encryptedData = cs.ReadByte()) != -1; i++)
                {
                    fsOut.WriteByte((byte)encryptedData);
                    if (prevVal != unchecked ((int)(i * 100 / lenStream)))
                    {
                        prevVal = unchecked ((int)(i * 100 / lenStream));
                        backgroundWorker.ReportProgress(prevVal);
                    }
                }
                backgroundWorker.ReportProgress(100);


                fsOut.Close();
                cs.Close();
                fsCrypt.Close();
            }
        }
Exemplo n.º 2
0
        public DataForDec ReadXml()
        {
            DataForDec dataForDec = new DataForDec();

            string xmlString = "";

            try {
                using (FileStream fs = new FileStream(data.InputFile, FileMode.Open, FileAccess.Read))
                    using (StreamReader sw = new StreamReader(fs))
                    {
                        string line = "";
                        while (true)
                        {
                            line = sw.ReadLine();

                            if (line.Equals(delimiter))
                            {
                                break;
                            }
                            xmlString += line;
                        }
                    }
            }
            catch {
                MessageBox.Show("Can not open file input file to decrypted!");
                return(dataForDec);
            }

            using (XmlReader reader
                       = XmlReader.Create(new StringReader(xmlString)))
            {
                reader.ReadToFollowing(XmlConstants.KEY_SIZE);
                reader.MoveToFirstAttribute();
                dataForDec.KeySize = int.Parse(reader.Value);

                reader.ReadToFollowing(XmlConstants.BLOCK_SIZE);
                reader.MoveToFirstAttribute();
                dataForDec.BlockSize = int.Parse(reader.Value);

                reader.ReadToFollowing(XmlConstants.CIPHER_MODE);
                reader.MoveToFirstAttribute();
                dataForDec.StringCipherMode = reader.Value;

                reader.ReadToFollowing(XmlConstants.FILE_EXTENSION);
                reader.MoveToFirstAttribute();
                dataForDec.FileExtension = reader.Value;

                string user      = "******";
                string sesionKey = "...";
                while (true)
                {
                    try
                    {
                        reader.ReadToFollowing(XmlConstants.USER);
                        reader.MoveToFirstAttribute();
                        user = reader.Value;

                        reader.ReadToFollowing(XmlConstants.SESSION_KEY);
                        reader.MoveToFirstAttribute();
                        sesionKey = reader.Value;

                        if ("".Equals(user) || "".Equals(sesionKey))
                        {
                            break;
                        }

                        dataForDec.UsersCollection.Add(user, new UserData()
                        {
                            EncSesKey = Convert.FromBase64String(sesionKey)
                        });
                    }
                    catch {
                        break;
                    }
                }
            }

            return(dataForDec);
        }