
Introduction
My encryption methods take a string and hash it using MD5 or SHA1 and then return it encrypted.
There are two types of encryption:
- One way Encryption
Take the input data and encrypt it, and there is no way to decrypt it again to get the source data. A good sample for one way encryption is MD5. Another good sample for one way encryption is SQL Server Membership; it store passwords encrypted and there is no way to get the original password back. All we can do is compare the password you entered and the hashed data.
- Two way Encryption
Take input data and encrypt it, and in another side we take the encrypted data and decrypt it again using the same algorithm.
See samples here: http://waleedelkot.blogspot.com/2009/02/encryption-and-decryption-using-c.html.
In this article, I will talk about MD5 and SHA1 and present a sample code. The namespace we are using is: System.Security.Cryptography
.
Advantages and Disadvantages
The following articles give a lot of detail on the differences of performance of the algorithms:
Important Information
MD5 (Message Digest Algorithm):
It was developed by Ronald Rivest in 1991. The hash size for the MD5 algorithm is 128 bits. The ComputeHash
methods of the MD5 class returns the hash as an array of 16 bytes.
SHA1 (Secure Hash Algorithm):
This was developed by NIST. The hash size for the SHA1 algorithm is 160 bits.
Background
If you are familiar with C# and its security classes, this article will be easy for you.
Using the code
You can use the GetMD5HashData
or GetSHA1HashData
method directly to hash any string. I'm using the MD5
class and the SHA1
class.
The sample code contains four methods:
GetMD5HashData
: takes any string and hashes it using the MD5
class.GetSHA1HashData
: takes any string and hashes it using the SHA1
class.ValidateMD5HashData
: compares input text using MD5 with the stored one.ValidateSHA1HashData
: compares input text using SHA1 with the stored one.
Hashing using the MD5 class
private string GetMD5HashData(string data)
{
MD5 md5 = MD5.Create();
byte[] hashData = md5.ComputeHash(Encoding.Default.GetBytes(data));
StringBuilder returnValue = new StringBuilder();
for (int i = 0; i < hashData.Length; i++)
{
returnValue.Append(hashData[i].ToString());
}
return returnValue.ToString();
}
Hashing using the SHA1 class
private string GetSHA1HashData(string data)
{
SHA1 sha1 = SHA1.Create();
byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));
StringBuilder returnValue = new StringBuilder();
for (int i = 0; i < hashData.Length; i++)
{
returnValue.Append(hashData[i].ToString());
}
return returnValue.ToString();
}
Validation methods
private bool ValidateMD5HashData(string inputData, string storedHashData)
{
string getHashInputData = GetMD5HashData(inputData);
if (string.Compare(getHashInputData, storedHashData) == 0)
{
return true;
}
else
{
return false;
}
}
private bool ValidateSHA1HashData(string inputData, string storedHashData)
{
string getHashInputData = GetSHA1HashData(inputData);
if (string.Compare(getHashInputData, storedHashData) == 0)
{
return true;
}
else
{
return false;
}
}
I hope my article was helpful for you all. Here is my blog: http://waleedelkot.blogspot.com/.