//генерация публичного ключа (с помощью секретного). public CECPoint publicKeyGen(BigInteger d) { CECPoint G = gDecompression(); CECPoint Q = CECPoint.multiply(G, d); return(Q); }
//формирование цифровой подписи. public string DSGen(byte[] h, BigInteger d) { BigInteger a = new BigInteger(h); BigInteger e = a % n; if (e == 0) { e = 1; } BigInteger k = new BigInteger(); CECPoint C = new CECPoint(); BigInteger r = new BigInteger(); BigInteger s = new BigInteger(); do { do { k.genRandomBits(n.bitCount(), new Random()); }while ((k < 0) || (k > n)); C = CECPoint.multiply(G, k); r = C.x % n; s = ((r * d) + (k * e)) % n; }while ((r == 0) || (s == 0)); string Rvector = padding(r.ToHexString(), n.bitCount() / 4); string Svector = padding(s.ToHexString(), n.bitCount() / 4); return(Rvector + Svector); }
//проверка цифровой подписи. public bool verifyDS(byte[] H, string sign, CECPoint Q) { string Rvector = sign.Substring(0, n.bitCount() / 4); string Svector = sign.Substring(n.bitCount() / 4, n.bitCount() / 4); BigInteger r = new BigInteger(Rvector, 16); BigInteger s = new BigInteger(Svector, 16); if ((r < 1) || (r > (n - 1)) || (s < 1) || (s > (n - 1))) { return(false); } BigInteger a = new BigInteger(H); BigInteger e = a % n; if (e == 0) { e = 1; } BigInteger v = e.modInverse(n); BigInteger z1 = (s * v) % n; BigInteger z2 = n + ((-(r * v)) % n); this.G = gDecompression(); CECPoint A = CECPoint.multiply(G, z1); CECPoint B = CECPoint.multiply(Q, z2); CECPoint C = A + B; BigInteger R = C.x % n; if (R == r) { return(true); } else { return(false); } }