private static bool CheckSignature(BigInteger m, BigInteger s, BigInteger d, BigInteger N) { BigInteger h = CalculateHash(m); BigInteger w = CryptoTools.ModuloPower(s, d, N); return(w == h); }
private static void SimulateShamirExchange(BigInteger p, BigInteger cA, BigInteger cB, BigInteger m) { BigInteger dA, dB; dA = ShamirTools.GenerateShamirPrivateKeyUsingAnother(p, cA); Console.WriteLine($"dA = {dA}"); dB = ShamirTools.GenerateShamirPrivateKeyUsingAnother(p, cB); Console.WriteLine($"dB = {dB}"); BigInteger x = m; Console.WriteLine($"Message is {x}"); BigInteger y = CryptoTools.ModuloPower(x, cA, p); Console.WriteLine($"Alice calculated y = {y} and sent it to Bob"); BigInteger z = CryptoTools.ModuloPower(y, cB, p); Console.WriteLine($"Bob calculated z = {z} and sent it to Alice"); BigInteger u = CryptoTools.ModuloPower(z, dA, p); Console.WriteLine($"Alice calculated u = {u} and sent it to Bob"); BigInteger w = CryptoTools.ModuloPower(u, dB, p); Console.WriteLine($"Bob calculated w = {w}"); }
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 SimulateDiffieHellmanExchange(BigInteger p, BigInteger g) { Console.WriteLine("Alice starts exchange"); BigInteger xA = CryptoTools.GenerateDiffieHellmanPrivateKey(g, p); Console.WriteLine("Alice generated private key xA = {0}", xA); BigInteger yA = CryptoTools.GenerateDiffieHellmanPublicKey(g, xA, p); Console.WriteLine("Alice generated public key yA = {0} and sent it to Bob", yA); BigInteger xB = CryptoTools.GenerateDiffieHellmanPrivateKey(g, p); Console.WriteLine("Bob generated private key xB = {0}", xB); BigInteger yB = CryptoTools.GenerateDiffieHellmanPublicKey(g, xB, p); Console.WriteLine("Bob generated public key yB = {0} and sent it to Alice", yB); BigInteger zAB = CryptoTools.ModuloPower(yB, xA, p); Console.WriteLine($"Alice calculated zAB = {zAB} - common private key"); BigInteger zBA = CryptoTools.ModuloPower(yA, xB, p); Console.WriteLine($"Bob calculated zBA = {zBA} - common private key"); }
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); }
private static bool CheckBanknoteSignature(BigInteger n, BigInteger s, BigInteger d, BigInteger N) { if (n == CryptoTools.ModuloPower(s, d, N)) { return(true); } return(false); }
//private static List<NumberIndex> babyList = new List<NumberIndex>(), giantList = new List<NumberIndex>(); internal static BigInteger SolveEquation(BigInteger y, BigInteger a, BigInteger p) { List <NumberIndex> commonList = new List <NumberIndex>(); BigInteger k, m; k = m = (BigInteger)Math.Ceiling(Math.Sqrt((double)p)); for (BigInteger i = 0; i < m; i++) { commonList.Add(new NumberIndex((y * CryptoTools.ModuloPower(a, i, p)) % p, i, true)); } for (BigInteger j = 1; j <= k; j++) { commonList.Add(new NumberIndex(CryptoTools.ModuloPower(a, j * m, p), j, false)); } commonList.Sort(); NumberIndex prev = null, cur = null; BigInteger iX = 0, jX = 0; foreach (var element in commonList) { if (cur != null) { prev = cur; } cur = element; if (prev != null) { if (prev.Number == cur.Number) { if (prev.IsBaby != cur.IsBaby) { if (prev.IsBaby == false) { iX = prev.Index; jX = cur.Index; } else { jX = prev.Index; iX = cur.Index; } break; } else { } } } } return(iX * m - jX); }
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; }
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); } }
private static void SimulateShamirExchange(BigInteger p) { BigInteger cA, dA, cB, dB; ShamirTools.GenerateShamirPrivateKeys(p, out cA, out dA); Console.WriteLine($"Alice generated private keys cA = {cA}, dA = {dA}"); ShamirTools.GenerateShamirPrivateKeys(p, out cB, out dB); Console.WriteLine($"Bob generated private keys cB = {cB}, dB = {dB}"); BigInteger x = -1; do { Console.WriteLine("Please enter message, x < p"); string s = Console.ReadLine(); try { x = BigInteger.Parse(s); } catch (FormatException) { x = -1; } }while (x < 0 || x >= p); Console.WriteLine($"Message is {x}"); BigInteger y = CryptoTools.ModuloPower(x, cA, p); Console.WriteLine($"Alice calculated y = {y} and sent it to Bob"); BigInteger z = CryptoTools.ModuloPower(y, cB, p); Console.WriteLine($"Bob calculated z = {z} and sent it to Alice"); BigInteger u = CryptoTools.ModuloPower(z, dA, p); Console.WriteLine($"Alice calculated u = {u} and sent it to Bob"); BigInteger w = CryptoTools.ModuloPower(u, dB, p); Console.WriteLine($"Bob calculated w = {w}"); }
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)); }
private static bool CheckSignature(BigInteger m, BigInteger r, BigInteger s, BigInteger p, BigInteger y, BigInteger g) { BigInteger h = CalculateHash(m); return((CryptoTools.ModuloPower(y, r, p) * CryptoTools.ModuloPower(r, s, p)) % p == CryptoTools.ModuloPower(g, h, p)); }
private static BigInteger GenerateKey(int n) { return(CryptoTools.GenerateUnsignedRandomBigInteger(0, BigInteger.Pow(2, n))); }
internal static void SimulatePokerExchange(BigInteger p) { BigInteger cA, dA, cB, dB; GeneratePokerPrivateKeys(p, out cA, out dA); Console.WriteLine($"Alice's private keys are cA = {cA} and dA = {dA}"); GeneratePokerPrivateKeys(p, out cB, out dB); Console.WriteLine($"Bob's private keys are cB = {cB} and dB = {dB}"); BigInteger alpha, beta, gamma; //карты GenerateCardNumbers(p, out alpha, out beta, out gamma); Console.WriteLine($"Alice generated numbers for cards and told Bob that alpha is {alpha}, beta is {beta}, gamma is {gamma}"); Console.WriteLine("START OF THE EXCHANGE"); Triplet cards = new Triplet(new Card(alpha, "ALPHA"), new Card(beta, "BETA"), new Card(gamma, "GAMMA")); Triplet encryptedCardsForBob = cards.ModuloPower(cA, p); Console.WriteLine($"Alice encrypted card numbers: alpha to {encryptedCardsForBob.X}, beta to {encryptedCardsForBob.Y}, gamma to {encryptedCardsForBob.Z}..."); encryptedCardsForBob.Mix(); Console.WriteLine($"...And then mixed them: {encryptedCardsForBob.X}, {encryptedCardsForBob.Y}, {encryptedCardsForBob.Z} and sent to Bob"); BigInteger cardAEncryptedNumber = encryptedCardsForBob.ChooseRandom3(); Console.WriteLine($"Bob chose {cardAEncryptedNumber} and sent to Alice"); BigInteger cardANumber = CryptoTools.ModuloPower(cardAEncryptedNumber, dA, p); Console.WriteLine($"Alice decrypted it; her card number is {cardANumber} and it's {cards.FindName(cardANumber)}!"); encryptedCardsForBob.RemoveUsedCard(); Triplet encryptedCardsForAlice = encryptedCardsForBob.ModuloPower(cB, p); encryptedCardsForAlice.Mix(); Console.Write($"Bob encrypted card numbers, mixed them and sent to Alice: "); for (int i = 0; i < 3; i++) { if (encryptedCardsForAlice[i].Number != 0) { Console.Write($"{encryptedCardsForAlice[i].Name} to {encryptedCardsForAlice[i].Number} "); } } Console.WriteLine(); BigInteger cardBEncryptedNumber = encryptedCardsForAlice.ChooseRandom2(); BigInteger cardBPartiallyDecryptedNumber = CryptoTools.ModuloPower(cardBEncryptedNumber, dA, p); Console.WriteLine($"Alice chose {cardBEncryptedNumber}, found its power {cardBPartiallyDecryptedNumber} and sent to Bob"); BigInteger cardBNumber = CryptoTools.ModuloPower(cardBPartiallyDecryptedNumber, dB, p); Console.WriteLine($"Bob decrypted it; his card number is {cardBNumber} and it's {cards.FindName(cardBNumber)}!"); encryptedCardsForBob.RemoveUsedCard(); for (int i = 0; i < 3; i++) { if (cards[i].Number != cardANumber && cards[i].Number != cardBNumber) { Console.WriteLine($"The third card has number {cards[i].Number} and is {cards[i].Name} "); } } }
private static BigInteger GenerateElGamalPublicKey(BigInteger g, BigInteger privateKey, BigInteger p) { return(CryptoTools.ModuloPower(g, privateKey, p)); }
private static BigInteger DecryptMessage(BigInteger receiverPrivateKey, BigInteger p, BigInteger y, BigInteger k) { return(y * CryptoTools.ModuloPower(k, p - receiverPrivateKey - 1, p) % p); }
private static BigInteger SignMessage(BigInteger y, BigInteger c, BigInteger N) { return(CryptoTools.ModuloPower(y, c, N)); }