Exemple #1
0
        public void GeneratePrimeNumbers_Not_Empty_Or_Null()
        {
            var primes = PrimeNumberGenerator.GeneratePrimeNumbers(5);

            Assert.NotEmpty(primes);
            Assert.NotNull(primes);
        }
Exemple #2
0
        public void GetPrimeFactors_can_return_correct_result_for_5775_and_23100(PrimeNumberGenerator primeGenerator)
        {
            var factorProvider = new PrimeFactorProvider(primeGenerator);
            var sut            = new CommonPrimeFactorProvider(factorProvider);

            Assert.That(() => sut.GetPrimeFactors(5775, 23100), Is.EqualTo(new[] { 3, 5, 5, 7, 11 }));
        }
Exemple #3
0
        public IResultOfEncryptionHash EncryptHashCode(BigInteger hash, WrappedInteger recipientOpenedKey)
        {
            var p = Subscriber.P;
            var g = Subscriber.G;
            var q = Subscriber.Q;
            var x = Subscriber.ClosedKey;
            var y = Subscriber.OpenedKey;

            var k = PrimeNumberGenerator.GeneratePrimeNumber(1, (q - 1).ToInt32()); // 3;

            //hash = 9;

            var r        = (g.ВСтепень(k) % p) % q;
            var inverseK = (CryptoFormula.АлгоритмЕвклида(q, k, out var euErr) ?? throw new Exception(euErr));
            var s        = inverseK * (hash + ((x * r) % q)) % q;

            var result = new DSAResultOfEncryptionHash(r, s, Subscriber.OpenedKey, hash)
            {
                P = Subscriber.P.Value,
                Q = Subscriber.G.Value,
                G = Subscriber.Q.Value,
                K = k,
                X = Subscriber.ClosedKey.Value,
            };

            return(result);
        }
Exemple #4
0
 public void Intialize()
 {
     ReadObj           = new JSonObject();
     WriteObj          = new JSonObject();
     SymmetricKey      = new SymmetricKeyGenerator();
     GeneratoreMachine = new PrimeNumberGenerator();
 }
Exemple #5
0
        public void generateNewKeys(string keyName, string passphrase, int moduleSize)
        {
            BigInteger testValue = 0;

            do
            {
                p   = PrimeNumberGenerator.GeneratePrimeNumber(moduleSize / 2);
                q   = PrimeNumberGenerator.GeneratePrimeNumber(moduleSize / 2);
                n   = BigInteger.Multiply(p, q);
                phi = BigInteger.Multiply(BigInteger.Subtract(p, 1), BigInteger.Subtract(q, 1));

                e = PrimeNumberGenerator.GenerateMutuallyPrimeNumber(phi, moduleSize / 2);
                d = PrimeNumberGenerator.MultiplicativelyInverseNumber(phi, e);

                if (BigInteger.Compare(e, 0) < 0)
                {
                    continue;
                }
                if (BigInteger.Compare(d, 0) < 0)
                {
                    continue;
                }

                publicKey  = new RSAPublicKey(e, n);
                privateKey = new RSAPrivateKey(d, n);
                privateKey.EncryptByPassphrase(passphrase);

                testValue = privateKey.Decrypt(publicKey.Encrypt(TEST_VALUE));
            } while (!BigInteger.Equals(testValue, TEST_VALUE));

            _keyName = keyName;
        }
        public static void Main(string[] args)
        {
            var inputValidation = new InputValidation();

            if (inputValidation.ValidateInputParameters(args))
            {
                IPrimeNumberGenerator primeNumGenerator = new PrimeNumberGenerator();

                var primeNumbers = primeNumGenerator.Generate(inputValidation.StartValue, inputValidation.EndValue);

                if (primeNumbers == null || !primeNumbers.Any())
                {
                    Console.WriteLine($"No prime numbers exist in specified range [{inputValidation.StartValue}, {inputValidation.EndValue}]");
                    return;
                }

                Console.WriteLine($"Prime numbers in specified range [{inputValidation.StartValue}, {inputValidation.EndValue}] (in ascending order):");

                foreach (var prime in primeNumbers)
                {
                    Console.Write($"{prime} ");
                }

                Console.WriteLine();
            }
        }
        public static (BigInteger P, BigInteger G) GenerateKeys(Random random)
        {
            PrimeNumberGenerator.PrimeRandom = random;
            var p = PrimeNumberGenerator.GeneratePrimeNumber(100, 1000); // (1000000, int.MaxValue); //
            var g = random.Next(2, p - 1);

            return(p, g);
        }
        public void Primes_GenerateNPrimes_ReturnsNthPrime2(int nthPrime, int expected)
        {
            var sut = new PrimeNumberGenerator();

            var actual = sut.Primes().Skip(nthPrime - 1).First();

            Assert.Equal(expected, actual);
        }
