コード例 #1
0
        public BigInteger generateP()
        {
            BigInteger currentP;

            do
            {
                rsa.q    = generateQ();
                currentP = BigInteger.ProbablePrime(264, new Random());
            } while (!currentP.IsProbablePrime(50) && evklid.gcd(currentP, rsa.q) == BigInteger.One);
            return(currentP);
        }
コード例 #2
0
        private void generate()
        {
            EvklidBigInteger evklid = new EvklidBigInteger();

            do
            {
                server.q = BigInteger.ProbablePrime(server.bitLength, new Random());
            } while (!server.q.IsProbablePrime(10));

            do
            {
                server.p = BigInteger.ProbablePrime(server.bitLength, new Random());
            } while (!server.p.IsProbablePrime(10));

            server.n = server.q.Multiply(server.p);

            server.f = (server.p.Subtract(BigInteger.One).Multiply(server.q.Subtract(BigInteger.One)));

            while (true)
            {
                server.d = BigInteger.ProbablePrime(server.bitLength, new Random());
                if (evklid.gcd(server.d, server.f).CompareTo(BigInteger.One) == 0)
                {
                    if (evklid.mY.CompareTo(BigInteger.Zero) < 0)
                    {
                        server.c = evklid.mY.Add(server.f);
                    }
                    else
                    {
                        server.c = evklid.mY;
                    }
                    break;
                }
            }
        }
コード例 #3
0
        public void generateSV()
        {
            EvklidBigInteger evklid = new EvklidBigInteger();
            BigInteger       tmp_s;

            do
            {
                tmp_s = BigInteger.ProbablePrime(bitLength, new Random());
            } while (!tmp_s.IsProbablePrime(10) && evklid.gcd(tmp_s, n).CompareTo(BigInteger.One) != 0);
            s = tmp_s;
            v = MyModPowBigInteger.FastModuloExponentiation(s, BigInteger.Two, n);
        }
コード例 #4
0
        /*
         * Generate P and Q
         */
        private void generate()
        {
            EvklidBigInteger evklid = new EvklidBigInteger();
            BigInteger       tmp_p, tmp_q;

            do
            {
                do
                {
                    tmp_p = BigInteger.ProbablePrime(bitLength, new Random());
                    tmp_q = BigInteger.ProbablePrime(bitLength, new Random());
                } while (!tmp_p.IsProbablePrime(10) && !tmp_q.IsProbablePrime(10));
            } while (evklid.gcd(tmp_q, tmp_p).CompareTo(BigInteger.One) != 0);
            p = tmp_p;
            q = tmp_q;
            n = q.Multiply(p);
            t = 10;
        }
コード例 #5
0
        public Player(BigInteger p_prev)
        {
            EvklidBigInteger evklid = new EvklidBigInteger();

            do
            {
                c = BigInteger.ProbablePrime(bitLength, new Random());
                if (evklid.gcd(c, p_prev).CompareTo(BigInteger.One) == 0)
                {
                    if (evklid.mY.CompareTo(BigInteger.Zero) < 0)
                    {
                        d = evklid.mY.Add(p_prev);
                    }
                    else
                    {
                        d = evklid.mY;
                    }
                    break;
                }
            } while (true);
        }
