Monday, July 28, 2008

encryption & decryption

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 < intLength; intCount++)
{
string strChar = strData.Substring((intCount * 3), 3);
bytData[intCount] = Convert.ToByte(strChar);
}
MemoryStream ms = new MemoryStream(bytData);

CryptoStream encStream = new CryptoStream(ms,
objKey.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(encStream);
string strReturnVal = sr.ReadLine();
sr.Close();
encStream.Close();
ms.Close();

return strReturnVal;

}
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 < strPassword.Length; intCount++)
{
objKey[intCount] = Convert.ToByte(Convert.ToChar(strPassword.Substring(intCount, 1)));
}
return objKey;

}
}

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");

hopes this will help you...
Regards
maddy.....

5 comments:

altacus said...

Thanks for your code, I refactored it a bit and wanted to share. 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 < bytData.Length; intCount++)
{
string strChar = strData.Substring((intCount * 3), 3);
bytData[intCount] = Convert.ToByte(strChar);
}

string strReturnVal = null;
using (MemoryStream ms = new MemoryStream(bytData))
{
using (CryptoStream encStream = new CryptoStream(ms, objKey.CreateDecryptor(), CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(encStream))
{
strReturnVal = sr.ReadLine();
sr.Close();
}
encStream.Close();
}
ms.Close();
}
return strReturnVal;
}

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 < strPassword.Length; ++intCount)
{
objKey[intCount] = Convert.ToByte(Convert.ToChar(strPassword.Substring(intCount, 1)));
}
return objKey;
}

saroj khatiwoda said...

thanks,
code worked,

Anonymous said...

Hi, this my first time to do encryption on password that are going to be stored into a database. I have read you codes and I am just a nooby at this. Can i ask where can i place this function encrypt the password that will be stored in the database? Thanks, I really hope you guys can help me.

Anonymous said...

how can i call the function? And where should i place the function, so that the passwords that the user will register in the my site will be encrypted. I am also using asp.net. Thanks again, i really need help if you guys can help me out

Unknown said...

hi this is nice. but i need vb coding