Introduction
This article will show you a simple approach of Encrypt and Decrypt techniques and computing Hash values.
Background
Once I had to do cryptography in one of my projects. I did not have much knowledge about this. I searched on the internet to get a better idea for a simple approach of doing Encryption and Decryption and computing Hash values. But I did not find all of them in a single place. This article will cover all these issues.
Using the Code
Open Visual Studio and open a new class library project. Add a class named “CryptographyManager.cs”.
public class CryptographyManager
{
}
Then add the following private
member variables:
private byte[] _keyByte = { };
private static string _key = Pass@123#;
private byte[] _ivByte = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
We will have 3 Encrypt and 3 Decrypt methods using the method overloading technique.
public string Encrypt(string value)
{
return Encrypt(value, string.Empty);
}
public string Encrypt(string value, string key)
{
return Encrypt(value, key, string.Empty);
}
public string Encrypt(string value, string key, string iv)
{
string encryptValue = string.Empty;
MemoryStream ms = null;
CryptoStream cs = null;
if (!string.IsNullOrEmpty(value))
{
try
{
if(!string.IsNullOrEmpty(key))
{
_keyByte = Encoding.UTF8.GetBytes
(key.Substring(0,8));
if (!string.IsNullOrEmpty(iv))
{
_ivByte = Encoding.UTF8.GetBytes
(iv.Substring(0, 8));
}
}
else
{
_keyByte = Encoding.UTF8.GetBytes(_key);
}
using (DESCryptoServiceProvider des =
new DESCryptoServiceProvider())
{
byte[] inputByteArray =
Encoding.UTF8.GetBytes(value);
ms = new MemoryStream();
cs = new CryptoStream(ms, des.CreateEncryptor
(_keyByte, _ivByte), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
encryptValue = Convert.ToBase64String(ms.ToArray());
}
}
catch
{
}
finally
{
cs.Dispose();
ms.Dispose();
}
}
return encryptValue;
}
public string Decrypt(string value)
{
return Decrypt(value, string.Empty);
}
public string Decrypt(string value, string key)
{
return Decrypt(value, key, string.Empty);
}
public string Decrypt(string value, string key, string iv)
{
string decrptValue = string.Empty;
if (!string.IsNullOrEmpty(value))
{
MemoryStream ms = null;
CryptoStream cs = null;
value = value.Replace(" ", "+");
byte[] inputByteArray = new byte[value.Length];
try
{
if (!string.IsNullOrEmpty(key))
{
_keyByte = Encoding.UTF8.GetBytes
(key.Substring(0, 8));
if (!string.IsNullOrEmpty(iv))
{
_ivByte = Encoding.UTF8.GetBytes
(iv.Substring(0, 8));
}
}
else
{
_keyByte = Encoding.UTF8.GetBytes(_key);
}
using (DESCryptoServiceProvider des =
new DESCryptoServiceProvider())
{
inputByteArray = Convert.FromBase64String(value);
ms = new MemoryStream();
cs = new CryptoStream(ms, des.CreateDecryptor
(_keyByte, _ivByte), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
decrptValue = encoding.GetString(ms.ToArray());
}
}
catch
{
}
finally
{
cs.Dispose();
ms.Dispose();
}
}
return decrptValue;
}
Now write methods to compute Hash values for a string
.
There are different Hash techniques. So we are making an enumeration for those names. Declare an Enum
in the class:
public enum HashName
{
SHA1 = 1,
MD5 = 2,
SHA256 = 4,
SHA384 = 8,
SHA512 = 16
}
Now we will write two overloading methods for computing Hash values.
public string ComputeHash(string plainText, string salt)
{
return ComputeHash(plainText, salt,HashName.MD5);
}
public string ComputeHash(string plainText, string salt, HashName hashName)
{
if (!string.IsNullOrEmpty(plainText))
{
byte[] plainTextBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
byte[] plainTextWithSaltBytes = null;
byte[] saltBytes;
if (!string.IsNullOrEmpty(salt))
{
saltBytes = ASCIIEncoding.ASCII.GetBytes(salt);
plainTextWithSaltBytes =
new byte[plainTextBytes.Length + saltBytes.Length];
}
else
{
int minSaltSize = 4;
int maxSaltSize = 8;
Random random = new Random();
int saltSize = random.Next(minSaltSize, maxSaltSize);
saltBytes = new byte[saltSize];
RNGCryptoServiceProvider rngCryptoServiceProvider =
new RNGCryptoServiceProvider();
rngCryptoServiceProvider.GetNonZeroBytes(saltBytes);
}
for (int i = 0; i < plainTextBytes.Length; i++)
{
plainTextWithSaltBytes[i] = plainTextBytes[i];
}
for (int i = 0; i < saltBytes.Length; i++)
{
plainTextWithSaltBytes[plainTextBytes.Length + i] =
saltBytes[i];
}
HashAlgorithm hash = null;
switch (hashName)
{
case HashName.SHA1:
hash = new SHA1Managed();
break;
case HashName.SHA256:
hash = new SHA256Managed();
break;
case HashName.SHA384:
hash = new SHA384Managed();
break;
case HashName.SHA512:
hash = new SHA512Managed();
break;
case HashName.MD5:
hash = new MD5CryptoServiceProvider();
break;
}
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
byte[] hashWithSaltBytes =
new byte[hashBytes.Length + saltBytes.Length];
for (int i = 0; i < hashBytes.Length; i++)
{
hashWithSaltBytes[i] = hashBytes[i];
}
for (int i = 0; i < saltBytes.Length; i++)
{
hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
}
string hashValue = Convert.ToBase64String(hashWithSaltBytes);
return hashValue;
}
return string.Empty;
}
Points of Interest
This code can be used in any .NET project for quick encryption and decryption of any text using DESCryptography algorithm.
History
- 7th August, 2009: Initial post
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.