Wednesday, August 12, 2009

How To: Hash Data Using MD5 and SHA1

There are two types of Encryption:

1- One way Encryption:
take input data and encrypt it, and there is no way to decrypt it again to get the source data and the good sample for one way encryption is MD5.

also the good sample for one way encryption (SQL Server Membership), it's store passwords encrypted and there is nno way to get the original Password.

only we can compare between the source you enterd and the hashed data.

2- Two way Encryption:
take input data and encrypt it, and in another side we can take encrypted data and decrypt it again using the same algorithm.

Sample:
http://waleedelkot.blogspot.com/2009/02/encryption-and-decryption-using-c.html

today I'll talk about MD5 and SHA1 and I'll Present a sample code.

Namespace: System.Security.Cryptography

the below method will return MD5 hashed string:

private string GetMD5HashData(string data)
{
MD5 md5 = MD5.Create();
byte[] hashData = md5.ComputeHash(Encoding.Default.GetBytes(data));
StringBuilder returnValue = new StringBuilder();
for (int i = 0; i <>
{
returnValue.Append(hashData[i].ToString());
}
return returnValue.ToString();
}

the below method will return MD5 hashed string:

private string GetSHA1HashData(string data)
{
SHA1 sha1 = SHA1.Create();
byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));
StringBuilder returnValue = new StringBuilder();
for (int i = 0; i <>
{
returnValue.Append(hashData[i].ToString());
}
return returnValue.ToString();
}

you can save the return value in Database and check it in the another side like SQL Server Membership.
that's great, but How can I Validate input Data and stored hashed data in Database?

the below method will validate MD5 hashed string:

private bool ValidateMD5HashData(string inputData, string storedHashData)
{
string getHashInputData = GetMD5HashData(inputData);
if (string.Compare(getHashInputData, storedHashData) == 0)
{
return true;
}
else
{
return false;
}
}

the below method will validate SHA1 hashed string:

private bool ValidateSHA1HashData(string inputData, string storedHashData)
{
string getHashInputData = GetSHA1HashData(inputData);
if (string.Compare(getHashInputData, storedHashData) == 0)
{
return true;
}
else
{
return false;
}
}

How To: Convert Text to Image Using C#

I'll show you how to convert any string to image and save it in your hard disk drive.
also you can use this sample code to prevent your website content from spiders.
there are many benfits.

1- Create Windows Forms Application.
2- Add Button Control on Form.
3- Add TextBox Control on Form and set Multiline to TRUE.

Sample Code:

public Color FontColor { get; set; }
public Color FontBackColor { get; set; }
public string FontName { get; set; }
public string ImagePath { get; set; }
public int FontSize { get; set; }
public int ImageHeight { get; set; }
public int ImageWidth { get; set; }
public Bitmap ImageText { get; set; }
public Graphics ImageGraphics { get; set; }
public Font ImageFont { get; set; }
public SolidBrush BrushForeColor { get; set; }
public SolidBrush BrushBackColor { get; set; }
public PointF ImagePointF { get; set; }



private void DrawText(string text)
{
FontColor = Color.Red;
FontBackColor = Color.Yellow;
FontName = "Arial";
FontSize = 10;
ImageHeight = textBox1.Height;
ImageWidth = textBox1.Width;
ImagePath = @"C:\WaleedImageTest.JPEG";
ImageText = new Bitmap(ImageWidth, ImageHeight);

ImageGraphics = Graphics.FromImage(ImageText);

ImageFont = new Font(FontName, FontSize);
ImagePointF = new PointF(5, 5);
BrushForeColor = new SolidBrush(FontColor);
BrushBackColor = new SolidBrush(FontBackColor);

ImageGraphics.FillRectangle(BrushBackColor, 0, 0, ImageWidth, ImageHeight);
ImageGraphics.DrawString(text, ImageFont, BrushForeColor, ImagePointF);

ImageText.Save(ImagePath, ImageFormat.Jpeg);
}


private void button1_Click(object sender, EventArgs e)
{
DrawText(textBox1.Text);
}


Then Run Your Application.