Friday, April 24, 2015

KPBDOTNET: Working on Data Encryption / Decryption

KPBDOTNET: Working on Data Encryption / Decryption: While coding we may have to handle sensitive data sometimes, like password fields. We may required this data not to be visible to the outs...

Working on Data Encryption / Decryption

While coding we may have to handle sensitive data sometimes, like password fields. We may required this data not to be visible to the outside world. In such cases we can implement the concept of Data Encryption/ Decryption.


Following code will show how to implement the same in C#.NET.


We have to create a simple C#.NET console application which will primarily focus on three different types of data Encryption Base64, SHA1, MD5.
So create three different methods to implement each type of encryption.
Also for Base64 data decryption create another method. Data decryption in remaining two types SHA1, MD5 is  not possible, it is one way process.


Complete code listing is as follows -


static void Main(string[] args)
{
Console.Write("Enter word : ");
string myWord = Console.ReadLine();
//Base64 Encoding
Console.WriteLine("=====1. Base 64 Encoding=====");
string MyEncoded=Encode_Base64(myWord);
string MyDecoded=Decode_Base64(MyEncoded);
Console.WriteLine("Encoded Word : "+MyEncoded);
Console.WriteLine("Decoded Word : "+MyDecoded);
Console.WriteLine(string.Empty);
//Hashing
Console.WriteLine("=====2. Hash SHA1 one way , Decoding not possible=====");
MyEncoded=Encode_Hash(myWord);
Console.WriteLine("Encoded Word : "+MyEncoded);
Console.WriteLine(string.Empty);
//MD5
Console.WriteLine("=====3. MD5 =====");
MyEncoded = Encode_MD5(myWord);
Console.WriteLine("Encoded Word : " + MyEncoded);
Console.WriteLine(string.Empty);
Console.ReadLine();
}
public static string Encode_Base64(string word)
{
var encoded = System.Text.Encoding.UTF8.GetBytes(word);
return System.Convert.ToBase64String(encoded);
}
public static string Decode_Base64(string Eword)
{
var decoded = System.Convert.FromBase64String(Eword);
return System.Text.Encoding.UTF8.GetString(decoded);
}
public static string Encode_Hash(string word)
{
var myHash=System.Text.Encoding.ASCII.GetBytes(word);
var mySHA=System.Security.Cryptography.SHA1.Create();
var myBytes=mySHA.ComputeHash(myHash);
var sb=new StringBuilder();
for (int i = 0; i < myBytes.Length; i++)
    {
        sb.Append(myBytes[i].ToString("X2"));
    }
return sb.ToString();
}
public static string Encode_MD5(string word)
{
var myHASH = System.Text.Encoding.ASCII.GetBytes(word);
var myMD5 = System.Security.Cryptography.MD5.Create();
var myBytes = myMD5.ComputeHash(myHASH);
var sb = new StringBuilder();
for (int i = 0; i < myBytes.Length; i++)
{
sb.Append(myBytes[i].ToString("X2"));
}
return sb.ToString();
}



Happy Coding :)


Friday, April 17, 2015

How to make entire DIV tag clickable

While working on various aspects of designing of the website, we may have to deal with a scenario where we have to make part of page , DIV tag, entirely clickable. Lets see how we can work on this.

The best way is to enclose entire div tag in an anchor tag as follows. This way we can mention the link, if any, where page should redirect after clicking div tag.


<a runat="server" href="http://www.kpbdotnet.blogspot.com/">
<div class="jumbotron">
this is my DIV tag and we are making this complete div tag clickable
<p>
We are contents of div tag
</p>
</div>
</a>

We can have any content in div tag like image, a long description, etc.

Sometimes, instead of redirect we may have to write some code. So replace anchor tag with LinkButton control as follows

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">
<div class="jumbotron">
test div tag
<asp:Label ID="lbl" runat="server" Text="click"></asp:Label>
</div>
</asp:LinkButton>
Code Snippet for OnClick event is as follows -
protected void LinkButton1_Click(object sender, EventArgs e)
{
lbl.Text = "You click div tag.";
}

So working on existing controls as you require, is the best way to achieve what you need.

- Happy Coding -
 


 

Tuesday, April 14, 2015

Opening two windows on Hyperlink click

Sometimes, we may require to open two windows on one Hyperlink click.

We can achieve this easily with button control by mentioning on link on client side scripting and the other on server side coding. But when we have to use Hyperlink, the simplest way to achieve this is as follows -

<a runat="server" href="http://www.google.com" onclick="window.open('http://www.yahoo.com');" >click Me</a>


We can execute above code and after clicking on the link two different windows will open one for http://www.google.com and the other for http://www.yahoo.com


Happy Coding !