Пример #1
0
        public static void B()
        {
            Console.WriteLine("(B) Using WriteNum method.");

            BigNum bigNum1a = new BigNum();

            bigNum1a.ReadNum("0101");
            BigNum bigNum2a = new BigNum();

            bigNum2a.ReadNum(new List <int> {
                399, 385
            });

            List <int> bigNum1PositionsOf1s = bigNum1a.WriteNum1sPosition();
            List <int> bigNum2PositionsOf1s = bigNum2a.WriteNum1sPosition();

            Console.Write("first number 1 digit positions are at: ");
            foreach (var pos in bigNum1PositionsOf1s)
            {
                Console.Write(pos);
                Console.Write(",");
            }

            Console.WriteLine();
            Console.Write("second number 1 digit positions are at: ");
            foreach (var pos in bigNum2PositionsOf1s)
            {
                Console.Write(pos);
                Console.Write(",");
            }

            Console.WriteLine();
        }
Пример #2
0
        public static void IIB()
        {
            Console.WriteLine("II.");
            Console.WriteLine("Finds the first five prime numbers with 100 binary digits");
            int countPrimeNumberFound = 0;
            int tryNumber             = 0;

            while (countPrimeNumberFound < 5)
            {
                List <int> positionsOf1s = new List <int> // Trusting that the number will have 100 binary digits.
                {
                    GlobalVariables.MAX_NUM - 100,
                    GlobalVariables.MAX_NUM - 1
                };
                int k = 20;

                BigNum primeNumberSuspect = new BigNum();
                primeNumberSuspect.ReadNum(positionsOf1s);
                Console.WriteLine($"try number: {++tryNumber}");
                if (MathOps.IsPrime(primeNumberSuspect, k))
                {
                    Console.WriteLine($"Prime {countPrimeNumberFound}: {primeNumberSuspect.BinaryValue}");
                    Console.WriteLine();
                }

                primeNumberSuspect.AddNum(GlobalVariables.two);
            }

            Console.WriteLine();
        }
Пример #3
0
        public static void H()
        {
            Console.WriteLine("(G) Using CompNum method.");
            Console.WriteLine("Comparing between zeroes.");
            BigNum bigNumH1 = new BigNum();
            BigNum bigNumH2 = new BigNum();

            bigNumH1.CompNum(bigNumH2);

            Console.WriteLine("Comparing between 8 and 16.");
            BigNum bigNumH3 = new BigNum(8L);
            BigNum bigNumH4 = new BigNum(16L);

            bigNumH3.CompNum(bigNumH4);

            Console.WriteLine("Comparing between 32 and 32.");
            BigNum bigNumH5       = new BigNum(32L);
            BigNum bigNumH6       = new BigNum();
            var    positionsOf1sH = new List <int> {
                394
            };

            bigNumH6.ReadNum(positionsOf1sH);
            bigNumH5.CompNum(bigNumH6);

            Console.WriteLine();
        }
Пример #4
0
        public static void IIC()
        {
            Console.WriteLine("III.");
            Console.WriteLine("Using RSA");

            //BigNum b = new BigNum();
            //List<int> bPositionsOf1s = new List<int>
            //{
            //     GlobalVariables.MAX_NUM - 16 - 1,
            //     GlobalVariables.MAX_NUM - 1
            //};
            //b.ReadNum(bPositionsOf1s);

            BigNum     b = new BigNum();
            List <int> bPositionsOf1s = new List <int>
            {
                GlobalVariables.MAX_NUM - 5 - 1,
                GlobalVariables.MAX_NUM - 1
            };

            b.ReadNum(bPositionsOf1s);
            Console.Write("b = ");
            MathOps.PrintNumberInPowersOfTwo(b);

            BigNum     plainText = new BigNum();
            List <int> plainTextPositionsOf1s = new List <int>
            {
                GlobalVariables.MAX_NUM - 197 - 1,
                GlobalVariables.MAX_NUM - 100 - 1,
                GlobalVariables.MAX_NUM - 50 - 1,
                GlobalVariables.MAX_NUM - 6 - 1,
                GlobalVariables.MAX_NUM - 1 - 1
            };

            plainText.ReadNum(plainTextPositionsOf1s);
            Console.Write("Encrypting the number: ");
            MathOps.PrintNumberInPowersOfTwo(plainText);

            BigNum cipherText = new BigNum();
            BigNum a          = new BigNum();
            BigNum n          = new BigNum();
            BigNum p          = new BigNum();
            BigNum q          = new BigNum();

            Console.WriteLine("Generating RSA");
            MathOps.GenRSA(b, a, p, q, n);
            Console.WriteLine("Encrypting plaintext to:");
            cipherText = MathOps.Encrypt(plainText, b, n);
            MathOps.PrintNumberInPowersOfTwo(cipherText);

            Console.WriteLine("Decrypting ciphertext to:");
            BigNum decryptedText = MathOps.Decrypt(cipherText, a, n);

            MathOps.PrintNumberInPowersOfTwo(decryptedText);

            plainText.CompNum(decryptedText);

            Console.WriteLine();
        }
