示例#1
0
        // Шифрование тестового файла.
        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);
                        }
                    }
                }
            }
        }
示例#2
0
        private static void EncriptFile(X509Certificate2 sert, string sourceFile, string encFile)
        {
            var publicKey = (Gost3410_2012_256CryptoServiceProvider)sert.PublicKey.Key;

            var asymmetricAlg = publicKey as Gost3410_2012_256;

            if (asymmetricAlg == null)
            {
                throw new CryptographicException("Not a gost certificate");
            }

            var symmetricKey = Gost28147.Create();

            Gost3410_2012_256  senderRndKey           = Gost3410_2012_256.Create();
            Gost3410Parameters senderRndKeyParameters = senderRndKey.ExportParameters(false);

            GostSharedSecretAlgorithm agreeKey = senderRndKey.CreateAgree(asymmetricAlg.ExportParameters(false));

            var encodedSymmetricKey = agreeKey.Wrap(symmetricKey, GostKeyWrapMethod.CryptoProKeyWrap);

            ICryptoTransform transform = symmetricKey.CreateEncryptor();

            using (FileStream writer = new FileStream(encFile, FileMode.Create))
            {
                BinaryWriter binaryWriter = new BinaryWriter(writer);

                binaryWriter.Write(encodedSymmetricKey.Length);
                binaryWriter.Write(encodedSymmetricKey);

                binaryWriter.Write(symmetricKey.IV.Length);
                binaryWriter.Write(symmetricKey.IV);

                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(writer, senderRndKeyParameters);

                using (CryptoStream cryptoStream = new CryptoStream(writer, transform, CryptoStreamMode.Write))
                {
                    var buffer = new byte[100];

                    using (FileStream reader = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
                    {
                        var length = reader.Read(buffer, 0, buffer.Length);
                        while (length > 0)
                        {
                            cryptoStream.Write(buffer, 0, buffer.Length);
                            length = reader.Read(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
        }
示例#3
0
        private void button4_Click(object sender, EventArgs e)
        {
            if (checkBox2.Checked && !checkBox1.Checked)
            {
                string plain_text       = richTextBox1.Text;
                byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text);

                Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider();
                byte[] signature = csp.SignData(plain_text_bytes, hash);


                BERelement main_seq = new BERelement(0x30);
                BERelement sign_seq = new BERelement(0x30);

                sign_seq.AddItem(new BERelement(0x0C, cert_name));
                sign_seq.AddItem(new BERelement(0x04, signature));
                sign_seq.AddItem(new BERelement(0x04, plain_text_bytes));

                main_seq.AddItem(sign_seq);
                byte[] test = main_seq.GetEncodedPacket().ToArray();

                File.WriteAllBytes("signature", test);

                try
                {
                    Int32         port   = 9595;
                    TcpClient     client = new TcpClient("127.0.0.1", port);
                    NetworkStream stream = client.GetStream();

                    stream.Write(mode, 1, 1);
                    stream.Write(test, 0, test.Length);
                    stream.Close();
                    client.Close();
                }
                catch (ArgumentNullException exception)
                {
                    MessageBox.Show("ArgumentNullException: " + exception);
                }
                catch (SocketException exception)
                {
                    MessageBox.Show("SocketException: " + exception);
                }
            }
            else if (checkBox1.Checked && !checkBox2.Checked)
            {
                Gost28147                 gost       = Gost28147.Create();
                Gost3410Parameters        public_key = csp.ExportParameters(false);
                GostSharedSecretAlgorithm agree_key  = csp.CreateAgree(sign.ExportParameters(false));
                byte[]          wrapped_key          = agree_key.Wrap(gost, GostKeyWrapMethod.CryptoProKeyWrap);
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream    ms = new MemoryStream();
                bf.Serialize(ms, public_key);
                byte[] public_key_bytes = ms.ToArray();
                ms.Close();

                MemoryStream memory_stream    = new MemoryStream();
                CryptoStream cs               = new CryptoStream(memory_stream, gost.CreateEncryptor(), CryptoStreamMode.Write);
                string       plain_text       = richTextBox1.Text;
                byte[]       plain_text_bytes = Encoding.ASCII.GetBytes(plain_text);

                cs.Write(plain_text_bytes, 0, plain_text_bytes.Length);
                cs.FlushFinalBlock();

                byte[] cipher_text_bytes = memory_stream.ToArray();
                memory_stream.Close();
                cs.Close();

                BERelement main_seq = new BERelement(0x30);
                BERelement sign_seq = new BERelement(0x30);

                sign_seq.AddItem(new BERelement(0x0C, cert_name));
                sign_seq.AddItem(new BERelement(0x04, wrapped_key));
                sign_seq.AddItem(new BERelement(0x04, gost.IV));
                sign_seq.AddItem(new BERelement(0x04, public_key_bytes));
                sign_seq.AddItem(new BERelement(0x04, cipher_text_bytes));

                main_seq.AddItem(sign_seq);
                byte[] test = main_seq.GetEncodedPacket().ToArray();

                File.WriteAllBytes("encryption", test);

                try
                {
                    Int32         port   = 9595;
                    TcpClient     client = new TcpClient("127.0.0.1", port);
                    NetworkStream stream = client.GetStream();

                    stream.Write(mode, 0, 1);
                    stream.Write(test, 0, test.Length);
                    stream.Close();
                    client.Close();
                }
                catch (ArgumentNullException exception)
                {
                    MessageBox.Show("ArgumentNullException: " + exception);
                }
                catch (SocketException exception)
                {
                    MessageBox.Show("SocketException: " + exception);
                }
            }
            else if (checkBox2.Checked && checkBox1.Checked)
            {
                MessageBox.Show("suka!");

                string plain_text       = richTextBox1.Text;
                byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text);

                Gost3411CryptoServiceProvider hash = new Gost3411CryptoServiceProvider();
                byte[] signature = csp.SignData(plain_text_bytes, hash);


                BERelement main_seq = new BERelement(0x30);
                BERelement sign_seq = new BERelement(0x30);

                sign_seq.AddItem(new BERelement(0x0C, cert_name));
                sign_seq.AddItem(new BERelement(0x04, signature));
                sign_seq.AddItem(new BERelement(0x04, plain_text_bytes));

                main_seq.AddItem(sign_seq);
                byte[] test = main_seq.GetEncodedPacket().ToArray();

                File.WriteAllBytes("signature_and_encryption", test);

                Gost28147                 gost       = Gost28147.Create();
                Gost3410Parameters        public_key = csp.ExportParameters(false);
                GostSharedSecretAlgorithm agree_key  = csp.CreateAgree(sign.ExportParameters(false));
                byte[]          wrapped_key          = agree_key.Wrap(gost, GostKeyWrapMethod.CryptoProKeyWrap);
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream    ms = new MemoryStream();
                bf.Serialize(ms, public_key);
                byte[] public_key_bytes = ms.ToArray();
                ms.Close();

                MemoryStream memory_stream = new MemoryStream();
                CryptoStream cs            = new CryptoStream(memory_stream, gost.CreateEncryptor(), CryptoStreamMode.Write);
                //string plain_text = richTextBox1.Text;
                //byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text);

                byte[] plain_text_bytes2 = test;
                cs.Write(plain_text_bytes2, 0, plain_text_bytes2.Length);
                cs.FlushFinalBlock();

                byte[] cipher_text_bytes2 = memory_stream.ToArray();
                memory_stream.Close();
                cs.Close();

                BERelement main_seq2 = new BERelement(0x30);
                BERelement sign_seq2 = new BERelement(0x30);

                sign_seq2.AddItem(new BERelement(0x0C, cert_name));
                sign_seq2.AddItem(new BERelement(0x04, wrapped_key));
                sign_seq2.AddItem(new BERelement(0x04, gost.IV));
                sign_seq2.AddItem(new BERelement(0x04, public_key_bytes));
                sign_seq2.AddItem(new BERelement(0x04, cipher_text_bytes2));

                main_seq2.AddItem(sign_seq2);
                byte[] test2 = main_seq2.GetEncodedPacket().ToArray();

                try
                {
                    Int32         port   = 9595;
                    TcpClient     client = new TcpClient("127.0.0.1", port);
                    NetworkStream stream = client.GetStream();

                    stream.Write(mode, 2, 1);
                    stream.Write(test2, 0, test2.Length);
                    stream.Close();
                    client.Close();
                }
                catch (ArgumentNullException exception)
                {
                    MessageBox.Show("ArgumentNullException: " + exception);
                }
                catch (SocketException exception)
                {
                    MessageBox.Show("SocketException: " + exception);
                }
            }
        }
示例#4
0
        private void button1_Click(object sender, EventArgs e)
        {
            Gost28147                 gost       = Gost28147.Create();
            Gost3410Parameters        public_key = csp.ExportParameters(false);
            GostSharedSecretAlgorithm agree_key  = csp.CreateAgree(sign.ExportParameters(false));

            byte[]          wrapped_key = agree_key.Wrap(gost, GostKeyWrapMethod.CryptoProKeyWrap);
            BinaryFormatter bf          = new BinaryFormatter();
            MemoryStream    ms          = new MemoryStream();

            bf.Serialize(ms, public_key);
            byte[] public_key_bytes = ms.ToArray();
            ms.Close();

            MemoryStream memory_stream = new MemoryStream();
            CryptoStream cs            = new CryptoStream(memory_stream, gost.CreateEncryptor(), CryptoStreamMode.Write);
            string       plain_text    = richTextBox1.Text;

            byte[] plain_text_bytes = Encoding.ASCII.GetBytes(plain_text);

            cs.Write(plain_text_bytes, 0, plain_text_bytes.Length);
            cs.FlushFinalBlock();

            byte[] cipher_text_bytes = memory_stream.ToArray();
            memory_stream.Close();
            cs.Close();

            BERelement main_seq = new BERelement(0x30);
            BERelement sign_seq = new BERelement(0x30);

            sign_seq.AddItem(new BERelement(0x02, wrapped_key));
            sign_seq.AddItem(new BERelement(0x02, gost.IV));
            sign_seq.AddItem(new BERelement(0x02, public_key_bytes));
            sign_seq.AddItem(new BERelement(0x02, cipher_text_bytes));

            main_seq.AddItem(sign_seq);
            byte[] test = main_seq.GetEncodedPacket().ToArray();

            //File.WriteAllBytes("asn1", test);

            /*
             * sign_seq.AddItem(new BERelement(0x02, signature));
             * sign_seq.AddItem(new BERelement(0x02, plain_text_bytes));
             *
             * main_seq.AddItem(sign_seq);
             * byte[] test = main_seq.GetEncodedPacket().ToArray();
             */


            //send data
            try
            {
                Int32         port   = 9595;
                TcpClient     client = new TcpClient("127.0.0.1", port);
                NetworkStream stream = client.GetStream();

                stream.Write(mode, 0, 1);
                stream.Write(test, 0, test.Length);
                //stream.Write(wrapped_key, 0, wrapped_key.Length);
                //stream.Write(gost.IV, 0, gost.IV.Length);
                //stream.Write(public_key_bytes, 0, public_key_bytes.Length);
                //stream.Write(cipher_text_bytes, 0, cipher_text_bytes.Length);
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException exception)
            {
                MessageBox.Show("ArgumentNullException: " + exception);
            }
            catch (SocketException exception)
            {
                MessageBox.Show("SocketException: " + exception);
            }
        }