Example #1
0
        public void EncryptFile(string inputFile, string outputFile, byte[] key, string mode)
        {
            FileStream instream;
            FileStream outstream;

            try
            {
                double percent  = 0.0;
                long   current  = 0;
                long   sizeFile = 0;
                var    cipher   = new GOST28147Engine();
                cipher.Init(true, new KeyParameter(key.ToArray()));
                IMode chosenMode = ChooseMode(mode, key, cipher);
                outstream = File.Open(outputFile, FileMode.Create);
                int chunkSize = 1024 * 1024;
                using (instream = File.OpenRead(inputFile))
                {
                    sizeFile = instream.Length;
                    ProgressChanged?.Invoke(0.0);
                    int    byteRead = 0;
                    byte[] input    = new byte[chunkSize];
                    byte[] output   = new byte[chunkSize];

                    while (true)
                    {
                        byteRead = instream.Read(input, 0, chunkSize);
                        if (byteRead == chunkSize)
                        {
                            chosenMode.Process(input, output);
                            outstream.Write(output, 0, chunkSize);
                        }
                        else
                        {
                            byte[] temp = new byte[byteRead];
                            Array.Copy(input, 0, temp, 0, byteRead);
                            input  = temp;
                            input  = AddPadding(input.ToArray());
                            output = new byte[input.Length];
                            chosenMode.Process(input, output);
                            outstream.Write(output, 0, output.Length);
                            break;
                        }
                        current += byteRead;
                        percent  = (double)current / sizeFile;
                        percent *= 100;
                        ProgressChanged?.Invoke(percent);
                    }
                }
            }
            catch (Exception)
            {
                throw new Exception("Ошибка!");
            }
            outstream?.Close();
            instream?.Close();
        }
Example #2
0
        public override byte[] Decrypt(byte[] ciphertext, byte[] key, string mode)
        {
            byte[] result = new byte[ciphertext.Length];
            var    cipher = new GOST28147Engine();

            cipher.Init(false, new KeyParameter(key.ToArray()));
            IMode chosenMode = ChooseMode(mode, key, cipher);

            chosenMode.Process(ciphertext, result);
            return(RemovePadding(result));
        }
Example #3
0
        public override byte[] Encrypt(byte[] text, byte[] key, string mode)
        {
            text = AddPadding(text);
            var cipher = new GOST28147Engine();

            cipher.Init(true, new KeyParameter(key.ToArray()));
            IMode chosenMode = ChooseMode(mode, key, cipher);

            byte[] result = new byte[text.Length];
            chosenMode.Process(text, result);
            return(result);
        }
Example #4
0
        private IMode ChooseMode(string mode, byte[] key, GOST28147Engine engine)
        {
            IMode chosenMode;

            switch (mode)
            {
            case "ECB":
                chosenMode = new ECB(engine);
                break;

            case "CFB":
                chosenMode = new CFB(engine, key.ToArray());
                break;

            default:
                throw new Exception("Неправильный режим");
            }

            return(chosenMode);
        }