Пример #1
0
        private static void StartServer2()
        {
            listen2.Start();
            try
            {
                while (true)
                {
                    Console.WriteLine("127.0.0.1:12346: started (ENCRYPT)...");
                    TcpClient client = listen2.AcceptTcpClient();

                    using (NetworkStream stream = client.GetStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string message = reader.ReadLine();
                            Console.WriteLine("Получено: " + message);
                            var sEncrypted = Sha.Encrypt(message.Split(' ')[0], message.Split(' ')[1]);
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                writer.WriteLine(sEncrypted);
                            }
                        }
                    }
                    Thread.Sleep(500);
                    client.Close();
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
        }
Пример #2
0
        public static string Encrypt(string plainText, string password)
        {
            if (plainText == null)
            {
                return(null);
            }

            if (password == null)
            {
                password = String.Empty;
            }

            var bytesToBeEncrypted = Encoding.UTF8.GetBytes(plainText);
            var passwordBytes      = Encoding.UTF8.GetBytes(password);

            // Hash the password with SHA256
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            var bytesEncrypted = Sha.Encrypt(bytesToBeEncrypted, passwordBytes);

            return(Convert.ToBase64String(bytesEncrypted));
        }