示例#1
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);
        }