Example #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);
        }
Example #2
0
 public byte[] GetPrivateKey() => priv.ToArray();