示例#1
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);
        }
示例#2
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);
        }
示例#3
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);
        }
示例#4
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));
                }
            }
        }
示例#5
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);
        }
示例#6
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);
        }