Esempio n. 1
0
        public static BigInteger2 inverseMod(BigInteger2 numbe, BigInteger2 modf)
        {
            //Check to see that gcd is 1 before proceeding: gcd(numbe,modf)=1
            ArrayList   factors2     = new ArrayList();
            int         count        = 0;
            BigInteger2 NUM          = (BigInteger2)numbe.Clone();
            BigInteger2 MOD          = (BigInteger2)modf.Clone();
            bool        foundInverse = true;

            while (true)
            {
                BigInteger2[] temp = BigIntegerExtensions.DivideBy(modf, numbe);

                Factors2 x = new Factors2();
                x.Mod   = modf;
                x.fMod  = (BigInteger2)BigInteger2.ONE().Clone();
                x.fnumb = BigInteger2.NegateZeros((BigInteger2)temp[0].Clone());
                x.fnumb.bitlength[0] = true;

                if (count == 0)
                {
                    factors2.Add(x); //added only in first instance
                }
                count++;             //iteration number
                //Look up for previous remainder for factor substitution in x:
                if (count == 2)
                {
                    Factors2 prevx = (Factors2)factors2[0];

                    //Check for Factors2 to incorporate!!
                    Factors2 tmpResolve = new Factors2();

                    Factors2 resCurr = new Factors2();
                    resCurr.fnumb = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevx.fnumb));
                    resCurr.fMod  = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevx.fMod));

                    tmpResolve.fnumb = x.fMod + resCurr.fnumb;
                    tmpResolve.fMod  = resCurr.fMod;
                    factors2.Add(tmpResolve);
                }
                if (count > 2)
                {
                    Factors2 prev2Back = (Factors2)factors2[factors2.Count - 2];
                    Factors2 prevBack  = (Factors2)factors2[factors2.Count - 1];
                    Factors2 resCurr   = new Factors2();

                    resCurr.fnumb = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevBack.fnumb));
                    resCurr.fMod  = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevBack.fMod));

                    resCurr.fnumb = prev2Back.fnumb + resCurr.fnumb;
                    resCurr.fMod  = prev2Back.fMod + resCurr.fMod;

                    factors2.Add(resCurr);
                }
                if (temp[1] == BigInteger2.ONE())
                {
                    break;
                }
                if (temp[1] == new BigInteger2(1, 0))
                {
                    foundInverse = false;
                    break;
                }
                modf  = (BigInteger2)numbe.Clone();
                numbe = (BigInteger2)temp[1].Clone();
            }

            if (!foundInverse)
            {
                return(new BigInteger2(1, 0));
            }
            //Calculate inverse mod
            Factors2    result = (Factors2)factors2[count - 1];
            BigInteger2 invres = result.fnumb;

            if (!invres.bitlength[0])
            {
                return((BigIntegerExtensions.DivideBy(invres, MOD))[1]);                     //if invres is positive
            }
            else // invres is negative
            {
                invres.bitlength[0] = false;
                if (invres < MOD)
                {
                    return(BigInteger2.NegateZeros(MOD - invres));
                }
                else
                {
                    BigInteger2 test = BigIntegerExtensions.DivideBy(invres, MOD)[0] + BigInteger2.ONE();
                    return(BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(test, MOD) - MOD));
                }
            }
        }
