Password Security : Encrypt and decrypt a string using Triple Des


In cryptographyTriple DES is the common name for the Triple Data Encryption Algorithm (TDEA or Triple DEAblock cipher, which applies theData Encryption Standard (DES) cipher algorithm three times to each data block.

This code snippet is used for encrypting and decrypting a string using Triple Des and hashing.


 // TO ENCRYPT A STRING                                       public static string Encrypt(string toEncrypt, bool useHashing)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

string key = "Someth1ngSpeci@l";

if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();

byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
catch (Exception)
{
return null;
}
}

// TO DECRYPT A STRING public static string Decrypt(string cipherString, bool useHashing)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);


string key = "Someth1ngSpeci@l"

if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception)
{
return null;

}


}

Validate EmailAddress in Asp.net using RegularExpressionValidator Control


Here are the steps to validate EmailAddress in Asp.net using RegularExpressionValidator Control.


Step 1 :
  Add a RegularExpressionValidator control to your web page.

Step 2: 

         Right click on the control and select properties.

    • Set the ControlToValidate property to indicate which control to validate.
    • Set the ValidationExpression property to  regular expression as shown
      in below image.