9

I have code to generate SHA512 from string.

 public static string GetCrypt(string text)
        {
            string hash = "";
            SHA512 alg = SHA512.Create();
            byte[] result = alg.ComputeHash(Encoding.UTF8.GetBytes(text));
            hash = Encoding.UTF8.GetString(result);
            return hash;
        }
    
Now I must convert hash back to string. Any ideas how can it be done? Thank you.
Mr.V.
  • 128
  • 1
  • 1
  • 7
  • 10
    This can't be done. A hash function is one way. – Gabber Aug 27 '12 at 14:28
  • 2
    What you want is encryption. There's an example [here](http://msdn.microsoft.com/en-us/library/system.security.cryptography.aescryptoserviceprovider) on how to use AES to do just that. – Nasreddine Aug 27 '12 at 14:33

3 Answers3

32

Hashes are 1-way. You can't get it back (easily). you might want actual encryption.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

Yes. Hashes are one-way. Please use symmetric encryption classes like RijndaelManaged.

Here is a RijndaelSimple class that I am using: http://www.obviex.com/samples/encryption.asp

The cached version of the same link is here: http://webcache.googleusercontent.com/search?q=cache:WyVau-XgIzkJ:www.obviex.com/samples/encryption.asp&hl=en&prmd=imvns&strip=1

Gautam Jain
  • 6,789
  • 10
  • 48
  • 67
0

You cant convert a hash back to string from which you computed the hash.

If you want it then you would have to compare the hash with each of the target strings hash.

If one of them matches with the hash,then that hash comes from the target string.

Its use: If you want to store passwords in database,you can store its hashes instead of passwords.So even if a hacker gets access to your database,he cant get the password cuz it is hashed.The only way to know the string through which we created the hash is to match it with the desired string!

Anirudha
  • 32,393
  • 7
  • 68
  • 89