Tuesday, June 9, 2009

encryption & decryption

METHOD-I

#region Decryption
public static string Decryption(string DecryptPassword)
{

return System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(DecryptPassword));
}
#endregion

#region Encryption
public static string Encryption(string EncrPassword)
{

return Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(EncrPassword));
}
#endregion
-------------------------------------------------------------------------------------------

METHOD-II ::

Class for encryption and decryption

using System;
using System.IO;
using System.Security.Cryptography;

// Encryption & Decryption using DES algorithm

public class clsDecrypt
{
//Encrypt function
public static string strEncrypt(string strData, string strKey1, string strKey2)
{
MemoryStream ms = new MemoryStream();

DESCryptoServiceProvider objKey = new DESCryptoServiceProvider();
objKey.Key = objLockKey(strKey1);
objKey.IV = objLockKey(strKey2);


CryptoStream encStream = new CryptoStream(ms,
objKey.CreateEncryptor(), CryptoStreamMode.Write);

StreamWriter sw = new StreamWriter(encStream);
sw.WriteLine(strData);
sw.Close();
encStream.Close();


byte[] bytData = ms.ToArray();
string strReturnData = "";

foreach (byte bytChar in bytData)
{
strReturnData += bytChar.ToString().PadLeft(3, Convert.ToChar("0"));
}
ms.Close();

return strReturnData;

}
//Decrypt function
public static string strDecrypt(string strData, string strKey1, string strKey2)
{

DESCryptoServiceProvider objKey = new DESCryptoServiceProvider();
objKey.Key = objLockKey(strKey1);
objKey.IV = objLockKey(strKey2);

Int16 intLength = Convert.ToInt16((strData.Length / 3));
byte[] bytData = new byte[intLength];

for (Int16 intCount = 0; intCount < strchar =" strData.Substring((intCount" ms =" new" encstream =" new" sr =" new" strreturnval =" sr.ReadLine();">
}
private static byte[] objLockKey(string strPassword)
{
const int intKeyLength = 8;
strPassword = strPassword.PadRight(intKeyLength,
Convert.ToChar(".")).Substring(0, intKeyLength);
byte[] objKey = new byte[strPassword.Length];
for (int intCount = 0; intCount <>
}
}

Call the above function from any page as below
//encryption
string encpassword=clsDecrypt.strEncrypt(password,"choice1" ,"choice2");
//decryption
string password=
clsDecrypt.string strDecrypt(encpassword,"choice1","choice2");

------------------------------------------------------------------------------------------------------------------------

I refactored it a bit. Main changes were utilizing the Using to enforce the dispose method for the streams and also the use of a string builder for speed.

private static DESCryptoServiceProvider CryptoHelper(string Key1, string Key2)
{
DESCryptoServiceProvider objKey = new DESCryptoServiceProvider();
objKey.Key = objLockKey(Key1);
objKey.IV = objLockKey(Key2);
return objKey;
}

public static string Encrypt(string strData, string strKey1, string strKey2)
{
using (MemoryStream ms = new MemoryStream())
{
DESCryptoServiceProvider objKey = CryptoHelper(strKey1, strKey2);

using (CryptoStream encStream = new CryptoStream(ms, objKey.CreateEncryptor(), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(encStream))
{
sw.WriteLine(strData);
sw.Close();
}
encStream.Close();
}

System.Text.StringBuilder strReturnData = new System.Text.StringBuilder();
foreach (byte bytChar in ms.ToArray())
{
strReturnData.Append(bytChar.ToString().PadLeft(3, Convert.ToChar("0")));
}
ms.Close();
return strReturnData.ToString().Trim();
}
}

// Decrypt function
public static string Decrypt(string strData, string strKey1, string strKey2)
{
DESCryptoServiceProvider objKey = CryptoHelper(strKey1, strKey2);

byte[] bytData = new byte[Convert.ToInt16((strData.Length / 3))];

for (Int16 intCount = 0; intCount < strchar =" strData.Substring((intCount" strreturnval =" null;" ms =" new" encstream =" new" sr =" new" strreturnval =" sr.ReadLine();" intkeylength =" 8;" strpassword =" strPassword.PadRight(intKeyLength," objkey =" new" intcount =" 0;" style="font-size:78%;">


No comments:

Popular Posts