// Шифрование тестового файла.
        static void EncryptTestFile(
            Gost3410_2012_512 publicKey,
            Gost3410_2012_512CryptoServiceProvider privateKey,
            string fileId = "2012_512")
        {
            // Создаем симметричный ключ.
            Gost28147 symmetric = Gost28147.Create();

            // Открываем ключ отправителя.
            Gost3410Parameters srcPublicKeyParameters = privateKey.ExportParameters(false);

            // Создаем agree ключ
            GostSharedSecretAlgorithm agree = privateKey.CreateAgree(
                publicKey.ExportParameters(false));

            // Зашифровываем симметричный ключ на agree ключе.
            byte[] WrappedKey = agree.Wrap(symmetric,
                                           GostKeyWrapMethod.CryptoPro12KeyWrap);

            // Создаем поток шифратора.
            ICryptoTransform transform = symmetric.CreateEncryptor();

            // Создаем зашифрованный файл.
            using (FileStream ofs = new FileStream(string.Format(EncryptedFileName, fileId), FileMode.Create))
            {
                BinaryWriter bw = new BinaryWriter(ofs);

                // Записываем зашифрованный симметричный ключ.
                bw.Write(WrappedKey.Length);
                bw.Write(WrappedKey);

                // Записываем синхропосылку
                bw.Write(symmetric.IV.Length);
                bw.Write(symmetric.IV);

                // Передаем открытый ключ.
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ofs, srcPublicKeyParameters);

                // Создаем поток шифрования для записи в файл.
                using (CryptoStream cs = new CryptoStream(ofs, transform, CryptoStreamMode.Write))
                {
                    byte[] data = new byte[4096];
                    // Открываем входной файл.
                    using (FileStream ifs = new FileStream(string.Format(SourceFileName, fileId), FileMode.Open, FileAccess.Read))
                    {
                        // и переписываем содержимое в выходной поток.
                        int length = ifs.Read(data, 0, data.Length);
                        while (length > 0)
                        {
                            cs.Write(data, 0, length);
                            length = ifs.Read(data, 0, data.Length);
                        }
                    }
                }
            }
        }
        // Расшифрование тестового файла.
        static void DecryptTestFile(Gost3410_2012_512CryptoServiceProvider privateKey, string fileId = "2012_512")
        {
            // Открываем зашифрованный файл.
            using (FileStream ifs = new FileStream(string.Format(EncryptedFileName, fileId), FileMode.Open, FileAccess.Read))
            {
                // Читаем зашифрованный симметричный ключ.
                BinaryReader br = new BinaryReader(ifs);
                byte[]       cek;
                int          cekLength = br.ReadInt32();
                cek = br.ReadBytes(cekLength);

                // Читаем синхропосылку
                byte[] iv;
                int    ivLength = br.ReadInt32();
                iv = br.ReadBytes(ivLength);

                // Читаем открытый ключ.
                BinaryFormatter    formatter = new BinaryFormatter();
                Gost3410Parameters srcPublicKeyParameters =
                    (Gost3410Parameters)formatter.Deserialize(ifs);

                // Создаем agree ключ
                GostSharedSecretAlgorithm agree = privateKey.CreateAgree(
                    srcPublicKeyParameters);

                // Расшифровываем симметричный ключ на agree
                SymmetricAlgorithm symmetric = agree.Unwrap(cek,
                                                            GostKeyWrapMethod.CryptoPro12KeyWrap);
                symmetric.IV = iv;

                // Создаем поток разшифрования.
                ICryptoTransform transform = symmetric.CreateDecryptor();

                // Создаем поток разшифрования из файла.
                using (CryptoStream cs = new CryptoStream(ifs, transform, CryptoStreamMode.Read))
                {
                    // Открываем расшифрованный файл
                    using (FileStream ofs = new FileStream(string.Format(DecryptedFileName, fileId), FileMode.Create))
                    {
                        byte[] data = new byte[4096];
                        // и переписываем содержимое в выходной поток.
                        int length = cs.Read(data, 0, data.Length);
                        while (length > 0)
                        {
                            ofs.Write(data, 0, length);
                            length = cs.Read(data, 0, data.Length);
                        }
                    }
                }
            }
        }