Exemple #9
0
        public void GeneratePrimeNumbers_Always_Returns_Primes(int nPrimes)
        {
            var results = PrimeNumberGenerator.GeneratePrimeNumbers(nPrimes);

            foreach (int i in results)
            {
                Assert.True(isPrime(i));
            }
        }
Exemple #10
0
        public static void Main(string[] args)
        {
            //Program1 prog1 = new Program1();
            //prog1.Run();

            PrimeNumberGenerator prog2 = new PrimeNumberGenerator();
            prog2.Run();
            System.Console.WriteLine("Press any key to Stop......");
            System.Console.ReadLine();
        }
Exemple #11
0
    public void PickedPrimeNumbersAreAsExpected()
    {
        var sut          = new PrimeNumberGenerator();
        var primeNumbers = sut.Generate(15).ToList();

        Assert.AreEqual(47, primeNumbers[14]);
        var otherPrimeNumbers = sut.Generate(155).ToList();

        Assert.AreEqual(907, otherPrimeNumbers[154]);
    }
Exemple #12
0
        public void TestPrime()
        {
            PrimeNumberGenerator png = new PrimeNumberGenerator();

            List <int> primeList = new List <int>()
            {
                2, 3, 5, 7, 11, 13, 17, 19
            };

            CollectionAssert.AreEqual(png.GetPrimeNumbers(20), primeList);
        }
 public void GetFirstGreaterTest()
 {
     Assert.AreEqual(3, PrimeNumberGenerator.GetFirstGreater(2));
     Assert.AreEqual(5, PrimeNumberGenerator.GetFirstGreater(4));
     Assert.AreEqual(7, PrimeNumberGenerator.GetFirstGreater(5));
     Assert.AreEqual(11, PrimeNumberGenerator.GetFirstGreater(8));
     Assert.AreEqual(7, PrimeNumberGenerator.GetFirstGreater(5));
     Assert.AreEqual(23, PrimeNumberGenerator.GetFirstGreater(22));
     Assert.AreEqual(53, PrimeNumberGenerator.GetFirstGreater(52));
     Assert.AreEqual(29, PrimeNumberGenerator.GetFirstGreater(25));
 }
Exemple #14
0
        private void BtSetRandomSubscriberParams(object sender, EventArgs e)
        {
            CatchException(() =>
            {
                tbClosedKey.Text = PrimeNumberGenerator
                                   .GeneratePrimeNumber(3, Convert.ToInt32(tbParamQ.Text) - 1, _random)
                                   .ToString();

                tbSubscriberName.Text = HelpfulMethods.GetRandomName(_random);
            });
        }
Exemple #15
0
    public void PrimeNumbersAreReturnedInSameSequence()
    {
        var sut               = new PrimeNumberGenerator();
        var primeNumbers      = sut.Generate(1155).ToList();
        var otherPrimeNumbers = sut.Generate(1515).ToList();

        for (var i = 0; i < primeNumbers.Count; i++)
        {
            Assert.AreEqual(primeNumbers[i], otherPrimeNumbers[i]);
        }
    }
Exemple #16
0
        public void CanGetRandomPrime(int bitLength)
        {
            var pg = new PrimeNumberGenerator();

            for (int i = 0; i < 3; i++)
            {
                var prime   = pg.GetRandomPrime(bitLength, 3);
                var isPrime = IsPrime((long)prime);
                Assert.True(isPrime);
            }
        }
