Beispiel #1
0
        private void f3_buttonSetKeys_Click(object sender, EventArgs e)
        {
            f3_textP.Text = new string(f3_textP.Text.Where(t => char.IsDigit(t)).ToArray());
            f3_textQ.Text = new string(f3_textQ.Text.Where(t => char.IsDigit(t)).ToArray());
            f3_textG.Text = new string(f3_textG.Text.Where(t => char.IsDigit(t)).ToArray());

            BigInteger p = 0, q = 0, g = 0;

            if (f3_textP.TextLength > 0 && f3_textQ.TextLength > 0 && f3_textG.TextLength > 0 &&
                RabinMiller.IsPrime(p = BigInteger.Parse(f3_textP.Text), 10) &&
                RabinMiller.IsPrime(q = BigInteger.Parse(f3_textQ.Text), 10) &&
                ((g = BigInteger.Parse(f3_textG.Text)) >= 2) && g < p - 1 && NumberTheory.BinaryModPow(g, q, p) != 1)
            {
                DiffieHellman.Params.P = p;
                DiffieHellman.Params.Q = q;
                DiffieHellman.Params.G = g;

                f3_buttonSetKeys.Enabled       = false;
                f3_buttonClearKeys.Enabled     = true;
                f3_buttonNextPrime.Enabled     = false;
                f3_buttonNextGenerator.Enabled = false;

                f3_textQ.ReadOnly = true;
                f3_textG.ReadOnly = true;

                f3_button_SetSecretA.Enabled       = true;
                f3_button_GetRandomSecretA.Enabled = true;
                f3_textSecretA.ReadOnly            = false;
                f3_button_SetSecretB.Enabled       = true;
                f3_button_GetRandomSecretB.Enabled = true;
                f3_textSecretB.ReadOnly            = false;
            }
        }
Beispiel #2
0
        private void f9_buttonNextPrimeP_Click(object sender, EventArgs e)
        {
            f9_textP.Text = new string(f9_textP.Text.Where(t => char.IsDigit(t)).ToArray());

            BigInteger ParamP;

            if (f9_textP.TextLength > 0)
            {
                ParamP = BigInteger.Parse(f9_textP.Text);
            }
            else
            {
                ParamP = 0;
            }
            if (++ParamP % 2 == 0)
            {
                ParamP++;
            }
            while (!RabinMiller.IsPrime(ParamP, 3))
            {
                ParamP += 2;
            }

            f9_textP.Text = Convert.ToString(ParamP);
        }
Beispiel #3
0
        private void f3_buttonNextPrime_Click(object sender, EventArgs e)
        {
            f3_textQ.Text = new string(f3_textQ.Text.Where(t => char.IsDigit(t)).ToArray());
            BigInteger ParamP, ParamQ;

            if (f3_textQ.TextLength > 0)
            {
                ParamQ = BigInteger.Parse(f3_textQ.Text);
            }
            else
            {
                ParamQ = 0;
            }
            if (++ParamQ % 2 == 0)
            {
                ParamQ++;
            }
            while (!RabinMiller.IsPrime(ParamQ, 3) || !RabinMiller.IsPrime(ParamP = 2 * ParamQ + 1, 3))
            {
                ParamQ += 2;
            }

            f3_textP.Text = Convert.ToString(ParamP);
            f3_textQ.Text = Convert.ToString(ParamQ);
        }
Beispiel #4
0
        private void f3_buttonNextGenerator_Click(object sender, EventArgs e)
        {
            f3_textP.Text = new string(f3_textP.Text.Where(t => char.IsDigit(t)).ToArray());
            f3_textQ.Text = new string(f3_textQ.Text.Where(t => char.IsDigit(t)).ToArray());
            f3_textG.Text = new string(f3_textG.Text.Where(t => char.IsDigit(t)).ToArray());

            BigInteger ParamP, ParamQ, ParamG = 0;

            if (f3_textP.TextLength > 0 && f3_textQ.TextLength > 0 && RabinMiller.IsPrime(ParamP = BigInteger.Parse(f3_textP.Text), 3) &&
                RabinMiller.IsPrime(ParamQ = BigInteger.Parse(f3_textQ.Text), 3))
            {
                RandomNumberGenerator rng = RandomNumberGenerator.Create();
                while (ParamG < 2 || ParamG >= ParamP - 1 || NumberTheory.BinaryModPow(ParamG, ParamQ, ParamP) == 1)
                {
                    ParamG = BigIntegerRandomUtils.RandomInRange(rng, 2, ParamP - 1);
                }
                f3_textG.Text = Convert.ToString(ParamG);
            }
        }
