Monday, October 13, 2008

Encryption & Decryption using MD5 Algorithm

If sumbody wants to try his hands on encryption & decryption using MD5 algorithm, then below are the two functions :


using System.Text;
using System.Security.Cryptography;

public static string EncryptMd5(string sOriginal)
{
string sPassKey = "16";
byte[] sPassKeyArray;
byte[] sOriginalArray = UTF8Encoding.UTF8.GetBytes(sOriginal);
MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
sPassKeyArray = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(sPassKey));
MD5Hash.Clear();
TripleDESCryptoServiceProvider tripleDesCsp = new TripleDESCryptoServiceProvider();
tripleDesCsp.Key = sPassKeyArray;
tripleDesCsp.Mode = CipherMode.ECB;
tripleDesCsp.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tripleDesCsp.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(sOriginalArray, 0, sOriginalArray.Length);
tripleDesCsp.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

public static string DecryptMd5(string sToOriginal)
{
string sPassKey = "16";
byte[] sPassKeyArray;
byte[] sOriginalArray = Convert.FromBase64String(sToOriginal);
MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
sPassKeyArray = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(sPassKey));
MD5Hash.Clear();
TripleDESCryptoServiceProvider tripleDesCsp = new TripleDESCryptoServiceProvider();
tripleDesCsp.Key = sPassKeyArray;
tripleDesCsp.Mode = CipherMode.ECB;
tripleDesCsp.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tripleDesCsp.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(sOriginalArray, 0, sOriginalArray.Length);
tripleDesCsp.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}

Thursday, September 25, 2008

GridView bug that fire RowCommand twice

I was trying my hands on .NET 3.5, and discovered that Gridview was firing Rowcommand twice for my ImageButton that is drawn using ButtonField Column. Later I found that this is a bug/unexpected behaviour from out of the box Gridview. I found some solution on internet that can be seen in detail here:
http://forums.asp.net/p/1002747/1324414.aspx#1324414
It mainly suggest to use TemplateField column with imagebutton, as Imagebutton is not quite compatible with ButtonField. Rest you can read above for details on it.

Thursday, June 26, 2008

Sorting XML With System.Xml.XPath.XPathExpression

If you want to retrieve a sorted list of nodes from an XmlDocument instance, there are a couple ways to do this. You can use XSLT, or you can write your own sorting algorithm. The .NET Framework provides capability to provide sort orders to the result of an xpath query through the System.Xml.XPath.XPathExpression::AddSort method. Here is a simplistic demo:

static void Main(string[] args)

{

XmlDocument doc = new XmlDocument();

doc.LoadXml("5361");

XPathNavigator nav = doc.CreateNavigator();

string path = "/a/b";

XPathExpression expression = nav.Compile(path);

expression.AddSort("text()", XmlSortOrder.Ascending, XmlCaseOrder.None, string.Empty, XmlDataType.Number);

XPathNodeIterator iterator = nav.Select(expression);

while (iterator.MoveNext())

{

Console.WriteLine(iterator.Current.Value);

}

}

Tuesday, June 13, 2006

Appending data from different text files to single file !!

These dayz I m doing nothing, except reading the code and tryin to understand it.
My senior came to me and asked me to write a small application to read all the text files from the directory and append their data into a file. Its a very small and easy thing to do, but still I m posting it thinking that this small piece of code will help someone.

using System.IO;
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo dir1 = new DirectoryInfo(@"C:\abc");

FileInfo[] txtfiles = dir1.GetFiles("*.txt");

FileInfo fxists = new FileInfo(@"c:\abc\c.txt");
if (fxists.Exists)
{
fxists.Delete();
}

foreach (FileInfo f in txtfiles)
{
StreamReader sr = File.OpenText(f.Name);
string read = null;
while ((read = sr.ReadLine()) != null)
{

FileInfo fi = new FileInfo(@"c:\abc\c.txt");
StreamWriter sw = fi.AppendText();

sw.Write(read.ToString());
sw.Close();

}

}

}

Monday, April 24, 2006

Lately, I have been given the task of stealin data from the job sites like Yahoo, CarrerBuilder and Monster and others like Bigwheels & StylineConcepts. I m really pissed off by doing this job since the last 1 month or so. As soon as I finish off with the parsing of 1 site. Another site comes for the parsing. I m waiting for a new project which doesnt involve html parsing.

This is the function which i used to extract the html content of a page into the string and used it further as i required.

public string Find_Source_Code(string myURL)
{
if(myURL.Trim().Length<=0) return "";

string return_result = "";
System.Net.WebClient Http = new System.Net.WebClient();
WebRequest objRequest = System.Net.HttpWebRequest.Create(myURL);

try
{
using(StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream()))
{
return_result = sr.ReadToEnd();
sr.Close();
}
}
catch
{
return_result = "";
}
return return_result;
}


private void ParsenCheck()
{
string line=Find_Source_Code(url);
string[] Description;
Boolean flag;
flag=false;

Description=line.Split('\n');

//Some more string commands to parse the data
}