Пример #1
0
        public EllipticDiffieHellman(EllipticCurve curve, CurvePoint generator, IntX order, byte[] priv = null)
        {
            this.curve     = curve;
            this.generator = generator;

            // Generate private key
            if (priv == null)
            {
                byte[] max = order.ToArray();
                do
                {
                    byte[] p1 = new byte[5 /*rand.Next(max.Length) + 1*/];

                    rand.GetBytes(p1);

                    if (p1.Length == max.Length)
                    {
                        p1[p1.Length - 1] %= max[max.Length - 1];
                    }
                    else
                    {
                        p1[p1.Length - 1] &= 127;
                    }

                    this.priv = DHHelper.FromArray(p1);
                } while (this.priv < 2);
            }
            else
            {
                this.priv = DHHelper.FromArray(priv);
            }

            // Generate public key
            pub = curve.Multiply(generator, this.priv);
        }
Пример #2
0
        public EllipticDiffieHellman(EllipticCurve curve, CurvePoint generator, BigInteger order, byte[] priv = null)
        {
            this.curve     = curve;
            this.generator = generator;

            // Generate private key
            if (priv == null)
            {
                this.priv = new BigInteger();
                this.priv.GenRandomBits(order.DataLength, rand);
            }
            else
            {
                this.priv = new BigInteger(priv);
            }

            // Generate public key
            pub = curve.Multiply(generator, this.priv);
        }