-1

I am encrypting my password using below code.

        public static string GetSHA1HashData(string password)
        {
            //create new instance of md5
            SHA1 sha1 = SHA1.Create();

            //convert the input text to array of bytes
            byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(password));

            //create new instance of StringBuilder to save hashed data
            StringBuilder returnValue = new StringBuilder();

            //loop for each byte and add it to StringBuilder
            for (int i = 0; i < hashData.Length; i++)
            {
                returnValue.Append(hashData[i].ToString());
            }
            // return hexadecimal string
            return returnValue.ToString();
        }

But I also want to create code for Decryption. I've tried, but couldn't a good solution. So could you help me on this?

Here I used System.Security.Cryptography => SHA1 : HashAlgorithm

Thanks in advance.

Kinjal Gohil
  • 958
  • 9
  • 15
  • Side note: do not forget to `Dispose` sha1: `using (SHA1 sha1 = SHA1.Create() {...}` – Dmitry Bychenko May 18 '16 at 06:56
  • 4
    How did this get 4 upvotes? – Luke Joshua Park May 18 '16 at 08:12
  • Don't store plain password hashes - even if they can't be "decrypted" you can still brute-force them. If you have passwords "salt" them and if you don't understand what "salting" means learn it or leave the job up to someone who knows how to do it right. – Robert May 18 '16 at 08:27

1 Answers1

4

Hash value can't be decrypted:

  1. Hash is short (say, 256-bit only), while String is arbitrary long (up to 2GB), so there're many Strings with the same hash (ambiguity)
  2. Hash algorithm (SHA1) has been specially designed such that it's a difficult task to find out a string that has given hash value (complexity)

Instead of decrypting, compare hash values: if user provides a password that has the same hash value that a stored hash, then the password is correct one.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215