Esempio n. 1
0
        public static BigInteger2[] DivideBy(BigInteger2 Numb1, BigInteger2 Numb2)
        {
            var numb1 = BigInteger2.GetBytesTwosCompliment(Numb1);
            var numb2 = BigInteger2.GetBytesTwosCompliment(Numb2);

            BigInteger n1 = new BigInteger(numb1);
            BigInteger n2 = new BigInteger(numb2);
            BigInteger quot;
            BigInteger result = BigInteger.DivRem(n1, n2, out quot);

            BigInteger2[] results = new BigInteger2[2];

            var bitAry = BigInteger2.ReverseBigIntegerBitsToBitArray(result.ToByteArray());
            var whole  = new BigInteger2(bitAry.Length, 0);

            BigInteger2.CopyFromBitArrayFromIndex(bitAry, whole, 0);

            var bitAry2  = BigInteger2.ReverseBigIntegerBitsToBitArray(quot.ToByteArray());
            var quotient = new BigInteger2(bitAry2.Length, 0);

            BigInteger2.CopyFromBitArrayFromIndex(bitAry2, quotient, 0);

            results[0] = BigInteger2.NegateZeros(whole);
            results[1] = BigInteger2.NegateZeros(quotient);

            return(results);
        }
Esempio n. 2
0
        public void setPrivateKey(byte[] key)
        {
            BitArray bitArray = new BitArray(key);

            d = new BigInteger2(bitArray.Length, 0);
            BigInteger2.CopyFromBitArrayFromIndex(bitArray, d, 0);
            d = BigInteger2.NegateZeros(d);
        }
Esempio n. 3
0
        public void setPublicKey(byte[] key)
        {
            BitArray bitArray = new BitArray(key);

            e = new BigInteger2(bitArray.Length, 0);
            BigInteger2.CopyFromBitArrayFromIndex(bitArray, e, 0);
            e = BigInteger2.NegateZeros(e);
        }
Esempio n. 4
0
        public byte[] DeInject(BigInteger2 decryptedMessageBits)
        {
            BitArray bitArray1 = BigInteger2.BigIntegerToBitArrayNoShrinkSize(decryptedMessageBits);

            var bitArray2 = PerformDeInjection(bitArray1);

            byte[] numArray = new byte[(bitArray2.Length - 1) / 8 + 1];
            bitArray2.CopyTo(numArray, 0);
            return(numArray);
        }
Esempio n. 5
0
        public void setModValue(byte[] value)
        {
            BitArray bitArray = new BitArray(value);

            Mod = new BigInteger2(bitArray.Length, 0);
            BigInteger2.CopyFromBitArrayFromIndex(bitArray, Mod, 0);
            Mod = BigInteger2.NegateZeros(Mod);
            n   = Mod.bitlength.Length - 2;
            //MessageBox.Show(""+(Mod.bitlength.Length - 2));
        }
Esempio n. 6
0
        //私钥加密
        private static byte[] priEncrypt(byte[] block, RSACryptoServiceProvider key)
        {
            RSAParameters param    = key.ExportParameters(true);
            BigInteger2   d        = new BigInteger2(param.D);
            BigInteger2   n        = new BigInteger2(param.Modulus);
            BigInteger2   biText   = new BigInteger2(block);
            BigInteger2   biEnText = biText.modPow(d, n);

            return(biEnText.getBytes());
        }
Esempio n. 7
0
        public byte[] Decrypt(String str)
        {
            ArrayList resultSet = new ArrayList();

            BigInteger2[] message = splitMessage(Convert.FromBase64String(str), n, "decrypt");

            GC.Collect();
            for (int i = 0; i < message.Length; i++)
            {
                //message[i] = BigInteger2.NegateZeros(message[i]);
                BigInteger2 unitResult = BigIntegerExtensions.modPower(message[i], d, Mod);
                unitResult = BigInteger2.NegateZeros(unitResult);
                BigInteger2 result = new BigInteger2(unitResult.bitlength.Length - 3, 0);
                for (int x = 3, xj = 2; x < unitResult.bitlength.Length; x++, xj++)
                {
                    result.bitlength[xj] = unitResult.bitlength[x];
                }
                unitResult = result;
                resultSet.Add(unitResult);
                GC.Collect();
            }

            //Create Full BigInteger2 containing bits from resultSet BigIntegers

            int bitSize = 0;

            foreach (BigInteger2 b in resultSet)
            {
                bitSize += (b.bitlength.Length - 2);
            }
            //MessageBox.Show("Whole decrypted Text length: " + bitSize);
            BigInteger2 wholeMessage = new BigInteger2(bitSize, 0);

            //Fill wholeMessage
            int curIndex = 2;

            foreach (BigInteger2 b in resultSet)
            {
                for (int i = 2; i < b.bitlength.Length; i++, curIndex++)
                {
                    wholeMessage.bitlength[curIndex] = b.bitlength[i];
                }
            }
            byte[] res = DeInject(wholeMessage);
            GC.Collect();
            // MessageBox.Show("Decryption Completed!!!");
            return(res);
        }
