Exemplo n.º 1
0
        Int64 CreateKeys(int number)
        {
            Console.WriteLine("Создание общего ключа № {0}...", number);
            Random random = new Random();
            Int64  g      = LongArithmetic.NextInt64(random);
            Int64  p      = LongArithmetic.NextInt64(random);

            Console.WriteLine("Отправка параметров g = " + g + " p = " + p);
            string message = "";

            message = "g = " + g.ToString() + " p = " + p.ToString();
            SendInformation(message, stream);

            Int64      a = LongArithmetic.NextInt64(random);
            BigInteger A = BigInteger.ModPow(g, a, p);

            Console.WriteLine("Отправка параметра A = " + A);
            message = "A = " + A;
            SendInformation(message, stream);

            message  = ReceiveInformation(stream);
            message += ' ';
            BigInteger B = BigInteger.Parse(LongArithmetic.GiveLong(ref message));

            Console.WriteLine("Получен параметр B = {0}", B);

            Int64 Key = Convert.ToInt64(BigInteger.ModPow(B, a, p).ToString());

            Key = LongArithmetic.GetFormatKey(Key);
            Console.WriteLine("Получен общий ключ № {0}: Key = {1}", number, Key);
            var buffer = new byte[8];

            buffer = BitConverter.GetBytes(Key);
            return(Key);
        }
Exemplo n.º 2
0
        static Int64 CreateKey(int number)
        {
            Int64  g = 0, p = 0, b = 0, Key = 0;
            string message = "";

            for (int i = 0; i < 3; i++)
            {
                switch (i)
                {
                case 0:
                    message  = ReceiveInformation(stream);
                    message += ' ';
                    g        = Convert.ToInt64(LongArithmetic.GiveLong(ref message));
                    p        = Convert.ToInt64(LongArithmetic.GiveLong(ref message));
                    Console.WriteLine("Получены параметры g = {0} и p = {1}", g, p);

                    Random random = new Random();
                    b = LongArithmetic.NextInt64(random);
                    BigInteger B = BigInteger.ModPow(g, b, p);
                    Console.WriteLine("Отправка параметра B = " + B);
                    message = "B = " + B;
                    SendInformation(message, stream);
                    break;

                case 1:
                    message  = ReceiveInformation(stream);
                    message += ' ';
                    BigInteger A = BigInteger.Parse(LongArithmetic.GiveLong(ref message));
                    Console.WriteLine("Получен параметр A = {0}", A);
                    Key = Convert.ToInt64(BigInteger.ModPow(A, b, p).ToString());
                    Key = LongArithmetic.GetFormatKey(Key);
                    Console.WriteLine("Получен общий ключ № {0}: Key = {1}", number, Key);
                    break;
                }
            }
            return(Key);
        }