Exemple #17
0
    public void CanGenerateFirstPrimeNumbers()
    {
        var sut                  = new PrimeNumberGenerator();
        var primeNumbers         = sut.Generate(5).ToList();
        var expectedPrimeNumbers = new[] { 2, 3, 5, 7, 11 };

        Assert.AreEqual(expectedPrimeNumbers.Length, primeNumbers.Count);
        for (var i = 0; i < primeNumbers.Count; i++)
        {
            Assert.AreEqual(expectedPrimeNumbers[i], primeNumbers[i]);
        }
    }
        public void ShouldGenerateFirst10PrimeNumbers()
        {
            var expected = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
            var target   = new PrimeNumberGenerator();

            var actual = target.Generate().Take(10).ToArray();

            Assert.Equal(expected.Length, actual.Length);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], actual[i]);
            }
        }
        public void ShouldGenerateFirst10PrimeNumbers()
        {
            var expected = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
            var target = new PrimeNumberGenerator();

            var actual = target.Generate().Take(10).ToArray();

            Assert.Equal(expected.Length, actual.Length);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], actual[i]);
            }
        }
Exemple #20
0
        public void PrimeNumberGeneratorChosenPrime()
        {
            var generator = new PrimeNumberGenerator(7);
            var expected  = new List <uint> {
                11, 13, 17, 19, 23
            };

            Assert.AreEqual(generator.CurrentPrime, 7u);
            foreach (var prime in expected)
            {
                Assert.AreEqual(generator.GetNext(), prime);
                Assert.AreEqual(generator.CurrentPrime, prime);
            }
        }
Exemple #21
0
        public void PrimeNumberGeneratorDefaultPrime()
        {
            var generator = new PrimeNumberGenerator();
            var expected  = new List <uint> {
                3, 5, 7, 11, 13
            };

            Assert.AreEqual(generator.CurrentPrime, 2u);
            foreach (var prime in expected)
            {
                Assert.AreEqual(generator.GetNext(), prime);
                Assert.AreEqual(generator.CurrentPrime, prime);
            }
        }
Exemple #22
0
        public static HashFunction Generate(int range)
        {
            long longPrime = PrimeNumberGenerator.GetFirstGreater(range);

            if (longPrime > int.MaxValue)
            {
                throw new RangeTooBigException();
            }

            int prime = Convert.ToInt32(longPrime);

            int a = _randomGenerator.Next(1, prime - 1);
            int b = _randomGenerator.Next(0, prime - 1);

            return(new HashFunction(prime, range, a, b));
        }
        public IResultOfEncryptionHash EncryptHashCode(BigInteger hash, WrappedInteger recipientOpenedKey)
        {
            var p = Subscriber.P;
            var g = Subscriber.G;
            //var k = 9;
            WrappedInteger k = PrimeNumberGenerator.GeneratePrimeNumber(1, (p - 2).ToInt32());

            while (CryptoFormula.НайтиНОД(k, p - 1) != 1)
            {
                k = PrimeNumberGenerator.GeneratePrimeNumber(1, (p - 2).ToInt32());
            }
            k    %= p - 1;
            hash %= p - 1;

            var a = g.ВСтепень(k) % p;
            var b = (CryptoFormula.АлгоритмЕвклида(p - 1, k, out var euErr) ?? throw new Exception(euErr))
                    * ((p - 1) + (hash - ((Subscriber.ClosedKey * a) % (p - 1)))) % (p - 1);

            return(new ElgamalResultOfEncryptionHash(a, b, Subscriber.OpenedKey, hash));
        }
