示例#1
0
        private async void Decypher_Click(object sender, RoutedEventArgs e)
        {
            string errors = "";

            if (inputPath == "")
            {
                errors += "No input file specified\n";
            }

            if (outputPath == "")
            {
                errors += "No output file specified\n";
            }

            if (loggedUser == "")
            {
                errors += "No logged user\n";
            }

            if (errors != "")
            {
                MessageBox.Show(errors, "Error", MessageBoxButton.OK);
                return;
            }

            string tempFile = System.IO.Path.GetTempFileName();

            using (var input = File.OpenRead(inputPath))
            {
                using (var reader = XmlReader.Create(input))
                {
                    aes = FilesManager.FromXml(reader, aes);

                    using (var output = File.OpenWrite(tempFile))
                    {
                        input.Position = (reader as IXmlLineInfo).LinePosition + "</Header>".Length;//Set proper position beacues xmlReader loads to much

                        Task copyTask = input.CopyToAsync(output);

                        //new Thread(() => StartUpdatingprogress(source, copyTask)).Start();
                        long length = input.Length;
                        do
                        {
                            double position = (double)input.Position;
                            pbStatus.Value = (position / length) * 100;
                        }while (!copyTask.IsCompleted);
                        pbStatus.Value = 100;

                        copyTask.Wait();
                    }
                }
            }

            using (var input = File.OpenRead(tempFile))
            {
                using (var decryptedStream = aes.DecrypteStream(input, loggedUser, LogInPassword.Text))
                {
                    if (decryptedStream == null)
                    {
                        return;
                    }
                    using (var output = File.OpenWrite(outputPath))
                    {
                        Task copyTask = decryptedStream.CopyToAsync(output);

                        long length = input.Length;
                        do
                        {
                            double position = (double)input.Position;
                            pbStatus.Value = (position / length) * 100;
                        }while (!copyTask.IsCompleted);
                        pbStatus.Value = 100;

                        await copyTask;
                    }
                }
            }
        }
示例#2
0
        private async void Cypher_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string errors = "";
                if (inputPath == "")
                {
                    errors += "No input file specified\n";
                }

                if (outputPath == "")
                {
                    errors += "No output file specified\n";
                }

                if (aes.cipherMode == 0)
                {
                    errors += "No cipher mode specified\n";
                }

                if (pendingRecipents == null || pendingRecipents.Count == 0)
                {
                    errors += "No Recipient was assigned\n";
                }

                if (errors != "")
                {
                    MessageBox.Show(errors, "Error", MessageBoxButton.OK);
                    return;
                }

                //INITIALIZE

                FileInfo info = new FileInfo(Path.GetTempFileName());
                int      randomValue;

                byte[] buffer = new byte[4];
                random.NextBytes(buffer);
                randomValue = BitConverter.ToInt32(buffer, 0);

                string basePassword = String.Format("{0}{1}", DateTime.Now, randomValue);

                byte[] salt1 = new byte[8];
                using (RNGCryptoServiceProvider rngCsp = new
                                                         RNGCryptoServiceProvider())
                {
                    // Fill the array with a random value.
                    rngCsp.GetBytes(salt1);
                }
                const int Iterations   = 400;
                var       keyGenerator = new Rfc2898DeriveBytes(basePassword, salt1, Iterations);
                aes.key = keyGenerator.GetBytes(aes.keySize);

                keyGenerator = new Rfc2898DeriveBytes(basePassword.Reverse().ToString(), salt1, Iterations);
                buffer       = new byte[16];
                random.NextBytes(buffer);
                aes.iV = buffer;

                // END OF INITIALIZE

                foreach (string nickname in pendingRecipents)
                {
                    aes.addUser(nickname);
                }



                using (var output = File.Open(outputPath, FileMode.Create))
                {
                    using (var xmlOutput = XmlWriter.Create(output))
                    {
                        xmlOutput.WriteStartDocument();

                        FilesManager.WriteToXml(xmlOutput, aes);

                        xmlOutput.Flush();

                        xmlOutput.WriteEndDocument();
                    }
                    using (var source = File.Open(inputPath, FileMode.Open))
                    {
                        using (var sourceEncypted = aes.EncrypteStream(source))
                        {
                            Task copyTask = sourceEncypted.CopyToAsync(output);

                            //new Thread(() => StartUpdatingprogress(source, copyTask)).Start();
                            long length = source.Length;
                            do
                            {
                                double position = (double)source.Position;
                                pbStatus.Value = (position / length) * 100;
                            }while (!copyTask.IsCompleted);
                            pbStatus.Value = 100;

                            await copyTask;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
            }
        }