/// <summary>
 /// Constructor with given starting prime number.
 /// </summary>
 /// <param name="startPrime">Starting prime number set to CurrentPrime property</param>
 public PrimeNumberGenerator(uint startPrime)
 {
     if (!NumericUtilities.IsPrime(startPrime))
     {
         throw new ArgumentException("Given start value is not a prime number!");
     }
     CurrentPrime = startPrime;
 }
 /// <summary>
 /// Generate next prime number and save it to CurrentPrime property.
 /// </summary>
 /// <returns>Next generated prime number</returns>
 public uint GetNext()
 {
     if (CurrentPrime == 2)
     {
         // Optimalization
         return(CurrentPrime = 3);
     }
     do
     {
         CurrentPrime += 2; // Skip even numbers
     } while (!NumericUtilities.IsPrime(CurrentPrime));
     return(CurrentPrime);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Create instance of SimpleRSA class.
        /// P and Q parameters must be prime numbers and P != Q.
        /// Their product should be large enough to correctly compute encrypted/decrypted messages.
        /// </summary>
        /// <param name="p">Prime number P</param>
        /// <param name="q">Primer number Q</param>
        public SimpleRSA(ushort p, ushort q)
        {
            if (!NumericUtilities.IsPrime(p))
            {
                throw new ArgumentException("P is not prime");
            }
            if (!NumericUtilities.IsPrime(q))
            {
                throw new ArgumentException("Q is not prime");
            }
            var euler = (uint)((p - 1) * (q - 1));

            Modulo     = (uint)p * q;
            PublicKey  = CountPublicKey(euler);
            PrivateKey = CountPrivateKey(euler, PublicKey);
        }