Exemplo n.º 1
0
        private static void StartServer3()
        {
            listen3.Start();
            try
            {
                while (true)
                {
                    Console.WriteLine("127.0.0.1:12347: started (DECRYPT)...");
                    TcpClient client = listen3.AcceptTcpClient();
                    using (NetworkStream stream = client.GetStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string message = reader.ReadLine();
                            Console.WriteLine("Получено: " + message);

                            var sDecrypted = Sha.Decrypt(message.Split(' ')[0], message.Split(' ')[1]);
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                writer.WriteLine(sDecrypted);
                            }
                        }
                    }
                    Thread.Sleep(500);
                    client.Close();
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Decrypt a string.
        /// </summary>
        /// <param name="encryptedText">String to be decrypted</param>
        /// <param name="password">Password used during encryption</param>
        /// <exception cref="FormatException"></exception>
        public static string Decrypt(string encryptedText, string password)
        {
            if (encryptedText == null)
            {
                return(null);
            }

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

            // Get the bytes of the string
            var bytesToBeDecrypted = Convert.FromBase64String(encryptedText);
            var passwordBytes      = Encoding.UTF8.GetBytes(password);

            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            var bytesDecrypted = Sha.Decrypt(bytesToBeDecrypted, passwordBytes);

            return(Encoding.UTF8.GetString(bytesDecrypted));
        }
Exemplo n.º 3
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));
        }