private static void EncryptMessage(BigInteger g, BigInteger receiverPublicKey, BigInteger p, BigInteger message, out BigInteger y, out BigInteger k) { BigInteger r = CryptoTools.GenerateRandomBigInteger(1, p - 1); k = CryptoTools.ModuloPower(g, r, p); y = (message * CryptoTools.ModuloPower(receiverPublicKey, r, p)) % p; }
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); }
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 SimulateMoneyExchange(BigInteger p, BigInteger q) { Console.WriteLine($"Bank's private p is {p}"); Console.WriteLine($"Bank's private q is {q}"); BigInteger N = p * q, phi = (p - 1) * (q - 1); Console.WriteLine($"Bank's public N is {N}, phi(N) = {phi}"); BigInteger c, d; CryptoTools.GenerateInverseNumbers(phi, out c, out d); Console.WriteLine($"Bank's private c is {c} and public d is {d}"); Console.WriteLine("START OF EXCHANGE"); BigInteger n = CryptoTools.GenerateRandomBigInteger(2, N); BigInteger r, rInv; CryptoTools.GenerateInverseNumbers(N, out r, out rInv); BigInteger n_ = n * CryptoTools.ModuloPower(r, d, N) % N; Console.WriteLine("Client wants to purchase an item."); Console.WriteLine($"Client generated private n = {n}, random r = {r}: (r, N) = 1, then calculated n_ = (n * r^d) mod N = {n_} and sent it to bank"); BigInteger s_ = CryptoTools.ModuloPower(n_, c, N); Console.WriteLine($"Bank calculated s_ = {s_}, withdrew money from client's account and sent s_ to client"); BigInteger s = (s_ * rInv) % N; Console.WriteLine($"Client calculated s = {s} - this is bank's signature. Client's banknote is <n = {n}, s = {s}>"); Console.WriteLine("Client sends banknote to shop"); SimulateBanknoteChecking(n, s, d, N); Console.WriteLine("Do you want to try to use the same banknote again? y - yes, else - no"); ConsoleKeyInfo cki = Console.ReadKey(); Console.WriteLine(); if (cki.KeyChar == 'y') { SimulateBanknoteChecking(n, s, d, N); } }
public static void SimulateElGamalSigning(BigInteger p, BigInteger g, BigInteger m) { Console.WriteLine($"Alice gonna sign messsage m = {m} and send it to Bob"); BigInteger x = CryptoTools.GenerateRandomBigInteger(2, p - 1); BigInteger y = CryptoTools.ModuloPower(g, x, p); Console.WriteLine($"Alice generated her private key x = {x} and calculated her public key y = {y}"); BigInteger h = CalculateHash(m); Console.WriteLine($"Hash function of message {m} is {h} (h(m) = m)"); BigInteger r, s; SignMessage(h, g, p, x, out r, out s); Console.WriteLine($"Alice signed her message and sent <m = {m}, r = {r}, s = {s}> to Bob"); Console.WriteLine("Bob received them and checked if signature is correct:"); SimulateElGamalChecking(m, r, s, p, y, g); }
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}"); }
private static BigInteger GenerateElGamalPrivateKey(BigInteger g, BigInteger p) { return(CryptoTools.GenerateRandomBigInteger(2, p - 1)); }