Exemplo n.º 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;
            }
        }
Exemplo n.º 2
0
        }// 1

        public void SendMessageFromSocket(int port, string IP, string operation)
        {
            byte[]      bytes      = new byte[1024];
            IPHostEntry ipHost     = Dns.GetHostEntry(IP);
            IPAddress   ipAddr     = ipHost.AddressList[0];
            IPEndPoint  ipEndPoint = new IPEndPoint(ipAddr, port);

            Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            string Message = InByte(MessageTextBox.Text);

            LEN_OPEN_KEY_CLIENT = MessageTextBox.Text.Length;


            sender.Connect(ipEndPoint);
            string infstring = "mes ";

            byte[] infbyte = Encoding.UTF8.GetBytes(infstring);
            int    byteinf = sender.Send(infbyte);

            string OPENkey_Client = GenerateRandomNumbers.CheckIt((BigInteger.Parse(GenerateRandomNumbers.GenerateRandomNumber(LEN_OPEN_KEY_CLIENT)))).ToString();

            byte[] openkeybyte        = Encoding.UTF8.GetBytes(OPENkey_Client);
            int    bytesopenkeyToSend = sender.Send(openkeybyte);

            byte[] bytes2 = new byte[1024];

            int    bytesRec       = sender.Receive(bytes2);
            string OPENkey_Server = "";

            OPENkey_Server += Encoding.UTF8.GetString(bytes2, 0, bytesRec);
            string CLOSEkey_Client = GenerateRandomNumbers.CheckIt((BigInteger.Parse(GenerateRandomNumbers.GenerateRandomNumber(LEN_CLOSE_KEY_CLIENT)))).ToString();
            string B = BigInteger.ModPow(BigInteger.Parse(OPENkey_Server), BigInteger.Parse(CLOSEkey_Client), BigInteger.Parse(OPENkey_Client)).ToString();

            byte[] Bbyte        = Encoding.UTF8.GetBytes(B);
            int    bytesBToSend = sender.Send(Bbyte);

            byte[] bytes3 = new byte[1024];
            int    bytesA = sender.Receive(bytes3);
            string A      = "";

            A += Encoding.UTF8.GetString(bytes3, 0, bytesA);
            string KEY = InByte(BigInteger.ModPow(BigInteger.Parse(A), BigInteger.Parse(CLOSEkey_Client), BigInteger.Parse(OPENkey_Client)).ToString());
            //string Message = InByte(MessageTextBox.Text);
            string output = XOR(Message, KEY);

            byte[] Messagebyte        = Encoding.UTF8.GetBytes(output);
            int    bytesMessageToSend = sender.Send(Messagebyte);

            //byte[] bytes4 = new byte[1024];
            //int bytesAnswer = sender.Receive(bytes4);
            // string Answer = "";
            // Answer += Encoding.UTF8.GetString(bytes4, 0, bytesAnswer);
            //MessageBox.Show(Answer);

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