-3

Below is my code for encrypting a string using SHA256.

SHA256CryptoServiceProvider x1 = new SHA256CryptoServiceProvider();

byte[] bs1 = System.Text.Encoding.UTF8.GetBytes(text);
bs1 = x1.ComputeHash(bs1);
System.Text.StringBuilder s1 = new System.Text.StringBuilder();

foreach (byte b in bs1)
{
    s1.Append(b.ToString("x2").ToLower());
}

Console.WriteLine(s1.ToString());
Console.ReadKey();

Can any one help me decrypt the resulting string using SHA256?

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Ajit Boyite
  • 33
  • 1
  • 2
  • 8
  • 6
    SHA256 is a **one-way** [hashing algorithm](https://en.wikipedia.org/wiki/Hash_function). It is not an encryption algorithm; you can't decrypt it, you can merely encrypt the same bytes again and compare the hashes. Note that hashing algorithms are 100% the correct way to go for storing passwords. You should never be able to decrypt a user's password. – ProgrammingLlama Aug 22 '18 at 05:19
  • 7
    You're essentially asking something like "how do I uncook an egg?" – ProgrammingLlama Aug 22 '18 at 05:37
  • 2
    @John Gosh I'd love to get the answer to that question! – Rafalon Aug 22 '18 at 06:19

1 Answers1

4

You are essentially generating a checksum, not encrypting that text. This answer regarding storing passwords as hashes should help clarify what that means.

You can't decrypt a hashed value, but you could compare it to another known value to see if it matches. That known value might be the result of hashing a password, for example, or a known hash from a rainbow table.

If you actually want to encrypt and be able to decrypt data, you should perhaps look into AES-encryption instead.

Kjartan
  • 18,591
  • 15
  • 71
  • 96