コード例 #6
0
        protected void Vote()
        {
            EvklidBigInteger evklid = new EvklidBigInteger();

            if (string.IsNullOrEmpty(voice.name))
            {
                titleName   = "Введите имя";
                checkResult = "Голосуйте, мы вам не мешаем";
                destruct();
                return;
            }
            else
            {
                titleName = "Здравствуйте, " + voice.name;
            }

            if (Voise == 0)
            {
                Text = "Введите свой голос!";
                return;
            }

            string filename = "Resource/people.txt";

            try {
                using (StreamReader reader = new StreamReader(filename)) {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (voice.name == line)
                        {
                            checkResult = "Не голосуйте повторно";
                            return;
                        }
                    }
                    reader.Close();
                }
                using (StreamWriter writer = new StreamWriter(filename, true)) {
                    string line = voice.name + "\n";
                    writer.Write(line);
                    writer.Close();
                }
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }


            voice.bitLength = 512;
            voice.n         = BigInteger.ProbablePrime(voice.bitLength, new Random());

            int h_key = key[Voise].GetHashCode();

            voice.n = voice.n.Add(new BigInteger(h_key.ToString()));

            BigInteger tmp_r;

            do
            {
                tmp_r = BigInteger.ProbablePrime(32, new Random());
            } while (evklid.gcd(tmp_r, server.getN()).CompareTo(BigInteger.One) != 0);
            voice.r = tmp_r;

            voice.h  = new BigInteger(Math.Abs(voice.n.GetHashCode()) + "");
            voice._h = voice.h.Multiply(MyModPowBigInteger.FastModuloExponentiation(voice.r, server.getD(), server.getN()));

            voice._s = server.getTicket(voice._h);
            voice.s  = voice._s.Multiply(voice.r.ModInverse(server.getN()));


            if (server.checkVoice(voice.n, voice.s))
            {
                checkResult = "Благодарим, вы успешно проголосовали";
            }
            else
            {
                checkResult = "Ошибка";
            }

            filename = "Resource/resultVoises.txt";
            try {
                using (StreamReader reader = new StreamReader(filename)) {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string lineInput = voice.n.ToString() + " " + voice.s.ToString();
                        if (lineInput == line)
                        {
                            checkResult = "У разных Имен совпали подписи";
                            return;
                        }
                    }
                    reader.Close();
                }
                using (StreamWriter writer = new StreamWriter(filename, true)) {
                    string line = voice.n + " " + voice.s + "\n";
                    writer.Write(line);
                    writer.Close();
                }
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
コード例 #7
0
        public void generate()
        {
            SHA256 sha256 = SHA256.Create();

            elGamal.h = new BigInteger(sha256.ComputeHash(Document.Data));
            if (elGamal.h.SignValue < 0)
            {
                elGamal.h = elGamal.h.Abs();
            }

            BigInteger currentQ;
            BigInteger currentP;

            do
            {
                currentQ = BigInteger.ProbablePrime(264, new Random());
                currentP = currentQ.Multiply(BigInteger.Two);
                currentP = currentP.Add(BigInteger.One);
            } while (!currentP.IsProbablePrime(50));

            elGamal.p      = currentP;
            elGamal.p_prev = elGamal.p.Subtract(BigInteger.One);
            elGamal.q      = currentQ;

            for (BigInteger i = BigInteger.Two; i.CompareTo(elGamal.p_prev) < 0; i = i.Add(BigInteger.One))
            {
                if (i.ModPow(elGamal.q, elGamal.p).CompareTo(BigInteger.One) != 0)
                {
                    elGamal.g = i;
                    break;
                }
            }

            elGamal.x = new BigInteger(256, new Random());
            elGamal.y = elGamal.g.ModPow(elGamal.x, elGamal.p);

            BigInteger currentK;

            do
            {
                currentK = new BigInteger(256, new Random());
            } while (currentK.Gcd(elGamal.p_prev).CompareTo(BigInteger.One) != 0);
            elGamal.k = currentK;

            elGamal.r = elGamal.g.ModPow(elGamal.k, elGamal.p);
            BigInteger tmp_hxr = elGamal.h.Subtract(elGamal.x.Multiply(elGamal.r));

            elGamal.u = tmp_hxr.Mod(elGamal.p_prev);

            check = evklid.gcd(elGamal.k, elGamal.p_prev);
            if (evklid.mY.CompareTo(BigInteger.Zero) < 0)
            {
                elGamal.k_revers = evklid.mY.Add(elGamal.p_prev);
            }
            else
            {
                elGamal.k_revers = evklid.mY;
            }

            BigInteger tmp_k_revers_u = elGamal.k_revers.Multiply(elGamal.u);

            elGamal.s = tmp_k_revers_u.Mod(elGamal.p_prev);

            BigInteger tmp_yr  = elGamal.y.ModPow(elGamal.r, elGamal.p);
            BigInteger tmp_rs  = elGamal.r.ModPow(elGamal.s, elGamal.p);
            BigInteger tmp_yrs = tmp_yr.Multiply(tmp_rs).Mod(elGamal.p);
            BigInteger tmp_ghp = elGamal.g.ModPow(elGamal.h, elGamal.p);

            check_left  = tmp_yrs;
            check_right = tmp_ghp;
        }