static public byte[] SetCommandAsn1(int cmd)
        {
            BERelement asn = new BERelement(0x30);

            asn.AddItem(new BERelement(0x02, cmd));
            return(asn.GetEncodedPacket());
        }
        static public byte[] SetCertNameAsn1(byte[] certName)
        {
            BERelement asn = new BERelement(0x30);

            asn.AddItem(new BERelement(0x04, certName));
            return(asn.GetEncodedPacket());
        }
        public static byte[] SetCertASN1(byte[] cert)
        {
            var mainSeq = new BERelement(0x30);

            mainSeq.AddItem(new BERelement(0x0C, cert));
            return(mainSeq.GetEncodedPacket());
        }
        static public byte[] SetSymKeyAndIVAsn1(byte[] data, byte[] iv)
        {
            BERelement asn = new BERelement(0x30);

            asn.AddItem(new BERelement(0x04, data));
            asn.AddItem(new BERelement(0x04, iv));
            return(asn.GetEncodedPacket());
        }
Exemple #5
0
        private void button2_Click(object sender, EventArgs e)
        {
            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, Encoding.UTF8.GetBytes("sign")));
            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();

            File.WriteAllBytes("asn2", test);

            /*
             * BERelement mSeq = BERelement.DecodePacket(test);
             * BERelement sSeq = null;
             * sSeq = mSeq.Items[0];
             * var signed2 = sSeq.Items[0].Value;
             * MessageBox.Show(signed2.Length.ToString());
             * var msg = sSeq.Items[1].Value;
             */

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

                stream.Write(mode, 1, 1);
                // stream.Write(signature, 0, signature.Length);
                //stream.Write(plain_text_bytes, 0, plain_text_bytes.Length);
                stream.Write(test, 0, test.Length);
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException exception)
            {
                MessageBox.Show("ArgumentNullException: " + exception);
            }
            catch (SocketException exception)
            {
                MessageBox.Show("SocketException: " + exception);
            }
        }
        public static byte[] SetCertListASN1(X509Certificate2Collection certs)
        {
            var asnCerts = new BERelement(0x30);

            foreach (var name in certs)
            {
                asnCerts.AddItem(new BERelement(0x0C, name.FriendlyName));
            }

            return(asnCerts.GetEncodedPacket());
        }
        static public byte[] CreateSignature(byte[] signature, byte[] sCert, DateTime sTime, byte[] data)
        {
            BERelement mainSeq = new BERelement(0x30);
            BERelement signSeq = new BERelement(0x30);

            signSeq.AddItem(new BERelement(0x0C, signature));
            signSeq.AddItem(new BERelement(0x0C, sCert));
            signSeq.AddItem(new BERelement(0x0c, BitConverter.GetBytes(sTime.ToBinary())));
            BERelement fileSeq = new BERelement(0x30);

            fileSeq.AddItem(new BERelement(0x0C, data));
            mainSeq.AddItem(signSeq);
            mainSeq.AddItem(fileSeq);
            return(mainSeq.GetEncodedPacket());
        }
Exemple #8
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);
                }
            }
        }
Exemple #9
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);
            }
        }