Exemplo n.º 1
0
        static void Main(string[] args)
        {
            X509Certificate2 cert = GetCertificateFromStore("CN=RootCA");

            if (cert == null)
            {
                Console.WriteLine("Certificate");
                Console.ReadLine();
            }

            Console.WriteLine(GenerateSalt());
            string emri = "Fortan";

            emri = emri.Substring(0, 1).ToUpper() + emri.Substring(1);
            Console.WriteLine(emri);

            while (true)
            {
                UDPServer udpserver = new UDPServer("127.0.0.1", 12000);
                Console.WriteLine("Serveri eshte i gatshem per kerkesa");
                //Merr absolute path per fajllin e certifikates se serverit


                while (true)
                {
                    byte[] receivedata = udpserver.Receive();


                    string[] data = Encoding.UTF8.GetString(receivedata).Split('.');

                    for (int i = 0; i < data.Length; i++)
                    {
                        Console.WriteLine(data[i] + " Length= " + data[i].Length);
                    }
                    Console.WriteLine(data.Length);


                    int messageLength = data[2].Length;

                    byte[] message = new byte[messageLength];

                    int length = data[1].Length;
                    Console.WriteLine(length);
                    desKey = new byte[length];



                    desKey = DecryptDataOaepSha1(cert, Convert.FromBase64String(data[1]));
                    int ivlength = data[0].Length;

                    desIv = new byte[ivlength];

                    desIv = Convert.FromBase64String(data[0]);
                    Console.WriteLine("Gjatesia e pranuar" + data.Length);
                    Console.WriteLine(Convert.ToBase64String(desKey));

                    byte[] decryptedMessage = DekriptoDes(data[2]);

                    Console.WriteLine(Convert.ToBase64String(decryptedMessage));



                    string[] tedhenat = Encoding.UTF8.GetString(decryptedMessage).Split(':');

                    Console.WriteLine(tedhenat.Length);
                    if (tedhenat.Length > 3)
                    {
                        if (handleRegistration(tedhenat))
                        {
                            serverMessage = "OK";
                        }


                        else
                        {
                            serverMessage = "Error";
                        }
                        udpserver.Send(Enkripto(serverMessage));
                    }
                    else
                    {
                        if (handleLogin(tedhenat))
                        {
                            serverMessage = "OK";
                        }
                        else
                        {
                            serverMessage = "Error";
                        }
                        udpserver.Send(Enkripto(serverMessage));
                    }
                }
            }
        }