/// <summary> /// 生成椭圆曲线数字签名 /// </summary> /// <param name="message">要签名的消息</param> /// <returns>返回签名的数字编码(r,s)</returns> public BigInteger[] GenerateSignature(byte[] message) { if (privateKey == null) { throw new InvalidOperationException(); } BigInteger e = CalculateE(curve.N, message); BigInteger d = new BigInteger(privateKey.Reverse().Concat(new byte[1]).ToArray()); BigInteger r, s; { do { BigInteger k; do { do { k = Helper_BigInt.NextBigInteger(curve.N.GetBitLength()); }while (k.Sign == 0 || k.CompareTo(curve.N) >= 0); ECPoint p = ECPoint.Multiply(curve.G, k); BigInteger x = p.X.Value; r = x.Mod(curve.N); }while (r.Sign == 0); s = (k.ModInverse(curve.N) * (e + d * r)).Mod(curve.N); if (s > curve.N / 2) { s = curve.N - s; } }while (s.Sign == 0); } return(new BigInteger[] { r, s }); }
public static UmbralPrivateKey GenRandomPrikey() { UmbralPrivateKey k = new UmbralPrivateKey(); k.key = Helper_BigInt.NextBigInteger(k.curve.N.GetBitLength()); return(k); }