Example #1
0
        public static void OdszyfrowywanieRc6(string wejscie, string wyjscie, Odbiorcy odbiorca, NaglowekPliku naglowek, MainWindow window)
        {
            //data
            var reader2 = new StreamReader(wejscie);
            var reader = new FileMy(reader2);
            while (true)
            {
                var searcher = reader.ReadLine();
                if (searcher != null && searcher.StartsWith("</EnctyptedFileHeader>"))
                    break;
            }
            var position = reader.Position;
            reader.Close();
            var entry = new FileStream(wejscie, FileMode.Open)
            {
                Position = position      
            };
            var exit = new System.IO.FileStream(wyjscie, FileMode.Create);



            //decrypt
            var blockCipher = CreateCipher(naglowek.Tryb, naglowek.WielkoscPodBloku);
            ICipherParameters parameters;
            if (naglowek.Tryb == "ECB")
                parameters = new KeyParameter(odbiorca.KluczSesyjny);
            else
                parameters = new ParametersWithIV(new KeyParameter(odbiorca.KluczSesyjny), naglowek.Iv);
            blockCipher.Init(false, parameters);

            //date for progresbar
            long lenghtFile = entry.Length;
            double progres = lenghtFile / 1000;
            long actuallyLenght = 0;

            //date for encrypt
            var buffer = new byte[16];
            var outputBytes = new byte[blockCipher.GetOutputSize(buffer.Length)];
            var length = 0;
            int bytesRead;

             while ((bytesRead = entry.Read(buffer, 0, buffer.Length)) > 0)
            {

                length = blockCipher.ProcessBytes(buffer, 0, bytesRead, outputBytes, 0);
                exit.Write(outputBytes, 0, length);
                //progresbar
                actuallyLenght += bytesRead;
                if (actuallyLenght > progres)
                {
                    window.progressBarOdszyfrowywanie.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                            delegate()
                            {
                                window.progressBarOdszyfrowywanie.Value += 0.1;
                            }
                            ));
                    actuallyLenght -= (long) progres;
                }
            }
            length = blockCipher.DoFinal(outputBytes, 0);
            exit.Write(outputBytes, 0, length);

            entry.Close();
            exit.Close();
        }
Example #2
0
        //-------------

        public static void SzyfrowanieRc6(string wejscie, string wyjscie, byte[] kluczsesyjny, NaglowekPliku naglowek, MainWindow window)
        {
            //entry
            var entry = new FileStream(wejscie, FileMode.Open);

            //exit
            var enter = Environment.NewLine;
            var enterdata = Encoding.UTF8.GetBytes(enter);
            var exit = new FileStream(wyjscie, FileMode.Append);
            exit.Write(enterdata, 0, enterdata.Length); // dodanie entera po xmlu

            var blockCipher = CreateCipher(naglowek.Tryb, naglowek.WielkoscPodBloku);

            ICipherParameters parameters;
            if (naglowek.Tryb == "ECB")
                parameters = new KeyParameter(kluczsesyjny);
            else
                parameters = new ParametersWithIV(new KeyParameter(kluczsesyjny), naglowek.Iv);
            blockCipher.Init(true, parameters);

            //date for progresbar
            var lenghtFile = entry.Length;
            double progres = lenghtFile/1000;
            long actuallyLenght = 0;

            //date for encrypt
            var buffer = new byte[16];
            byte[] outputBytes = null;
            var length = 0;
            int bytesRead;

            outputBytes = new byte[blockCipher.GetOutputSize(buffer.Length)];
            while ((bytesRead = entry.Read(buffer, 0, buffer.Length)) > 0)
            {
                length = blockCipher.ProcessBytes(buffer, 0, bytesRead, outputBytes, 0);
                exit.Write(outputBytes, 0, length);
                //progresbar
                actuallyLenght += bytesRead;
                if (actuallyLenght > progres)
                {
                    window.progressBarSzyfrowanie.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                            delegate() 
                            {
                                window.progressBarSzyfrowanie.Value += 0.1;
                            }
                        ));
                    actuallyLenght -= (long) progres;
                }
            }           
            length = blockCipher.DoFinal(outputBytes, 0);
            exit.Write(outputBytes, 0, length);


            exit.Close();
            entry.Close();
        }