Sunday, April 27, 2008

salting in Password encryption using C#

Salting means :: creating random string and attach with original password before encrypting.So your password are safe and untraceble to dictionary attack.

public string MuruEncrypt(string paramPass)
{

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string salt = Guid.NewGuid().ToString();

byte[] p = UnicodeEncoding.UTF8.GetBytes(text+salt);
byte[] en = md5.ComputeHash(p);
return Convert.ToBase64String(en);
}

MD5 CryptoServiceProvider using C#

This class used to create a function Encrypt the Password.
Just put it this way,Whenever the user login into password protected pages of the website,their password first undergo the MD5 Algorithm based conversion.
Then converted or encrypted password check their identification on the stored data.
If both are matched,we allow the user to navigate the login protected pages.


This password can be crack down once the hacker using the dictionary attack if your database compromised by the hacker.

We can overcome this ,by using salting concept.

Salting means just before generating the hash of a text, we attach a piece of random string – known as a salt - into the original text.

using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Web;
using System.Security.Cryptography;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Summary description for Murugesan
///

public class Murugesan
{
public Murugesan()
{

}

public string MuruDate(DateTime d)
{
d = d.AddDays(2);
string DateAddS = d.ToLongDateString();
return DateAddS;

}


public string Encrypt(string text)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] p = UnicodeEncoding.UTF8.GetBytes(text);
byte[] en = md5.ComputeHash(p);
return Convert.ToBase64String(en);


}



}

Sunday, April 20, 2008

How to use SQLBulkCopy in C#

This is code implementation for alternative bcp command prompt of SQL Server for copying the Table records among Local server Databases or Server Databases.

SqlConnection Con = new SqlConnection("Data Source=(Local);Initial Catalog=Pack;user id=sa;pwd=*****");
Con.Open();
SqlCommand Com = new SqlCommand("Select *from TrackDetails", Con);
SqlDataReader Dr = Com.ExecuteReader();


//Destination Database Connection Opening..
SqlConnection DCon = new SqlConnection("Data Source=(Local);Initial Catalog=Muru;user id=sa;pwd=*****");
DCon.Open();

SqlBulkCopy bc = new SqlBulkCopy(DCon);
bc.DestinationTableName = "SQLBulk";
bc.WriteToServer(Dr);
Dr.Close();
Response.Write("Copied successfully");


Just for a demo purpose.you can use the WriteServer method of SQLBulkCopy Class in try,catch and finally to close the stream SQLDataReader and destination connection as well as source connection.

Friday, April 04, 2008

Extracting a month from String - C#

How do extract the month from the user entered string?

DateTime dt=Request.Form["DropDownList1"];
string spdate=dt.Month;

Then simply use this string on your query like to see the result on monthly based
Select *from Sales where Month(salesdate)='"+spdate+"' ;