Beispiel #5
0
        private void f5_buttonSetKeys_Click(object sender, EventArgs e)
        {
            f5_textP.Text = new string(f5_textP.Text.Where(t => char.IsDigit(t)).ToArray());
            f5_textQ.Text = new string(f5_textQ.Text.Where(t => char.IsDigit(t)).ToArray());
            f5_textG.Text = new string(f5_textG.Text.Where(t => char.IsDigit(t)).ToArray());

            BigInteger p = 0, q = 0, g = 0;

            if (f5_textP.TextLength > 0 && f5_textQ.TextLength > 0 && f5_textG.TextLength > 0 &&
                RabinMiller.IsPrime(p = BigInteger.Parse(f5_textP.Text), 10) &&
                RabinMiller.IsPrime(q = BigInteger.Parse(f5_textQ.Text), 10) &&
                ((g = BigInteger.Parse(f5_textG.Text)) >= 2) && g < p - 1 && NumberTheory.BinaryModPow(g, q, p) != 1)
            {
                if (p > MyAlphabets.AllTypesTogether.Length)
                {
                    DiffieHellman.Params.P = p;
                    DiffieHellman.Params.Q = q;
                    DiffieHellman.Params.G = g;

                    f5_buttonSetKeys.Enabled       = false;
                    f5_buttonClearKeys.Enabled     = true;
                    f5_buttonNextPrime.Enabled     = false;
                    f5_buttonNextGenerator.Enabled = false;

                    f5_textQ.ReadOnly = true;
                    f5_textG.ReadOnly = true;

                    f5_button_SetSecretA.Enabled       = true;
                    f5_button_GetRandomSecretA.Enabled = true;
                    f5_textSecretA.ReadOnly            = false;
                    f5_button_SetSecretB.Enabled       = true;
                    f5_button_GetRandomSecretB.Enabled = true;
                    f5_textSecretB.ReadOnly            = false;
                }
                else
                {
                    MessageBox.Show("Пожалуйста, выберите значение P больше длины используемого алфавита (" + Convert.ToString(MyAlphabets.AllTypesTogether.Length) + " симв.)");
                }
            }
        }
Beispiel #6
0
        private void f4_buttonSetKeys_Click(object sender, EventArgs e)
        {
            f4_textP.Text = new string(f4_textP.Text.Where(t => char.IsDigit(t)).ToArray());

            if (f4_textP.TextLength > 0)
            {
                if (RabinMiller.IsPrime(BigInteger.Parse(f4_textP.Text), 10))
                {
                    f4_textP.ReadOnly                = true;
                    f4_buttonNextPrime.Enabled       = false;
                    f4_buttonSetKeys.Enabled         = false;
                    f4_buttonClearKeys.Enabled       = true;
                    f4_textCa.ReadOnly               = false;
                    f4_textCb.ReadOnly               = false;
                    f4_textBox_MessageAlice.ReadOnly = false;
                    f4_button_GetRandomCa.Enabled    = true;
                    f4_button_GetRandomCb.Enabled    = true;
                    f4_button_SetCa.Enabled          = true;
                    f4_button_SetCb.Enabled          = true;
                }
            }
        }
Beispiel #7
0
        private void f9_buttonSetParams_Click(object sender, EventArgs e)
        {
            f9_textP.Text = new string(f9_textP.Text.Where(t => char.IsDigit(t)).ToArray());
            f9_textQ.Text = new string(f9_textQ.Text.Where(t => char.IsDigit(t)).ToArray());

            if (f9_textP.TextLength > 0 && f9_textQ.TextLength > 0)
            {
                if (RabinMiller.IsPrime(BigInteger.Parse(f9_textP.Text), 10) && RabinMiller.IsPrime(BigInteger.Parse(f9_textQ.Text), 10))
                {
                    if (f9_chooseHashFun.Text != "")
                    {
                        f9_chooseHashFun.Enabled     = false;
                        f9_textP.ReadOnly            = true;
                        f9_textQ.ReadOnly            = true;
                        f9_textN.Text                = Convert.ToString(BigInteger.Parse(f9_textP.Text) * BigInteger.Parse(f9_textQ.Text));
                        f9_buttonNextPrimeP.Enabled  = false;
                        f9_buttonNextPrimeQ.Enabled  = false;
                        f9_buttonSetParams.Enabled   = false;
                        f9_buttonClearParams.Enabled = true;
                    }
                }
            }
        }
Beispiel #8
0
        private async Task Computations()
        {
            Busy.Visibility = Visibility.Visible;

            PrimeNumberState result;

            var token = _token.Token;

            switch (_options.Type)
            {
            case PrimalityTestType.Aks:
                result = await Task.Run(() => AksTest.IsPrime(_number, token), token);

                break;

            case PrimalityTestType.Fermat:
            {
                var k = GetNumberOfIterations();
                if (k == -1)
                {
                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                result = await FermatTest.IsPrime(_number, k, token);
            }
            break;

            case PrimalityTestType.LucasLehmer:
                result = await LucasLehmerTest.IsPrime(_number, token);

                break;

            case PrimalityTestType.Pepin:
                result = await Task.Run(() => PepinTest.IsPrime(_number), token);

                break;

            case PrimalityTestType.RabinMiller:
            {
                var k = GetNumberOfIterations();
                if (k == -1)
                {
                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                result = await RabinMiller.IsPrime(_number, k, token);
            }
            break;

            case PrimalityTestType.SolovayStrassen:
            {
                if (!long.TryParse(_number.ToString(), out var num))
                {
                    MessageBox.Show("This number too big to use this method.\nPlease select another primality test.",
                                    "Result", MessageBoxButton.OK);


                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                var k = GetNumberOfIterations();
                if (k == -1)
                {
                    Busy.Visibility = Visibility.Hidden;
                    return;
                }

                result = await SolovayStrassen.IsPrime(num, k, token);

                break;
            }

            default:
            {
                Busy.Visibility = Visibility.Hidden;
                return;
            }
            }

            MessageBox.Show($"Your number is {Enum.GetName(typeof(PrimeNumberState), result)}", "Result");

            Busy.Visibility = Visibility.Hidden;
        }