private static EllipticCurve SetupCurve(IConfigurationRoot config) { var curveSection = config.GetSection("EllipticCurve"); if (curveSection is null) { return(new EllipticCurve()); } System.Globalization.NumberStyles ns; if (config["NumberStyle"] == "dec") { ns = System.Globalization.NumberStyles.Integer; } else { ns = System.Globalization.NumberStyles.HexNumber; } var a = BigInteger.Parse(curveSection["A"], ns); var b = BigInteger.Parse(curveSection["B"], ns); var p = BigInteger.Parse(curveSection["P"], ns); var n = BigInteger.Parse(curveSection["N"], ns); var g = new BigIntegerPoint(BigInteger.Parse(curveSection.GetSection("G")["X"], ns), BigInteger.Parse(curveSection.GetSection("G")["Y"], ns)); return(new EllipticCurve(a, b, p, g, n)); }
/// <summary> /// Generates a random private-public key pair. /// </summary> /// <returns>The tuple consists of a private and public key</returns> public (BigInteger privateKey, BigIntegerPoint publicKey) GenerateKeyPair() { BigInteger privateKey = EllipticCurveHelpers.RandomIntegerBelow(this.Curve.N); BigIntegerPoint publicKey = this.Curve.ScalarMult(privateKey, this.Curve.G); return(privateKey, publicKey); }
/// <summary> /// Generates signature. /// </summary> /// <param name="privateKey"></param> /// <param name="message"></param> /// <returns></returns> public (BigInteger r, BigInteger s) Sign(string message, BigInteger privateKey) { BigInteger z = this.HashMessage(message); BigInteger r = 0; BigInteger s = 0; while (r == 0 || s == 0) { BigInteger k = EllipticCurveHelpers.RandomIntegerBelow(this.Curve.N); BigIntegerPoint point = this.Curve.ScalarMult(k, this.Curve.G); r = EllipticCurveHelpers.MathMod(point.X, this.Curve.N); s = EllipticCurveHelpers.MathMod((z + r * privateKey) * this.Curve.InverseMod(k, this.Curve.N), this.Curve.N); } return(r, s); }
public static ECPoint GetSharedKey(this DiffieHellman dh, byte[] bytePrivateKey, ECPoint bytePublicKey) { var privateKey = new BigInteger(bytePrivateKey); var publicKey = new BigIntegerPoint { X = new BigInteger(bytePublicKey.X), Y = new BigInteger(bytePublicKey.Y), }; var sharedKey = dh.GetSharedkey(privateKey, publicKey); var result = new ECPoint { X = sharedKey.X.ToByteArray(), Y = sharedKey.Y.ToByteArray(), }; return(result); }