예제 #1
0
        void RSAwork(BigInteger lenghtP, BigInteger lenghtQ)
        {
            E      = 0; N = 0; FunctionOfEuler = 0; P = 0; Q = 0; TestNumber = 0; testik = "";
            testik = GenerateRandomNumbers.GenerateRandomNumber(lenghtP);
            P      = GenerateRandomNumbers.CheckIt(BigInteger.Parse(testik));
            testik = GenerateRandomNumbers.GenerateRandomNumber(lenghtQ);
            Q      = GenerateRandomNumbers.CheckIt(BigInteger.Parse(testik));
            if (P == Q)
            {
                Q = GenerateRandomNumbers.CheckIt(BigInteger.Parse(testik + 1));
            }
            N = Q * P;
            FunctionOfEuler = (P - 1) * (Q - 1);

            testik = GenerateRandomNumbers.GenerateRandomNumber((N.ToString()).Length / 3);

            E = GenerateRandomNumbers.CheckIt(BigInteger.Parse(testik));
            while ((E == P) && (E == Q))
            {
                E = GenerateRandomNumbers.CheckIt(E + 1);
            }
            D = Euclid.ToCountNod(E, FunctionOfEuler);
            if (D < 0)
            {
                D = D + FunctionOfEuler;
            }
        }
예제 #2
0
        public void ExecuteThreadServer()
        {
            // Устанавливаем для сокета локальную конечную точку

            // Назначаем сокет локальной конечной точке и слушаем входящие сокеты

            //try
            //{
            // Начинаем слушать соединения
            sListener.Bind(ipEndPoint);
            sListener.Listen(10);
            //Socket handler = sListener.Accept();

            //textBoxServer.Text = "Ожидаем соединение через порт " + ipEndPoint;
            while (true)
            {
                Socket handler = sListener.Accept();
                string data    = null;

                byte[] bytes    = new byte[1024];
                int    bytesRec = handler.Receive(bytes);

                data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
                // Показываем данные на консоли
                InfLoginPassword = data;
                //Action action = () => textBoxClient.Text = InfLoginPassword;
                //if (InvokeRequired)
                //{
                //    Invoke(action);
                //}
                //else { action(); }
                // MessageBox.Show(InfLoginPassword);



                //делим пароль и логин
                string Inf      = "";
                string Login    = "";
                string Password = "";
                int    i        = 0;
                while (data[i] != ' ')
                {
                    Inf += data[i];
                    i++;
                }
                i++;

                if (Inf == "mes")
                {
                    string OPENkey_Server = GenerateRandomNumbers.CheckIt((BigInteger.Parse(GenerateRandomNumbers.GenerateRandomNumber(LEN_OPEN_KEY_Server)))).ToString();
                    byte[] openkeybyte    = Encoding.UTF8.GetBytes(OPENkey_Server);
                    handler.Send(openkeybyte);

                    byte[] byteOPENkey_Client = new byte[1024];
                    int    bytesOPENkey       = handler.Receive(byteOPENkey_Client);
                    string OPENkey_Client     = "";
                    OPENkey_Client += Encoding.UTF8.GetString(byteOPENkey_Client, 0, bytesOPENkey);

                    string CLOSEkey_Server = GenerateRandomNumbers.CheckIt((BigInteger.Parse(GenerateRandomNumbers.GenerateRandomNumber(LEN_CLOSE_KEY_SERVER)))).ToString();
                    string A     = BigInteger.ModPow(BigInteger.Parse(OPENkey_Server), BigInteger.Parse(CLOSEkey_Server), BigInteger.Parse(OPENkey_Client)).ToString();
                    byte[] Abyte = Encoding.UTF8.GetBytes(A);
                    handler.Send(Abyte);

                    byte[] byteB  = new byte[1024];
                    int    bytesB = handler.Receive(byteB);
                    string B      = "";
                    B += Encoding.UTF8.GetString(byteB, 0, bytesB);

                    string KEY             = InByte(BigInteger.ModPow(BigInteger.Parse(B), BigInteger.Parse(CLOSEkey_Server), BigInteger.Parse(OPENkey_Client)).ToString());
                    byte[] byteMessageCode = new byte[65536];
                    //byte[] byteMessageCode = new byte[1024];
                    int    bytesMessageCode = handler.Receive(byteMessageCode);
                    string MessageCode      = "";
                    MessageCode += Encoding.UTF8.GetString(byteMessageCode, 0, bytesMessageCode);

                    string Messagecode = XOR(MessageCode, KEY);
                    string Message     = "";
                    for (int j = 0; j < Messagecode.Length; j = j + 16)
                    {
                        string word = "";
                        for (int k = 0; k < 16; k++)
                        {
                            word += Messagecode[k + j];
                        }
                        int number = Conv(word);
                        Message += Convert.ToChar(number);
                    }
                    Action action2 = () => textBoxClient.Text = Message;
                    if (InvokeRequired)
                    {
                        Invoke(action2);
                    }
                    else
                    {
                        action2();
                    }
                    //MessageBox.Show(Message);
                }
                if (Inf == "aut")
                {
                    while (data[i] != ' ')
                    {
                        Login += data[i];
                        i++;
                    }
                    i++;

                    Random     rand    = new Random();
                    BigInteger lenghtA = 0;
                    lenghtA = rand.Next(1, SIZE_N);
                    string N = GenerateRandomNumbers.GenerateRandomNumber(lenghtA);

                    if (ExistLogin(Login))
                    {
                        byte[] msg = Encoding.UTF8.GetBytes(N);
                        handler.Send(msg);
                        byte[] NPassword      = new byte[1024];
                        int    bytesNPassword = handler.Receive(NPassword);
                        string NPasswordHash  = "";
                        NPasswordHash += Encoding.UTF8.GetString(NPassword, 0, bytesNPassword);
                        if (ExistPassword(Login, N, NPasswordHash))
                        {
                            string reply = "Вы вошли в свой аккаунт!"; byte[] aut = Encoding.UTF8.GetBytes(reply);
                            handler.Send(aut);
                        }
                        else
                        {
                            string reply = "Пароль не верен!"; byte[] aut = Encoding.UTF8.GetBytes(reply);
                            handler.Send(aut);
                        }
                    }
                    else
                    {
                        string reply = "Пользователя не существует! Зарегистрируйтесь!";
                        byte[] msg   = Encoding.UTF8.GetBytes(reply);
                        handler.Send(msg);
                    }
                }
                if (Inf == "reg")
                {
                    while (data[i] != ' ')
                    {
                        Login += data[i];
                        i++;
                    }
                    i++;
                    if (ExistLogin(Login))
                    {
                        string reply = "Пользователь уже существует!";
                        byte[] msg   = Encoding.UTF8.GetBytes(reply);
                        handler.Send(msg);
                    }
                    else
                    {
                        for (int j = i; j < data.Length; j++)
                        {
                            Password += data[j];
                        }
                        Registration(Login, Password, handler);
                        string reply = "Регистрация проршла успешно!!";
                        byte[] msg   = Encoding.UTF8.GetBytes(reply);
                        handler.Send(msg);
                    }
                }

                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }

            //    handler.Shutdown(SocketShutdown.Both);
            //    handler.Close();
            //}
            //finally { }
        }