Monday, August 10, 2009

Encrypt - Decrypt with DES and MD5 Algorithm

Encrypt - Decrypt with DES Algorithm
================================

// Use this Namespace
using System.Security.Cryptography;

// Encrypt Function
public static string Encryption(string value)
{
if (value != "")
{
DESCryptoServiceProvider CryptoProvidor = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, CryptoProvidor.CreateEncryptor(Key_64, Iv_64), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
}
return string.Empty;
}

// Decrypt Function

public static string Descryption(string value)
{
if (value != "")
{
DESCryptoServiceProvider CryptoProvidor = new DESCryptoServiceProvider();
Byte[] buf = Convert.FromBase64String(value);
MemoryStream ms = new MemoryStream(buf);
CryptoStream cs = new CryptoStream(ms, CryptoProvidor.CreateDecryptor(Key_64, Iv_64), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
return string.Empty;
}


Encrypt with MD5 Algorithm
========================

// Encrypt Function

public static string EncryptWithMD5(string ClearString)
{
System.Text.UnicodeEncoding objUE = new System.Text.UnicodeEncoding();

byte[] bytClearString = objUE.GetBytes(ClearString);

MD5CryptoServiceProvider objProv = new MD5CryptoServiceProvider();

byte[] hash = objProv.ComputeHash(bytClearString);
return Convert.ToBase64String(hash);

}

No comments:

Post a Comment