Esempio n. 8
0
        public static BigInteger2 gcd(BigInteger2 Numb1, BigInteger2 Numb2)
        {
            var numb1 = BigInteger2.GetBytesTwosCompliment(Numb1);
            var numb2 = BigInteger2.GetBytesTwosCompliment(Numb2);

            BigInteger n1 = new BigInteger(numb1);
            BigInteger n2 = new BigInteger(numb2);

            var result = BigInteger.GreatestCommonDivisor(n1, n2);

            var bitAry = BigInteger2.ReverseBigIntegerBitsToBitArray(result.ToByteArray());
            var whole  = new BigInteger2(bitAry.Length, 0);

            BigInteger2.CopyFromBitArrayFromIndex(bitAry, whole, 0);
            return(BigInteger2.NegateZeros(whole));
        }
Esempio n. 9
0
        public static BigInteger2 modPower(BigInteger2 current, BigInteger2 power, BigInteger2 mod)
        {
            var numb1 = BigInteger2.GetBytesTwosCompliment(current);
            var numb2 = BigInteger2.GetBytesTwosCompliment(power);
            var numb3 = BigInteger2.GetBytesTwosCompliment(mod);

            BigInteger n1 = new BigInteger(numb1);
            BigInteger n2 = new BigInteger(numb2);
            BigInteger n3 = new BigInteger(numb3);

            var result = BigInteger.ModPow(n1, n2, n3);

            var bitAry = BigInteger2.ReverseBigIntegerBitsToBitArray(result.ToByteArray());
            var whole  = new BigInteger2(bitAry.Length, 0);

            BigInteger2.CopyFromBitArrayFromIndex(bitAry, whole, 0);

            return(BigInteger2.NegateZeros(whole));
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public byte[] Encrypt(String str)
        {
            BigInteger2[] message = this.splitMessage(Encoding.UTF8.GetBytes(str), this.n - 1, "encrypt");
            mess = new BigInteger2[message.Length];
            mess = message;
            //MessageBox.Show("Message Length: " + message.Length);
            //return data;
            ArrayList resultSet = new ArrayList();

            GC.Collect();
            //Encrypt each Message and populate resultSet
            for (int i = 0; i < message.Length; i++)
            {
                BigInteger2 unitResult = BigIntegerExtensions.modPower(message[i], e, Mod);
                unitResult = BigInteger2.NegateZeros(unitResult);

                if (i == 0 && unitResult.bitlength.Length - 2 < n)
                {
                    //MessageBox.Show("Padding with 'a'");
                    int         ind = n + 4 - unitResult.bitlength.Length;
                    BigInteger2 tmp = new BigInteger2(n, 0);

                    for (int xi = ind, xj = 2; xj < unitResult.bitlength.Length; xi++, xj++)
                    {
                        tmp.bitlength[xi] = unitResult.bitlength[xj];
                    }
                    unitResult = tmp;
                    //PAD Left with byte 'a'
                    byte[]   pad    = ASCIIEncoding.ASCII.GetBytes("a");
                    BitArray padAry = new BitArray(pad);

                    //PAD left of unitResult with padAry
                    BigInteger2 padded = new BigInteger2(unitResult.bitlength.Length - 2 + padAry.Length, 0);
                    int         pi, pj;
                    for (pi = 2, pj = 0; pj < padAry.Length; pi++, pj++)
                    {
                        padded.bitlength[pi] = padAry[pj];
                    }

                    //Continue filling from unitResult
                    for (int indi = pi, indj = 2; indi < padded.bitlength.Length; indi++, indj++)
                    {
                        padded.bitlength[indi] = unitResult.bitlength[indj];
                    }
                    unitResult = padded;
                }
                if (i > 0 && unitResult.bitlength.Length - 2 < n)
                {
                    //MessageBox.Show("Padding with zeros");
                    //pad result's leftside with zeros
                    int         ind = n + 4 - unitResult.bitlength.Length;
                    BigInteger2 tmp = new BigInteger2(n, 0);
                    for (int xi = ind, xj = 2; xj < unitResult.bitlength.Length; xi++, xj++)
                    {
                        tmp.bitlength[xi] = unitResult.bitlength[xj];
                    }

                    unitResult = null;
                    unitResult = tmp;
                }

                GC.Collect();
                resultSet.Add(unitResult);
            }

            //Create Full BigInteger2 containing bits from resultSet BigIntegers

            int bitSize = 0;

            foreach (BigInteger2 b in resultSet)
            {
                bitSize += (b.bitlength.Length - 2);
            }

            BigInteger2 wholeMessage = new BigInteger2(bitSize, 0);

            //Fill wholeMessage
            int curIndex = 2;

            foreach (BigInteger2 b in resultSet)
            {
                for (int i = 2; i < b.bitlength.Length; i++, curIndex++)
                {
                    wholeMessage.bitlength[curIndex] = b.bitlength[i];
                }
            }

            byte[] result = BigInteger2.getKeyBytes(wholeMessage);
            GC.Collect();
            // MessageBox.Show("Encryption Completed!!!");
            return(result);
        }
Esempio n. 12
0
        public BigInteger2[] splitMessage(byte[] wholeMesg, int mesgCellBits, string direction)
        {
            BigInteger2[] result = null;

            if (direction.Equals("decrypt"))
            {
                BitArray fullMesgArry = new BitArray(wholeMesg);
                int      wholeBits    = wholeMesg.Length * 8;

                int cells = 0;

                //remove front few zeros of padding
                BitArray firstByte = new BitArray(8, false);
                for (int m = 0; m < firstByte.Length; m++)
                {
                    firstByte[m] = fullMesgArry[m];
                }
                int x = 0;
                for (x = 0; x < firstByte.Length; x++)
                {
                    if (firstByte[x])
                    {
                        break;
                    }
                }
                //value of first x zero values to be deleted
                BitArray temp = new BitArray(wholeBits - x, false);

                //Fill the temp array
                int ix, jx;
                for (ix = x, jx = 0; ix < fullMesgArry.Length; ix++, jx++)
                {
                    temp[jx] = fullMesgArry[ix];
                }
                fullMesgArry = null;
                fullMesgArry = temp;

                //First Test to see whether 1st byte=='a'
                BitArray pad = new BitArray(8, false);

                //fill pad with first byte of wholeMesg
                for (int pi = 0; pi < 8; pi++)
                {
                    pad[pi] = fullMesgArry[pi];
                }

                BigInteger2 padInteger = new BigInteger2(8, 0);
                for (int pi = 2, pj = 0; pi < padInteger.bitlength.Length; pi++, pj++)
                {
                    padInteger.bitlength[pi] = pad[pj];
                }


                byte[] padbytes = BigInteger2.getBytes(padInteger);
                if (ASCIIEncoding.ASCII.GetBytes("a")[0] == padbytes[0])
                {
                    //MessageBox.Show("I am in removing 'a' byte from padding");
                    //Strip the first byte of fullMsgAry
                    BitArray fullAry = new BitArray(fullMesgArry.Length - 8, false);
                    for (int pi = 8, pj = 0; pi < fullMesgArry.Length; pi++, pj++)
                    {
                        fullAry[pj] = fullMesgArry[pi];
                    }
                    fullMesgArry = fullAry;
                }

                wholeBits = fullMesgArry.Length;
                cells     = wholeBits / mesgCellBits;

                int remBits = wholeBits % mesgCellBits;

                //number of BigInteger2 arry
                int numbOfFullCells = cells;
                int fullBits        = cells * mesgCellBits;
                if (remBits > 0)
                {
                    cells += 1;
                    //MessageBox.Show("Added 1 cell for remaining bits");
                }

                result = new BigInteger2[cells];

                //Fill cells:
                int i        = 0;
                int curIndex = 2;
                int curCell  = 0;

                if (numbOfFullCells > 0)
                {
                    //MessageBox.Show("I am in Full Cells");
                    BigInteger2 cellBgInt = new BigInteger2(mesgCellBits, 0);
                    for (i = 0; i < fullBits; i++, curIndex++)
                    {
                        cellBgInt.bitlength[curIndex] = fullMesgArry.Get(i);
                        if (curIndex == mesgCellBits + 1)
                        {
                            result[curCell] = cellBgInt;
                            curCell++;
                            curIndex  = 1;
                            cellBgInt = new BigInteger2(mesgCellBits, 0);
                        }
                    }
                }

                //Fill last Cell!!!
                if (remBits > 0)
                {
                    //MessageBox.Show("I am in remaining Cells");
                    BigInteger2 cellBgInt = new BigInteger2(remBits, 0);
                    for (int n = i, j = 2; n < fullMesgArry.Length; n++, j++)
                    {
                        cellBgInt.bitlength[j] = fullMesgArry.Get(n);
                    }
                    result[result.Length - 1] = cellBgInt;
                }

                GC.Collect();
            }
            else//encrypt
            {
                var list = GetBoolListFromBytes(wholeMesg);

                BitArray fullMesgArry = new BitArray(list.Count);
                for (var index = 0; index < fullMesgArry.Length; index++)
                {
                    fullMesgArry[index] = list[index];
                }

                int wholeBits = fullMesgArry.Count;

                int cells = 0;
                cells = wholeBits / (mesgCellBits - 1);

                int remBits = wholeBits % (mesgCellBits - 1);

                //number of BigInteger2 arry
                int numbOfFullCells = cells;
                int fullBits        = cells * (mesgCellBits - 1);
                if (remBits > 0)
                {
                    cells += 1;
                }

                result = new BigInteger2[cells];

                //Fill cells:
                int i        = 0;
                int curIndex = 3;
                int curCell  = 0;

                if (numbOfFullCells > 0)
                {
                    //MessageBox.Show("I am in Full Cells");
                    BigInteger2 cellBgInt = new BigInteger2(mesgCellBits, 0);
                    try
                    {
                        for (i = 0; i < fullBits; i++, curIndex++)
                        {
                            cellBgInt.bitlength[curIndex] = fullMesgArry.Get(i);
                            if (curIndex == mesgCellBits + 1)
                            {
                                cellBgInt.bitlength[2] = true;
                                result[curCell]        = cellBgInt;
                                curCell++;
                                curIndex  = 2;
                                cellBgInt = new BigInteger2(mesgCellBits, 0);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Happens in 1st: " + ex.Message);
                    }
                }

                //Fill last Cell!!!
                if (remBits > 0)
                {
                    //MessageBox.Show("I am in remaining Cells");
                    BigInteger2 cellBgInt = new BigInteger2(remBits + 1, 0);
                    cellBgInt.bitlength[2] = true;
                    try
                    {
                        for (int n = i, j = 3; n < fullMesgArry.Length; n++, j++)
                        {
                            cellBgInt.bitlength[j] = fullMesgArry.Get(n);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Exception happens in 2nd: " + ex.Message);
                    }
                    result[result.Length - 1] = cellBgInt;
                }
                GC.Collect();
            }

            return(result);
        }
Esempio n. 13
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. 14
0
 public byte[] getModValue()
 {
     return(BigInteger2.getKeyBytes(Mod));
 }
Esempio n. 15
0
 public byte[] getPrivateKey()
 {
     d = BigInteger2.NegateZeros(d);
     return(BigInteger2.getKeyBytes(d));
 }
Esempio n. 16
0
        public static BigInteger2 getProbablePrime(int n)
        {
            BigInteger2 num     = new BigInteger2(n);
            BigInteger2 zero    = BigInteger2.Zero();
            BigInteger2 temp    = new BigInteger2(1, 0);
            BigInteger2 nem     = ((BigInteger2)num.Clone()) + BigInteger2.T50();
            bool        success = true;
            int         ni      = 0;

            while (true)
            {
                ni++;
                Console.Out.WriteLine(ni);
                if (num < nem || num == nem)
                {
                    foreach (int i in mySet)
                    {
                        bool[] val = BigInteger2.ConvertToBinary(i, 11);
                        bool[] t   = new bool[val.Length];
                        for (int x = 0; x < val.Length; x++)
                        {
                            t[x] = (val[x] ? true : false);
                        }
                        temp.bitlength = t;
                        if (BigIntegerExtensions.DivideBy(num, temp)[1] == zero)
                        {
                            success = false;
                            break;
                        }
                    }
                }
                if (success && BigIntegerExtensions.MillerRabinIsPrime(num))
                {
                    break;
                }
                if (num == nem)
                {
                    success = true;
                    var skip = DateTime.Now.Millisecond % 5;
                    switch (skip)
                    {
                    case 0:

                        num = num + BigInteger2.TWO();
                        nem = nem + BigInteger2.T50();
                        break;

                    case 1:
                        num = num + BigInteger2.FOUR();
                        nem = nem + BigInteger2.T100();
                        break;

                    case 2:
                        num = num + BigInteger2.T16();
                        nem = nem + BigInteger2.T100();
                        break;

                    case 3:
                        num = num + BigInteger2.T32();
                        nem = nem + BigInteger2.T100();
                        break;

                    case 4:
                        num = num + BigInteger2.T50();
                        nem = nem + BigInteger2.T100();
                        break;
                    }
                }
                else
                {
                    success = true;
                    num     = num + BigInteger2.TWO();
                }
            }
            return(num);
        }
Esempio n. 17
0
 public byte[] getPublicKey()
 {
     e = BigInteger2.NegateZeros(e);
     return(BigInteger2.getKeyBytes(e));
 }
Esempio n. 18
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));
                }
            }
        }