Ejemplo n.º 1
0
        public static void RSAalgorithm()
        {
            #region Generating
            //var plainText = RSACrypto.generateMessage(50);
            var plainText = "Hello, how are you? What are you doing today? Bayo";
            Console.WriteLine($"PlainText: {plainText} | Length: {plainText.Length}");

            Stopwatch sw = Stopwatch.StartNew();

            sw.Start();
            var p = RSACalculations.randomInteger();
            Thread.Sleep(100);
            var q = RSACalculations.randomInteger();
            sw.Stop();
            Console.WriteLine($"Value of p: {p}, q: {q} | Generation time [ms]: {sw.Elapsed}");

            sw.Start();
            var n = RSACalculations.nAndPhiGenerator("n", p, q);
            sw.Stop();
            Console.WriteLine($"Value of n: {n} | Generation time [ms]: {sw.Elapsed}");

            sw.Start();
            var phi = RSACalculations.nAndPhiGenerator("phi", p, q);
            sw.Stop();
            Console.WriteLine($"Value of phi: {phi} | Generation time [ms]: {sw.Elapsed}");

            sw.Start();
            var e = RSACalculations.eGenerator(phi);
            sw.Stop();
            Console.WriteLine($"Public key: {e} | Generation time [ms]: {sw.Elapsed}");

            sw.Start();
            BigInteger d = 0;
            try
            {
                d = RSACalculations.dGenerator(e, phi);
            }
            catch (ArithmeticException err)
            {
                TextWriter errorWriter = Console.Error;
                errorWriter.WriteLine(err.Message);
            }
            sw.Stop();
            Console.WriteLine($"Private key: {d} | Generation time [ms]: {sw.Elapsed}");
            #endregion

            var encryptList = RSACrypto.encryptMessage(plainText, e, n);

            Console.WriteLine("=== TESTS ===");
            BlumBlumShub.N = phi;
            BlumBlumShub.generatorBBS();

            var decryptText = RSACrypto.decryptMessage(encryptList, d, n);

            Console.WriteLine(plainText);
            RSACrypto.isSame(plainText, decryptText);
        }
        public SectionTwo()
        {
            // Blum Blum Shub RNG
            blumBlumShub = new BlumBlumShub();

            // Randomizing by shuffling, Algorithm B, by defualt using original RNG
            randomShuffle = new AlgorithmBShuffling(blumBlumShub);

            // Fisher-Yates Shuffle with Blum Blum Shub RNG
            fisherYatesShuffle = new FisherYatesShuffle(randomShuffle);
        }
Ejemplo n.º 3
0
        private static void TestBlumBlumShub()
        {
            RandomNumberGenerator rng = new BlumBlumShub(12);
            var nums = new int[10];

            for (int i = 0; i < 10; i++)
            {
                nums[i] = rng.Next(1, 100);
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(nums[i]);
            }
        }
Ejemplo n.º 4
0
        public ILandscape Generate(ILandscape input, IParameterProvider paramProvider)
        {
            var pointsCount = paramProvider.Get <int>("num");
            var width       = paramProvider.Get <int>("w");
            var height      = paramProvider.Get <int>("h");
            var rng         = new BlumBlumShub(DateTime.Now.Ticks);

            return(new ImmutableLandscapeBuilder()
                   .SetWidth(width)
                   .SetHeight(height)
                   .SetPoints(
                       Enumerable.Range(0, pointsCount)
                       .Select(x => new Point(rng.Next(width), rng.Next(height)))
                       .ToList()
                       )
                   .Build());
        }
Ejemplo n.º 5
0
        private int[] generateArray(int arraySize)
        {
            var          r = new Random();
            BlumBlumShub s = new BlumBlumShub();

            int[] array = new int[arraySize];
            for (int i = 0; i < array.Length; i++)
            {
                long[] randomNumbers = { s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100), s.Next(100) };
                int    min           = (int)randomNumbers[0];
                for (int a = 1; a < randomNumbers.Length; a++)
                {
                    if (randomNumbers[a] < min)
                    {
                        min = (int)randomNumbers[a];
                    }
                }
                array[i] = min;
            }
            return(array);
        }
Ejemplo n.º 6
0
        public void scenarioTwo()
        {
            int[]        problemSizes = { 1024, 5120, 25600, 128000 };
            BlumBlumShub shub         = new BlumBlumShub();

            //integers 1 to N shuffled using the Fisher-Yates algorithm
            for (int i = 0; i < problemSizes.Length; i++)
            {
                int[] array = generateArray(problemSizes[i]);
                Console.WriteLine("Problem Size (randomly chosen from 12 values) " + problemSizes[i] + " : ");
                FisherYatesShuffle fShuffle = new FisherYatesShuffle();
                array = fShuffle.Shuffle(array);
                Stopwatch stopWatch = new Stopwatch();

                //mergeSort testing
                MergeSort <int> mergeS       = new MergeSort <int>(array);
                long[]          runSpeeds    = new long[100];
                long            runSpeedsSum = 0;
                for (int a = 0; a < runSpeeds.Length; a++)
                {
                    stopWatch.Start();
                    mergeS.sort();
                    stopWatch.Stop();
                    runSpeeds[i]  = stopWatch.ElapsedTicks;
                    runSpeedsSum += runSpeeds[i];
                }
                //Average


                Console.WriteLine("Merge Sort (Average): " + runSpeedsSum / runSpeeds.Length);
                //heapSort testing
                stopWatch = new Stopwatch();

                //mergeSort testing
                Heap <int> Heap = new Heap <int>(array);
                runSpeeds    = new long[100];
                runSpeedsSum = 0;
                for (int a = 0; a < runSpeeds.Length; a++)
                {
                    stopWatch.Start();
                    Heap.sortHeap();
                    stopWatch.Stop();
                    runSpeeds[i]  = stopWatch.ElapsedTicks;
                    runSpeedsSum += runSpeeds[i];
                }

                Console.WriteLine("Heap Sort (Average): " + runSpeedsSum / runSpeeds.Length);

                //bucket Sort
                BucketSort <int> bucketSort = new BucketSort <int>(array);
                runSpeeds    = new long[100];
                runSpeedsSum = 0;
                for (int a = 0; a < runSpeeds.Length; a++)
                {
                    stopWatch.Start();
                    var arraySorted = bucketSort.bucketSort();
                    stopWatch.Stop();
                    runSpeeds[i]  = stopWatch.ElapsedTicks;
                    runSpeedsSum += runSpeeds[i];
                }
                Console.WriteLine("Bucket Sort (Average): " + runSpeedsSum / runSpeeds.Length);
            }
        }