示例#1
0
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            UdpClient klienti   = new UdpClient();
            Socket    newSocket = new Socket(AddressFamily.InterNetwork,
                                             SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ep         = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12000);
            EndPoint   tempRemote = (EndPoint)ep;

            klienti.Connect(ep);

            byte[] bytesend = Encoding.ASCII.GetBytes(textBoxUsername.Text + ' ' + textBoxPassword.Text);

            klienti.Send(bytesend, bytesend.Length);



            byte[] receivedData = klienti.Receive(ref ep);
            if (string.Equals(Encoding.ASCII.GetString(receivedData), "Wrong password/username"))
            {
                textBoxUsername.Text = "";
                textBoxPassword.Text = "";
                Console.WriteLine(Encoding.ASCII.GetString(receivedData));
                grade.Text = Encoding.ASCII.GetString(receivedData);
            }

            Console.WriteLine(Encoding.ASCII.GetString(receivedData));
            grade.Text = Encoding.ASCII.GetString(receivedData);

            X509Certificate2 cert = GetCertificateFromStore("CN=RootCA");

            if (cert == null)
            {
                Console.WriteLine("Certificate 'CN=CERT_SIGN_TEST_CERT' not found.");
                Console.ReadLine();
            }

            if (Validate())
            {
                string email    = textBoxUsername.Text.Trim();
                string password = textBoxPassword.Text.Trim();
                DES    des      = new DES();


                string mesazhi = email + ":" + password + ":" + "blla";
                Console.WriteLine(mesazhi);
                byte[] encrytedData = des.Enkripto(mesazhi);

                byte[] IV  = des.getIV();
                byte[] key = des.getKey();


                byte[] encryptedKey = EncryptDataOaepSha1(cert, key);

                Console.WriteLine(encryptedKey.Length);
                Console.WriteLine(Convert.ToBase64String(encryptedKey));

                Console.WriteLine(Convert.ToBase64String(key));
                Console.WriteLine(Convert.ToBase64String(DecryptDataOaepSha1(cert, encryptedKey)));



                string delimiter            = ".";
                string fullmessageEncrypted = Convert.ToBase64String(IV) + delimiter + delimiter + Convert.ToBase64String(encrytedData);



                Console.WriteLine("qa qova :" + fullmessageEncrypted.Length);
                Console.WriteLine("IV:" + Convert.ToBase64String(IV));
                Console.WriteLine("Qelsi: " + Convert.ToBase64String(encryptedKey));
                Console.WriteLine("Mesazhi: " + Convert.ToBase64String(encrytedData));

                if (Encoding.UTF8.GetString(des.Dekripto(Convert.ToBase64String(receivedData))).Substring(0, 2) == "OK")
                {
                    MessageBox.Show("Successful Login");
                }
                else
                {
                    MessageBox.Show("Login failed");
                }
            }
        }