Yesterday, I wrote how to encrypt a password using a one-way MD5 hash, but as some of the comments point out, the MD5 algorithm is not strong enough. I listened and wrote a new encryption method that uses SHA256 instead of MD5.

public string Encrypt(string plainMessage)

{

  byte[] data = Encoding.UTF8.GetBytes(plainMessage);

  using (HashAlgorithm sha = new SHA256Managed())

  {

    byte[] encryptedBytes = sha.TransformFinalBlock(data, 0, data.Length);

    return Convert.ToBase64String(sha.Hash);

  }

}

If I pass my own name to the Encrypt method this would be the result:

Encrypt("Mads Kristensen ") --> “1D3D1917866958C5C0BBA109E20DD5E9B8AAA061AD936472FDE5833F66757D666D616473”.

Comments


Comments are closed