コード例 #1
0
        static void gen_keys()
        {
            Random random = new Random();

            a_p = 2;

            A_p = g.Pow(a_p).Mod(q);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string server = "127.0.0.1";

            Int32         port   = 13000;
            TcpClient     client = new TcpClient(server, port);
            NetworkStream stream = client.GetStream();

            g = new Org.BouncyCastle.Math.BigInteger(2.ToString());
            p = new Org.BouncyCastle.Math.BigInteger(31.ToString());
            q = new Org.BouncyCastle.Math.BigInteger(5.ToString());
            int n = Int32.Parse(args[0]);

            gen_keys();

            Org.BouncyCastle.Math.BigInteger[] P = req_keys(n - 1);

            int[] V = gen_v(n - 1);



            //Start time
            DateTime now = DateTime.Now;

            Console.WriteLine("Strat Second: {0}", now.Millisecond);


            Random random = new Random();
            int    s      = 4;

            //int s = 29;
            Org.BouncyCastle.Math.BigInteger U = (g.Pow(s)).Mod(p);
            for (int i = 0; i < n - 1; ++i)
            {
                U = U.Multiply((P[i].Pow(V[i])).Mod(p)).Mod(p);
            }

            byte[] bytes;

            bytes = new byte[204800];
            bytes = Encoding.UTF8.GetBytes(U.ToString());
            stream.Write(bytes);

            stream.Flush();

            bytes = new byte[64];
            stream.Read(bytes, 0, bytes.Length);

            int c = Int32.Parse(Encoding.UTF8.GetString(bytes));


            int v_p = c;

            for (int i = 0; i < n - 1; ++i)
            {
                v_p = v_p ^ V[i];
            }


            string V_str = v_p + "|";

            for (int i = 0; i < n - 1; ++i)
            {
                V_str += V[i] + "|";
            }

            string P_str = A_p.ToString() + "|";

            for (int i = 0; i < n - 1; ++i)
            {
                P_str += P[i] + "|";
            }

            Org.BouncyCastle.Math.BigInteger a_p_big = new Org.BouncyCastle.Math.BigInteger(a_p.ToString());
            Org.BouncyCastle.Math.BigInteger v_p_big = new Org.BouncyCastle.Math.BigInteger(v_p.ToString());

            Org.BouncyCastle.Math.BigInteger s_big = new Org.BouncyCastle.Math.BigInteger(s.ToString());

            Org.BouncyCastle.Math.BigInteger r_big = (s_big.Add((a_p_big.Multiply(v_p_big)).Negate())).Mod(q);
            //int r = (s - ((a_p * v_p) % 31));
            //int r = Int32.Parse(r_big.ToString());

            string r_str = r_big.ToString();

            string msg = V_str + P_str + r_str;

            bytes = new byte[204800];
            bytes = Encoding.UTF8.GetBytes(msg);
            stream.Write(bytes);

            stream.Flush();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            int n = Int32.Parse(args[0]);

            g = new Org.BouncyCastle.Math.BigInteger(2.ToString());
            p = new Org.BouncyCastle.Math.BigInteger(31.ToString());
            q = new Org.BouncyCastle.Math.BigInteger(5.ToString());

            TcpListener server    = null;
            Int32       port      = 13000;
            IPAddress   localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data


            TcpClient     client = server.AcceptTcpClient();
            NetworkStream stream = client.GetStream();

            byte[] bytes;

            bytes = new byte[204800];
            stream.Read(bytes, 0, bytes.Length);

            string U = Encoding.UTF8.GetString(bytes);


            Random random = new Random();
            int    c      = random.Next(1, 4);

            bytes = new byte[64];
            bytes = Encoding.UTF8.GetBytes(c.ToString());
            stream.Write(bytes);

            stream.Flush();


            bytes = new byte[204800];
            stream.Read(bytes, 0, bytes.Length);

            string msg = Encoding.UTF8.GetString(bytes);

            string[] temp_split = msg.Split("|");

            int[] V  = new int[n];
            int   c0 = 0;

            for (int i = 0; i < n; ++i)
            {
                V[i] = Int32.Parse(temp_split[i]);
                c0   = c0 ^ V[i];
            }


            Org.BouncyCastle.Math.BigInteger[] P = new Org.BouncyCastle.Math.BigInteger[n];
            for (int i = 0; i < n; ++i)
            {
                P[i] = new Org.BouncyCastle.Math.BigInteger(temp_split[n + i]);
            }

            int r = Int32.Parse(temp_split[2 * n]);

            if (c0 == c)
            {
                Console.WriteLine("1st verification PASS");
            }
            else
            {
                Console.WriteLine("1st verification FAIL");
            }

            Org.BouncyCastle.Math.BigInteger U0 = (g.Pow(r)).Mod(p);
            for (int i = 0; i < n; ++i)
            {
                U0 = U0.Multiply((P[i].Pow(V[i])).Mod(p)).Mod(p);
            }
            Console.WriteLine(U);
            Console.WriteLine(U0);

            string U1   = U0.ToString();
            bool   flag = true;

            for (int i = 0; i < U1.Length; ++i)
            {
                if (U1[i] != U[i])
                {
                    Console.WriteLine("2nd verification FAIL");
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                Console.WriteLine("2nd verification PASS");
            }
            //End time

            DateTime now = DateTime.Now;

            Console.WriteLine("Strat Second: {0}", now.Millisecond);
        }