Пример #5
0
        /// <summary>
        /// Gets BigNum type number and an index.
        /// Returns the sub number if the number by the index.
        /// Example: number = 0..001101101 (length of 400 bits), index = 398.
        /// The returned number will be 0..000110110.
        /// </summary>
        /// <param name="divided"></param>
        /// <param name="indexPos"></param>
        /// <returns></returns>
        public static BigNum GetSubBigNumFromLeftToIndex(BigNum divided, int indexPos)
        {
            BigNum     bigNumFromLeftToIndex = new BigNum();
            List <int> positionsOf1s         = new List <int>();
            int        initialIndexPos       = indexPos;

            while (indexPos >= divided.MSB1Location)
            {
                if (divided[indexPos] == 1)
                {
                    positionsOf1s.Add(GlobalVariables.MAX_NUM - (initialIndexPos - indexPos) - 1);
                }

                indexPos--;
            }

            bigNumFromLeftToIndex.ReadNum(positionsOf1s);
            return(bigNumFromLeftToIndex);
        }
Пример #6
0
        public static void A()
        {
            Console.WriteLine("(A) Using ReadNum methods.");
            Console.WriteLine("1. reading 0101");
            BigNum bigNum1a = new BigNum();

            bigNum1a.ReadNum("0101");
            Console.WriteLine(bigNum1a.WriteNum());
            Console.WriteLine($"The first 1' digit is in position: {bigNum1a.MSB1Location}");

            Console.WriteLine("2. reading positions of 1s. Getting a the list {399, 385}.");
            BigNum bigNum2a = new BigNum();

            bigNum2a.ReadNum(new List <int> {
                399, 385
            });
            Console.WriteLine(bigNum2a.WriteNum());
            Console.WriteLine($"The first 1' digit is in position: {bigNum2a.MSB1Location}");

            Console.WriteLine();
        }
Пример #7
0
        /// <summary>
        /// Gets BigNum number.
        /// Finds the index of the MSB 1 digit, run from it to the LSB.
        /// It has boolean variable <isSmaller> that set to true when the first 1's digits is cleared
        /// (set to 0).
        /// Unless <isSmaller> = true, this method cannot set any bit.
        /// Use random (IsGeneratedToSet() method) to set or clear that bit.
        /// </summary>
        /// <param name="top"></param>
        /// <returns></returns>
        private static BigNum GenerateNumberFromOneToGivenTop(BigNum top)
        {
            BigNum generatedNumber = new BigNum();

            if (top.MSB1Location.HasValue)
            {
                do
                {
                    bool       isSmaller         = false;
                    List <int> PositionsOf1sList = new List <int>();
                    for (int i = top.MSB1Location.Value; i < GlobalVariables.MAX_NUM; ++i)
                    {
                        if (top[i] == 1)
                        {
                            if (GenerateOneOrZero() == 0)
                            {
                                isSmaller = true;
                            }
                            else
                            {
                                PositionsOf1sList.Add(i);
                            }
                        }
                        else
                        {
                            if ((GenerateOneOrZero() == 1) && (isSmaller))
                            {
                                PositionsOf1sList.Add(i);
                            }
                        }
                    }

                    generatedNumber.ReadNum(PositionsOf1sList);
                } while ((generatedNumber.CompNumWithoutPrint(GlobalVariables.zero)) ||
                         (generatedNumber.CompNumWithoutPrint(top)));
            }

            return(generatedNumber);
        }
Пример #8
0
        /// <summary>
        /// O.
        /// Generates random prime number in the length of MAX_NUN/4.
        /// </summary>
        public static BigNum GenPrime()
        {
            BigNum     randomPrimeNumber = new BigNum();
            BigNum     top           = new BigNum();
            int        k             = 20;
            int        tryNumber     = 1;
            List <int> positionsOf1s = new List <int>();

            for (int i = 392; i < GlobalVariables.MAX_NUM; ++i)    // TODO change back.
            {
                positionsOf1s.Add(i);
            }

            top.ReadNum(positionsOf1s);
            Console.WriteLine("Generating random prime number...");
            do
            {
                Console.WriteLine($"try number: {tryNumber++}");
                randomPrimeNumber = GenerateNumberFromOneToGivenTop(top);
            } while (!IsPrime(randomPrimeNumber, k));

            return(randomPrimeNumber);
        }
Пример #9
0
        /// <summary>
        /// J.
        /// Gets two BigNum type numbers, divided and divider.
        /// Returns by reference the result - quotient and reminder.
        /// </summary>
        /// <param name="divider"></param>
        public static void DivNum(BigNum divided, BigNum divider, BigNum quotientRetrunByRef, BigNum reminderReturnByRef)
        {
            List <int> positionsOf1s = new List <int>();

            if (divided.MSB1Location.HasValue)
            {
                int indexPos = divided.MSB1Location.Value + divider.Length - 1;

                BigNum subResult = new BigNum(divided);
                while (indexPos < GlobalVariables.MAX_NUM)
                {
                    BigNum numberFromLeftToIndex = GetSubBigNumFromLeftToIndex(subResult, indexPos);

                    if (numberFromLeftToIndex >= divider)
                    {
                        // Saving quotient's info.
                        positionsOf1s.Add(indexPos);

                        // Subtract.
                        int    numberOfShifts   = GlobalVariables.MAX_NUM - indexPos - 1;
                        BigNum numberToSubtract = new BigNum(divider);
                        numberToSubtract.ShiftLeft(numberOfShifts);
                        subResult.SubNum(numberToSubtract);
                    }

                    indexPos++;
                }

                quotientRetrunByRef.ReadNum(positionsOf1s);
                reminderReturnByRef.Set(subResult);
            }
            else
            {
                Console.WriteLine("Cannot divide by zero");
            }
        }