Example for MD5 Hashing and SH512(salted Hashing)

Example for MD5 Hashing and SH512(salted Hashing)

MD5 Hashing (Message Digest algorithm 5) is a cryptographic hashing function to produce 128 hash value, and utilized in a wide variety of security applications, as well to check data integrity

public class FileSecure

{

static readonly string securityCode= "mysaltkey";

/// <summary>

/// Encrypt text string

/// </summary>

/// <param name="toEncryptorDecrypt"> data to encryptorDecrypt</param>

/// <param name="encrypt">Weather encrypt or decrypt</param>

/// <returns>An encrypted or decrypted string</returns>

 

public static string EncryptorDecrypt(string key,bool encrypt)

{

byte[] toEncryptorDecryptArray;

ICryptoTransform cTransform;

// Transform the specified region of bytes array to resultArray

MD5CryptoServiceProvider md5Hasing = new MD5CryptoServiceProvider();

byte[] keyArrays= md5Hasing.ComputeHash(UTF8Encoding.UTF8.GetBytes(securityCode));

md5Hasing.Clear();

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()

{ Key = keyArrays, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };

if (encrypt == true)

{toEncryptorDecryptArray = UTF8Encoding.UTF8.GetBytes(key);

cTransform = tdes.CreateEncryptor();

}

else

{

toEncryptorDecryptArray = Convert.FromBase64String(key.Replace(' ', '+'));

cTransform = tdes.CreateDecryptor();

}

byte[] resultsArray = cTransform.TransformFinalBlock(toEncryptorDecryptArray, 0, toEncryptorDecryptArray.Length);

tdes.Clear();

if(encrypt == true)

{ //if encrypt we need to return encrypted string

return Convert.ToBase64String(resultsArray, 0, resultsArray.Length);

}

//else we need to return decrypted string

return UTF8Encoding.UTF8.GetString(resultsArray);

}

 
SHA512 salted hashing

One of a weakness in the MD5 cryptographic hash function is that it allows the construction of different messages with the same MD5 hash. This is known as an MD5 "collision". Secure Hash Algorithm (SHA) 2 is a set of cryptographic hash functions(SHA-224, SHA-256, SHA-384, SHA-512). Salting is a common way to randomize hashes. By adding a random string (which is called a salt) before a password is hashed, makes it much more difficult to crack the password hash.

 

//Sample code for SHA512 hashing

public static string CreateSHAHash(string PasswordSHA512)

{

System.Security.Cryptography.SHA512Managed sha512 = new System.Security.Cryptography.SHA512Managed();

Byte[] EncryptedSHA512 = sha512.ComputeHash(System.Text.Encoding.UTF8.GetBytes(string.Concat(PasswordSHA512, securityCode)));

sha512.Clear();

return Convert.ToBase64String(EncryptedSHA512);

}

}


Ebook Download
View all
Learn
View all