public static RSACryption Create() { RSACryption r = new RSACryption(); var rand = new Random((int)DateTime.Now.ToFileTime()); BigInteger p = PrimeFunctions.BigPrimeGroups[rand.Next(0, PrimeFunctions.BigPrimeGroups.Length)]; BigInteger q = PrimeFunctions.BigPrimeGroups[rand.Next(0, PrimeFunctions.BigPrimeGroups.Length)]; while (q == p) { q = PrimeFunctions.BigPrimeGroups[rand.Next(0, PrimeFunctions.BigPrimeGroups.Length)]; } BigInteger phi = (p - 1) * (q - 1); BigInteger n = p * q; BigInteger k = rand.Next(10, 20); BigInteger e = 1; BigInteger d = -1; while (d == -1) { e = PrimeFunctions.MiddlePrimeGroups[rand.Next(0, PrimeFunctions.MiddlePrimeGroups.Length)]; d = ExtendedEuclideanalgorithm.GetMultiplicativeInverseModule(e, 0 - phi); } r.LoadPublicKey(Convert.ToBase64String(e.ToByteArray()), Convert.ToBase64String(n.ToByteArray())); r.LoadPrivateKey(Convert.ToBase64String(d.ToByteArray()), Convert.ToBase64String(n.ToByteArray())); return(r); }
/// <summary> /// 创建密钥对。 /// </summary> /// <param name="n">N值</param> /// <param name="e">公钥</param> /// <param name="d">密钥</param> public static void Create(out byte[] n, out byte[] e, out byte[] d) { RSACryption r = new RSACryption(); var rand = new Random((int)DateTime.Now.ToFileTime()); BigInteger p = PrimeFunctions.BigPrimeGroups[rand.Next(0, PrimeFunctions.BigPrimeGroups.Length)]; BigInteger q = PrimeFunctions.BigPrimeGroups[rand.Next(0, PrimeFunctions.BigPrimeGroups.Length)]; while (q == p) { q = PrimeFunctions.BigPrimeGroups[rand.Next(0, PrimeFunctions.BigPrimeGroups.Length)]; } BigInteger phi = (p - 1) * (q - 1); BigInteger nv = p * q; BigInteger k = rand.Next(10, 20); BigInteger ev = 1; BigInteger dv = -1; while (dv == -1) { ev = PrimeFunctions.MiddlePrimeGroups[rand.Next(0, PrimeFunctions.MiddlePrimeGroups.Length)]; dv = ExtendedEuclideanalgorithm.GetMultiplicativeInverseModule(ev, 0 - phi); } n = nv.ToByteArray(); e = ev.ToByteArray(); d = dv.ToByteArray(); }