private static void SignMessage(BigInteger h, BigInteger g, BigInteger p, BigInteger x, out BigInteger r, out BigInteger s) { BigInteger k, kRev, ret; do { k = CryptoTools.GenerateRandomBigInteger(2, p - 1); ret = CryptoTools.EuclidAlgorithm(p - 1, k, out _, out kRev); } while (ret != 1); if (kRev < 0) { kRev += (p - 1); } r = CryptoTools.ModuloPower(g, k, p); BigInteger u = (h - x * r) % (p - 1); if (u < 0) { u += (p - 1); } s = (kRev * u) % (p - 1); }
public static BigInteger GenerateShamirPrivateKeyUsingAnother(BigInteger p, BigInteger c) { BigInteger temp, d, ret = CryptoTools.EuclidAlgorithm(p - 1, c, out temp, out d); if (d < 0) { d += (p - 1); } return(d); }
internal static void SimulateRSASigning(BigInteger p, BigInteger q, BigInteger d, BigInteger m) { Console.WriteLine($"Alice gonna sign messsage m = {m} and send it to Bob"); BigInteger N = p * q; BigInteger phi = (p - 1) * (q - 1); BigInteger c, temp; BigInteger ret = CryptoTools.EuclidAlgorithm(phi, d, out temp, out c); if (ret != 1) { Console.WriteLine($"d = {d} isn't mutually prime with phi(N)! A new one will be generated"); BigInteger cCandidate, dCandidate; do { dCandidate = CryptoTools.GenerateRandomBigInteger(1, phi); ret = CryptoTools.EuclidAlgorithm(phi, dCandidate, out temp, out cCandidate); } while (ret != 1); c = cCandidate; d = dCandidate; } if (c < 0) { c += phi; } Console.WriteLine($"Alice's public keys are N = {N} and d = {d}"); Console.WriteLine($"Alice's private key is c = {c}"); BigInteger y = CalculateHash(m); Console.WriteLine($"Hash function of message {m} is {y} (h(m) = m)"); BigInteger s = SignMessage(y, c, N); Console.WriteLine($"Alice sent message {m} and signature {s} to Bob"); Console.WriteLine("Bob received them and checked if signature is correct"); SimulateRSAChecking(m, s, d, N); }
public static void GenerateShamirPrivateKeys(BigInteger p, out BigInteger c, out BigInteger d) { Random r = new Random(); BigInteger cCandidate, dCandidate, ret; do { BigInteger temp; cCandidate = r.Next(1, (int)p); ret = CryptoTools.EuclidAlgorithm(p - 1, cCandidate, out temp, out dCandidate); } while (ret != 1); //крутимся пока не найдём такое cCandidate: (cCandidate, p - 1) = 1. тогда dCandidate обратное к cCandidate с точностью до модуля c = cCandidate; if (dCandidate < 0) { dCandidate += (p - 1); } d = dCandidate; }
internal static void SimulateRSAExchange(BigInteger p, BigInteger q, BigInteger d, BigInteger x) { BigInteger N = p * q; BigInteger phi = (p - 1) * (q - 1); BigInteger c, temp; BigInteger ret = CryptoTools.EuclidAlgorithm(phi, d, out temp, out c); if (ret != 1) { Console.WriteLine($"d = {d} isn't mutually prime with phi(N)! A new one will be generated"); BigInteger cCandidate, dCandidate; do { dCandidate = CryptoTools.GenerateRandomBigInteger(1, phi); ret = CryptoTools.EuclidAlgorithm(phi, dCandidate, out temp, out cCandidate); } while (ret != 1); c = cCandidate; d = dCandidate; } if (c < 0) { c += phi; } Console.WriteLine($"Receiver's public keys are N = {N} and d = {d}"); Console.WriteLine($"Receiver's private key is c = {c}"); BigInteger y = CryptoTools.ModuloPower(x, d, N); Console.WriteLine($"Sender encrypted message and sent y = {y} to receiver"); BigInteger w = CryptoTools.ModuloPower(y, c, N); Console.WriteLine($"Receiver decrypted y and got w = {w}"); }