public static Keys GenerateRSAKeys(BigInt E, BigInt p, BigInt q, bool CheckPreConditions = false) { Keys Keys = new Keys(); int DiffPQSize = p.BitsCount() - q.BitsCount(); if (!CheckPreConditions || (p != q && Math.Abs(DiffPQSize) <= 30)) { BigInt N = new BigInt(E.Size, 0); BigInt phiN = new BigInt(E.Size, 0); phiN = (p - 1) * (q - 1); if (BigInt.Egc(E, phiN) == 1) { N = p * q; BigInt D = E.ModInverse(phiN); Keys.PublicKey = new PublicKey(N, E); Keys.PrivateKey = new PrivateKey(N, D, p, q); } else { throw new ArithmeticException("An error occur while generating the RSA Key: Egc(E, phiN) != 1!"); } } else { throw new ArithmeticException("An error occur while generating the RSA Key: p == q or |Size(p) - Size(q)| > 30!"); } return(Keys); }
public static Keys GenerateRSAKeys(BigInt E, short Size, int Rounds = 20, bool CheckPreConditions = false) { Keys Keys = new Keys(); Random Rand = new Random(); BigInt N = new BigInt(E.Size, 0); BigInt phiN = new BigInt(E.Size, 0); // Generate p a Prime Number of Size BigInt p = new BigInt(E.Size, 0); BigInt q = new BigInt(E.Size, 0); bool Done = false; int DiffPQSize = 0; while (!Done) { p.GenPseudoPrime(Size, Rounds, Rand); do { q.GenPseudoPrime(Size, Rounds, Rand); DiffPQSize = p.BitsCount() - q.BitsCount(); } while (p == q || (Math.Abs(DiffPQSize) > 30 && CheckPreConditions)); phiN = (p - 1) * (q - 1); if (BigInt.Egc(E, phiN) == 1) { Done = true; N = p * q; } else { p.Clear(); q.Clear(); } } BigInt D = E.ModInverse(phiN); Keys.PublicKey = new PublicKey(N, E); Keys.PrivateKey = new PrivateKey(N, D, p, q); return(Keys); }