Esempio n. 2
0
        public void generateNewKey()
        {
            var testMessage = "[Administrator] wrote: ==>I am fed up of taking tests where you sore highly yet fail to progress to the next rounds of interviews. Given to abnormalities stated as team fit or cultural fit." + System.Environment.NewLine + " How can you say such crap, when you've never seen me sitted in your teams!!. Please visit and support the secure cryptology aspects of the ethical world, without the likes of Secret Service tampering or peeking through your mail: https://martinlayooinc.co.uk is therefore open to do business with you." + System.Environment.NewLine +
                              "So I have taken up my projects under my own company umbrella, and winning: https://www.martinlayooinc.co.uk/Home/Product?prodId=checkMutableUber" + System.Environment.NewLine +
                              " If in doubt of the buy and immediate download procedures or unsure whether you are spending money on value, then check out free samples and engage in the buying process for 0.01 - a penny.How can you discredit that.https://www.martinlayooinc.co.uk/Home/Product?prodId=checkAES" + System.Environment.NewLine +
                              " Feel free to test drive my apps.Yes, to survive I am running my own business as I am fed up of spurious claims from employers amidst interviewing processes viewable link below this";

            var testMessage2 = "If in doubt of the buy and immediate download procedures or unsure whether you are spending money on value, then check out free samples and engage in the buying process for 0.01 - a penny.How can you discredit that.https://www.martinlayooinc.co.uk/Home/Product?prodId=checkAES";

            MessageBox.Show("RSA Public & Private Key Pair For Signing Is About to Get Generated. Please Click Ok, and Wait...\nYou will be notified when Keys are generated to Continue Process.");
            DateTime startTime = DateTime.Now;
            var      tasks     = new List <Task>();
            Task     tp        = new TaskFactory().StartNew(() =>
            {
                p = BigIntegerExtensions.getProbablePrime(num2);//should be a prime number
                p = BigInteger2.NegateZeros(p);
                GC.Collect();
            });

            tasks.Add(tp);
            //MessageBox.Show("P Got:");

            Task tq = new TaskFactory().StartNew(() =>
            {
                q = BigIntegerExtensions.getProbablePrime(num1);//should be a prime number
                q = BigInteger2.NegateZeros(q);
                //MessageBox.Show("p and q determined!!");
                GC.Collect();
            });

            tasks.Add(tq);
            Task.WaitAll(tasks.ToArray());

            Task tm = new TaskFactory().StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        while (p.gcd(p, q) != BigInteger2.ONE())
                        {
                            q = BigIntegerExtensions.getProbablePrime(num1);//should be a prime number
                            q = BigInteger2.NegateZeros(q);
                            //MessageBox.Show("p and q determined!!");
                            GC.Collect();
                        }
                        Mod = p * q;

                        //MessageBox.Show("p Is:");
                        //MessageBox.Show(p.ToString());

                        //MessageBox.Show("q Is:");
                        //MessageBox.Show(q.ToString());
                        Mod = BigInteger2.NegateZeros(Mod);
                        //MessageBox.Show("Mod Is:");
                        //MessageBox.Show(Mod.ToString());
                        n = Mod.bitlength.Length - 2;
                        GC.Collect();
                        BigInteger2 px = p - BigInteger2.ONE();
                        BigInteger2 qx = q - BigInteger2.ONE();
                        BigInteger2 f  = px * qx;
                        f = BigInteger2.NegateZeros(f);

                        GC.Collect();

                        while (true)
                        {
                            e = BigIntegerExtensions.getProbablePrime(n - 1);
                            e = BigInteger2.NegateZeros(e);
                            d = e.inverseMod(e, f);
                            if (d == new BigInteger2(1, 0))
                            {
                                continue;
                            }
                            d = BigInteger2.NegateZeros(d);
                            break;
                        }

                        //MessageBox.Show("Found decryption Key");

                        //MessageBox.Show("Encryption Key Got:");
                        // MessageBox.Show(e.ToString());

                        //MessageBox.Show("Decryption Key Got:");
                        //MessageBox.Show(d.ToString());

                        GC.Collect();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Exception:" + ex.Message);
                    }
                    try
                    {
                        var resultBytes     = Encrypt(testMessage);
                        var encryptedString = Convert.ToBase64String(resultBytes);
                        var decryptBytes    = Decrypt(encryptedString);
                        var decryptedString = ASCIIEncoding.UTF8.GetString(decryptBytes);
                        var isKeys          = false;

                        if (decryptedString.Equals(testMessage))
                        {
                            isKeys = true;
                        }
                        else
                        {
                            isKeys = false;
                        }
                        if (!isKeys)
                        {
                            continue;
                        }

                        resultBytes     = Encrypt(testMessage2);
                        encryptedString = Convert.ToBase64String(resultBytes);
                        decryptBytes    = Decrypt(encryptedString);
                        decryptedString = ASCIIEncoding.UTF8.GetString(decryptBytes);

                        if (decryptedString.Equals(testMessage2))
                        {
                            DateTime endTime = DateTime.Now;
                            TimeSpan tspan   = endTime - startTime;
                            MessageBox.Show(String.Format("Keys Set!! Key generation Time elapsed=>{0} Hours: {1} Minutes: {2} Seconds", tspan.Hours, tspan.Minutes, tspan.Seconds));

                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message + System.Environment.NewLine + e.StackTrace);
                    }
                }
            });

            Task.WaitAny(tm);
        }
Esempio n. 3
0
        //MillerRabinTest
        public static bool MillerRabinIsPrime(BigInteger2 numb)
        {
            //Use Rabin's Test suite a.modPower(d,numb)== 1 : then numb is prime else numb composite

            BigInteger2 n         = (BigInteger2)numb.Clone();
            BigInteger2 numbLess1 = n - BigInteger2.ONE();

            BigInteger2[] testSuite = new BigInteger2[3];

            //Fill testSuite with BigInteger2 values:
            //Including 5, 11, and 61

            testSuite[0]           = new BigInteger2(1, 0);
            testSuite[0].bitlength = BigInteger2.ConvertToBinary(61, 6);
            testSuite[1]           = new BigInteger2(1, 0);
            testSuite[1].bitlength = BigInteger2.ConvertToBinary(31, 6);
            testSuite[2]           = new BigInteger2(1, 0);
            testSuite[2].bitlength = BigInteger2.ConvertToBinary(11, 4);

            // Determine two.power(r) factor of q:
            // By: using q is least set bit referenced from zero
            int i = 0, j = 0;

            for (i = numbLess1.bitlength.Length - 1, j = 0; i >= 2; i--, j++)
            {
                if (numbLess1.bitlength[i])
                {
                    break;
                }
            }
            //Console.Out.WriteLine("j: {0}", j.ToString());
            //Form component two.power(r) = twoPowerR
            BigInteger2 twoPowerS = BigInteger2.TWO().power(j);

            //Calculate d:
            BigInteger2 d         = BigIntegerExtensions.DivideBy(numbLess1, twoPowerS)[0];

            //Use testSuite to eliminate Composites:
            bool isPrime = true;

            foreach (BigInteger2 integer in testSuite)
            {
                bool        prime  = false;
                BigInteger2 result = BigIntegerExtensions.modPower(integer, d, numb);
                if (result == BigInteger2.ONE())
                {
                    prime = true;
                    continue;
                }
                else
                {
                    for (i = 0; i < j; i++)
                    {
                        if ((numb - result) == BigInteger2.ONE())
                        {
                            prime = true;
                            break;
                        }
                        result = BigIntegerExtensions.modPower(result, BigInteger2.TWO(), numb);
                    }
                    if (prime)
                    {
                        continue;
                    }
                    else
                    {
                        isPrime = false;
                        break;
                    }
                }
            }
            return(isPrime);
        }