From dd5a1b55fe624bdfc21259121853da5a1d47cd1b Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sun, 24 May 2026 13:07:23 +0000 Subject: [PATCH] [Sync Iteration] csharp/diffie-hellman/1 --- solutions/csharp/diffie-hellman/1/DiffieHellman.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 solutions/csharp/diffie-hellman/1/DiffieHellman.cs diff --git a/solutions/csharp/diffie-hellman/1/DiffieHellman.cs b/solutions/csharp/diffie-hellman/1/DiffieHellman.cs new file mode 100644 index 0000000..c104e3b --- /dev/null +++ b/solutions/csharp/diffie-hellman/1/DiffieHellman.cs @@ -0,0 +1,12 @@ +using System.Numerics; + +public static class DiffieHellman +{ + private static Random rng = new(); + public static BigInteger PrivateKey(BigInteger primeP) => rng.Next(1, (int)primeP); + + // use BigInteger.ModPow() instead of Math.Pow() since double has less digits and cuts off! + public static BigInteger PublicKey(BigInteger primeP, BigInteger primeG, BigInteger privateKey) => BigInteger.ModPow(primeG, privateKey, primeP); + + public static BigInteger Secret(BigInteger primeP, BigInteger publicKey, BigInteger privateKey) => BigInteger.ModPow(publicKey, privateKey, primeP); +} \ No newline at end of file