Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public byte[] GetSharedSecret(byte[] pK)
        {
            byte[] p1 = new byte[pK[0] | (pK[1] << 8) | (pK[2] << 16) | (pK[3] << 24)]; // Reconstruct x-axis size
            byte[] p2 = new byte[pK.Length - p1.Length - 4];
            Array.Copy(pK, 4, p1, 0, p1.Length);
            Array.Copy(pK, 4 + p1.Length, p2, 0, p2.Length);

            CurvePoint remotePublic = new CurvePoint(DHHelper.FromArray(p1), DHHelper.FromArray(p2));

            byte[] secret = curve.Multiply(remotePublic, priv).X.ToArray(); // Use the x-coordinate as the shared secret

            // PBKDF2-HMAC-SHA1 (Common shared secret generation method)
            return(new Rfc2898DeriveBytes(secret, Encoding.UTF8.GetBytes("P1sN0R4inb0wPl5P1sPls"), 1000).GetBytes(32));
        }