public static void GenerateKey(out BigInteger N, out BigInteger E, out BigInteger D) { Calc.Generate.Random gen = new Calc.Generate.Random(); BigInteger P = gen.GetPrimeBig(72); BigInteger Q = gen.GetPrimeBig(72); N = P * Q; BigInteger phi = (P - 1) * (Q - 1); E = gen.GetPrimeBig(64); D = Calc.BigIntWorker.InverseMod(E, phi); }
private static bool InternalIsPrimeMR(BigInteger value, int witnessCount, params ulong[] witnesses) { // compute n − 1 as (2^s)·d (where d is odd) BigInteger valLessOne = value - BigInteger.One; BigInteger d = valLessOne / 2; // we know that value is odd and valLessOne is even, so unroll 1st iter of loop uint s = 1; while ((d % 2) == BigInteger.Zero) { d /= 2; s++; } Calc.Generate.Random gen = new Calc.Generate.Random(Convert.ToUInt32(DateTime.Now.Ticks % 100000000)); // test value against each witness for (int i = 0; i < witnessCount; i++) { BigInteger a; if (i < witnesses.Length) { a = witnesses[i]; if (a >= valLessOne) { a %= value - 3; a += 3; } } else { a = gen.GetBig(); } BigInteger x = BigInteger.ModPow(a, d, value); if (x == BigInteger.One) { continue; } for (uint r = 1; (r < s) && (x != valLessOne); r++) { x = BigInteger.ModPow(x, 2, value); if (x == BigInteger.One) { return(false); } } if (x != valLessOne) { return(false); } } // witnesses confirm value is prime return(true); }
public static void GenerateKey(out BigInteger N, out BigInteger E, out BigInteger D) { Calc.Generate.Random gen = new Calc.Generate.Random(Convert.ToUInt32(DateTime.Now.Ticks % 100000000)); BigInteger phi; do { BigInteger P = gen.GetPrimeBig(20); BigInteger Q = gen.GetPrimeBig(20); N = P * Q; phi = (P - 1) * (Q - 1); E = gen.GetPrimeBig(15); D = Calc.BigIntWorker.InverseMod(E, phi); } while (E > phi); }