public static byte[] GenerarFirmaElectronica(byte[] message, BigInteger privateKey)
        {
            while (true)
            {
                //Genero un valor 'k' aleatorio
                BigInteger k = Helper.Random(Params.n);

                //Calculo el punto P = k*G
                Punto kP = k * Params.G;

                BigInteger r = Arithmetic.Mod(kP.x, Params.n);

                if (r.IsZero)
                {
                    continue;
                }

                var eHash = FirmaElectronica.ObtenerHash(message);
                var e     = new BigInteger(eHash);
                if (e.Sign == -1)
                {
                    e = BigInteger.Negate(e);
                }

                BigInteger s = Arithmetic.Mod(
                    Arithmetic.Inversion_ExtendedEuclidean(k, Params.n) * (e + privateKey * r), Params.n
                    );

                var asn1 = new ASN1_Generator.ASN1_Generator();
                var seq  = new ASN1_Generator.ASN1_Sequence();

                seq.content.Add(new ASN1_Generator.ASN1_Integer()
                {
                    Value = r
                });
                seq.content.Add(new ASN1_Generator.ASN1_Integer()
                {
                    Value = s
                });

                asn1.Elements.Add(seq);

                return(asn1.GetBytes());
            }
        }
示例#2
0
        private static ASN1_Element parseSequence(byte[] content)
        {
            var thisSequence = new ASN1_Sequence();

            int cursor = 0;

            while (cursor < content.Length)
            {
                var type = content[cursor];
                var size = content[cursor + 1];

                switch (type)
                {
                case 0x02:
                    thisSequence.content.Add(parseInteger(content.Skip(cursor + 2).Take(size).ToArray()));
                    break;
                }
                cursor += size + 2;
            }

            return(thisSequence);
        }