Heare I created the C# method for to return the encrypted password where I used SHA256 + SALT
public static string sha256encrypt(string phrase,string UserName)
{
string salt = CreateSalt(UserName);
string saltAndPwd = String.Concat(phrase, salt);
UTF8Encoding encoder = new UTF8Encoding();
SHA256Managed sha256hasher = new SHA256Managed();
byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(saltAndPwd));
string hashedPwd = String.Concat(byteArrayToString(hashedDataBytes), salt);
return hashedPwd;
}
public static string byteArrayToString(byte[] inputArray)
{
StringBuilder output = new StringBuilder("");
for (int i = 0; i < inputArray.Length; i++)
{
output.Append(inputArray[i].ToString("X2"));
}
return output.ToString();
}
Heare I taken Username, converted to a byte array, then XORed the bytes together. I took the xored result as the seed to my random number generator. Then I used the result of the PRG as my salt.
private static string CreateSalt(string UserName)
{
string username = UserName;
byte[] userBytes; string salt;
userBytes = ASCIIEncoding.ASCII.GetBytes(username);
long XORED = 0x00; foreach (int x in userBytes)
XORED = XORED ^ x;
Random rand = new Random(Convert.ToInt32(XORED));
salt = rand.Next().ToString();
salt += rand.Next().ToString();
salt += rand.Next().ToString();
salt += rand.Next().ToString();
return salt;
}
Hope it helps!!!!
Friday, December 26, 2008
Subscribe to:
Posts (Atom)
