Example #1
0
File: RSA.cs Project: why1799/RSA
    public static bool ferma(LongArithmetic x)
    {
        //Random rnd = new Random();
        LaggedFibRandom rnd = new LaggedFibRandom((int)(DateTime.UtcNow.ToBinary() % 1000000000));

        for (int j = 2; j < 100; j++)
        {
            if (x % j == 0 && x != j)
            {
                return(false);
            }
        }

        for (int i = 0; i < 20; i++)
        {
            LongArithmetic a = (rnd.Next() % (x - 2)) + 2;

            if (gcd(a, x) != 1)
            {
                return(false);
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Проверка {0} на простоту. Тест {1}:", x.ToString(), i + 1);
            Console.ForegroundColor = ConsoleColor.White;

            if (mypows(a, x - 1, x) != 1)
            {
                return(false);
            }
        }
        return(true);
    }
Example #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            int            bits   = int.Parse(comboBox1.Text.Substring(0, comboBox1.Text.IndexOf(' ')));
            LongArithmetic bitsla = 1;

            for (int i = 0; i < bits; i++)
            {
                bitsla *= 2;
            }

            global::RSA.Times = bitsla.ToString().Count() / _base;

            LongArithmetic p = GetRandom("p");

            Console.WriteLine();
            LongArithmetic q = GetRandom("q");

            Console.WriteLine();

            textBox1.Text           = p.ToString();
            textBox2.Text           = q.ToString();
            Console.ForegroundColor = ConsoleColor.Red;
            textBox3.Text           = (p * q).ToString();
            Console.WriteLine("n=" + textBox3.Text);

            LongArithmetic fn = (p - 1) * (q - 1);

            Console.WriteLine("ф(n)=" + fn.ToString());
            textBox4.Text = fn.ToString();

            LongArithmetic _e = 1, d = 1;
            bool           got = false;

            LongArithmetic x = new LongArithmetic();
            LongArithmetic y = new LongArithmetic();

            global::RSA.Times = global::RSA.Times * 5 / 4;

            Console.WriteLine("Вычисление e и k:");
            while (!got)
            {
                _e = global::RSA.Random();

                LongArithmetic z = global::RSA.egcd(fn, _e, ref x, ref y);

                if (z == 1 && y > 0 && (_e * y) % fn == 1)
                {
                    d = y;
                    //Console.WriteLine(d * _e % fn);
                    got = true;
                }
            }

            if (!got)
            {
                Console.WriteLine("Вычислить e и k не удалось:");
            }
            else
            {
                Console.WriteLine("e={0}", _e);
                Console.WriteLine("k={0}", d);

                textBox5.Text = _e.ToString();
                textBox6.Text = d.ToString();
            }
        }
Example #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("Введите n");
                return;
            }

            if (textBox5.Text == "")
            {
                MessageBox.Show("Введите e");
                return;
            }

            LongArithmetic _n = new LongArithmetic();
            LongArithmetic _e = new LongArithmetic();

            try
            {
                _n = LongArithmetic.Parse(textBox3.Text);
                _e = LongArithmetic.Parse(textBox5.Text);
            }
            catch
            {
                MessageBox.Show("Числа невозможно считать!");
                return;
            }

            Console.WriteLine("Начало шифрования!");

            StringBuilder sb = new StringBuilder();

            List <ushort>  list        = new List <ushort>();
            LongArithmetic text        = new LongArithmetic();
            LongArithmetic criptedtext = new LongArithmetic();

            richTextBox2.Clear();
            foreach (var c in richTextBox1.Text)
            {
                if ((int)c > 9999 || (int)c < 0)
                {
                    MessageBox.Show("Этот текст содержить невозможные для чтения символы!");
                    return;
                }
                list.Add((ushort)c);
                if (list.Count == 6)
                {
                    text        = new LongArithmetic(list);
                    criptedtext = global::RSA.mypows(text, _e, _n);
                    richTextBox2.AppendText(criptedtext.ToString() + "\n");
                    list.Clear();
                }
            }

            if (list.Count > 0)
            {
                text        = new LongArithmetic(list);
                criptedtext = global::RSA.mypows(text, _e, _n);
                richTextBox2.AppendText(criptedtext.ToString() + "\n");
            }
            Console.WriteLine("Шифровка закончена!");
        }