Have a fun while searching

Tuesday, July 5, 2011

Encription and Decription

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Xml;
using System.Security.Cryptography;
using System.Net.Mail;
using System.Data.SqlClient;
namespace EncryptBF
{
public class Encry
{
public string encryptPassword(string strText)
{
return Encrypt(strText, "&%#@?,:*");
}
public string decryptPassword(string str)
{
return Decrypt(str, "&%#@?,:*");
}
private string Encrypt(string strText, string strEncrypt)
{

byte[] byKey = new byte[20];

byte[] dv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

try
{

byKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));

DESCryptoServiceProvider des = new DESCryptoServiceProvider();

byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(strText);

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);

cs.Write(inputArray, 0, inputArray.Length);

cs.FlushFinalBlock();

return Convert.ToBase64String(ms.ToArray());

}

catch (Exception ex)
{

throw ex;

}

}



private string Decrypt(string strText, string strEncrypt)
{

byte[] bKey = new byte[20];

byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

try
{

bKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));

DESCryptoServiceProvider des = new DESCryptoServiceProvider();

Byte[] inputByteArray = inputByteArray = Convert.FromBase64String(strText);

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey, IV), CryptoStreamMode.Write);

cs.Write(inputByteArray, 0, inputByteArray.Length);

cs.FlushFinalBlock();

System.Text.Encoding encoding = System.Text.Encoding.UTF8;

return encoding.GetString(ms.ToArray());

}

catch (Exception ex)
{

throw ex;

}

}

private String GeneratePasskey()
{
int stringLength = 10;

Random rnd = new Random((int)System.DateTime.Now.Ticks);
System.Text.StringBuilder randomText = new
System.Text.StringBuilder(stringLength);

//value of A in ascii codes
int minValue = 65;

//value of Z in ascii codes
int maxValue = 90;

//the range that we are allowed to go above the min value
int randomRange = maxValue - minValue;

double rndValue;

for (int i = 0; i < stringLength; i++)
{
rndValue = rnd.NextDouble();

randomText.Append((char)(minValue + rndValue * randomRange));
}


return randomText.ToString().ToLower();
}

private String GetReferenceNumber()
{
int size = 10;

Random r = new Random();

string legalChars = "123456789abcdefghijklmnopqrstuvwxyz";

StringBuilder sb = new StringBuilder();

for (int i = 0; i < size; i++)

sb.Append(legalChars.Substring(r.Next(0, legalChars.Length - 1), 1));

return sb.ToString();
}
}
}

0 comments:

Post a Comment