Exemple #24
0
        public static (BigInteger P, BigInteger Q, BigInteger G) GenerateParams(Random random)
        {
            PrimeNumberGenerator.PrimeRandom = random;
            var p = PrimeNumberGenerator.GeneratePrimeNumber(100, 1000); // (1000000, int.MaxValue); //
            var q = (p - 1).азложитьНаПростыеМножители().Max();
            var h = random.Next(1, p - 2);

            var getG = new Func <BigInteger>(() => { return(h.ВСтепень((p - 1) / q) % p); });
            var g    = getG();

            while (g < 2)
            {
                p = PrimeNumberGenerator.GeneratePrimeNumber(100, 1000);
                q = (p - 1).азложитьНаПростыеМножители().Max();
                h = random.Next(1, p - 2);
                g = getG();
            }

            return(p, q, g);
        }
Exemple #25
0
    public static void Main()
    {
        int limit = 10000000;
        PrimeNumberGenerator primes = new PrimeNumberGenerator(limit);
        int start = 1000001;

        try
        {
            int[] values = primes.GetPrimesFrom(start);
            Console.WriteLine("There are {0} prime numbers from {1} to {2}",
                              start, limit);
        }
        catch (NotPrimeException e)
        {
            Console.WriteLine("{0} is not prime", e.NonPrime);
            Console.WriteLine(e);
            Console.WriteLine("--------");
        }

        AppDomain            domain = AppDomain.CreateDomain("Domain2");
        PrimeNumberGenerator gen    = (PrimeNumberGenerator)domain.CreateInstanceAndUnwrap(
            typeof(Example).Assembly.FullName,
            "PrimeNumberGenerator", true,
            BindingFlags.Default, null,
            new object[] { 1000000 }, null, null);

        try
        {
            start = 100;
            Console.WriteLine(gen.GetPrimesFrom(start));
        }
        catch (NotPrimeException e)
        {
            Console.WriteLine("{0} is not prime", e.NonPrime);
            Console.WriteLine(e);
            Console.WriteLine("--------");
        }
    }
Exemple #26
0
        static void SolveKeyGeneration()
        {
            Console.WriteLine(StandardMessages.GeneratingPrimesInit());
            var sw = new Stopwatch();

            sw.Start();
            var primeNumberGenerator = new PrimeNumberGenerator(keySize / 2);

            sw.Stop();

            int    n = primeNumberGenerator.Primes.Count;
            double t = sw.Elapsed.TotalSeconds;

            Console.WriteLine(StandardMessages.GeneratingPrimesFinish(n, t));

            Console.WriteLine(StandardMessages.GeneratingKeys());
            var keyPair = rsa.GenerateKeyPair(primeNumberGenerator);

            string publicKey  = PublicKeyConverter.KeyToString(keyPair.PublicKey).ToHexString(),
                   privateKey = PrivateKeyConverter.KeyToString(keyPair.PrivateKey).ToHexString();

            Console.WriteLine(StandardMessages.DisplayKeys(publicKey, privateKey));
        }
        public void HundredThousandPlaces_Test()
        {
            var g = new PrimeNumberGenerator();

            g.HundredThousandPlaces();
        }
 public void SetUp()
 {
     _primeNumberGenerator = new PrimeNumberGenerator(new DivisorsLocator());
 }
Exemple #29
0
 public static void Task()
 {
     PrimeNumberGenerator.CalculatePrimeNumbers(10000);
     FactorialCalculator.CalculateFactorial(5000);
 }
Exemple #30
0
 public BigInteger[] DivideTest(int maxNumber)
 {
     return(PrimeNumberGenerator.GetPrimeNumbers(maxNumber).ToArray());
 }
Exemple #31
0
        public void TestSinglePrime()
        {
            PrimeNumberGenerator png = new PrimeNumberGenerator();

            Assert.IsTrue(png.IsPrime(113));
        }
 public void HundredPlaces_Test()
 {
     var    g       = new PrimeNumberGenerator();
     Number divisor = g.HundredPlaces();
 }
        public void MillionPlaces_Test()
        {
            var g = new PrimeNumberGenerator();

            g.MillionPlaces();
        }
 public void SetUp()
 {
     _divisorsLocator = new Mock<IDivisorsLocator>();
     _primeNumberGenerator = new PrimeNumberGenerator(_divisorsLocator.Object);
 }