Tuesday 4 December 2012

Encrypt and Decrypt Password in Asp.Net



Encrypt and Decrypt Password in Asp.Net

// Encryption of Password in Asp.Net on form before storeing value in database.
// Dencryption of Password in Asp.Net on form after getting value from database.
// To evaluate this code drag one <asp:TextBox> and one <asp:Button> control on your web page & write     this code on .CS page.

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     string s= Encrypt(TextBox1.Text);
     Response.Write("Encrypted Password"+s+"<br/>");
     Response.Write("decrypt Password"+ Decrypt(s));
    }

//     Method or Function for Encrytion

    public string Encrypt(string str)
    {
        byte[] encrt_byte = new byte[str.Length];
        encrt_byte = UTF8Encoding.UTF8.GetBytes(str);
        String s = Convert.ToBase64String(encrt_byte);
        return s;
    }

//     Method or Function for Decrytion

    public string Decrypt(string str)
    {
        UTF8Encoding objutfencoding = new UTF8Encoding();
        Decoder utfdecode = objutfencoding.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(str);
        int countchar = utfdecode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[countchar];
        utfdecode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        string s = new string(decoded_char);
        return s;
    }
}


Sql Parameter & Injection


Sql Parameter & Injection

Sql Parameter :- To prevent Sql injection we use sql parameter.

Sql Injection :- fetching the values from database to make condition true illegally called sql injection.
                         example:- sql satement--- select * from [table name] where [column name]=[condition];
                   Condition with Sql Injection------select * from [table name] where [column name]= 1 or 1=1 ;

                                   The statement will always true..


     see next for detailed code information...

Program to find given name is either Male or Female



Program to find given name is either Male or Female
           // Male or Female.... 80% ladies name contains vowels at their last


             Console.WriteLine("Enter your Name to get gender");
             string s = Console.ReadLine();
             int len = s.Length;
            char c =s[ len - 1];
            if (c == 'a' || c=='A')
            {
                Console.WriteLine("Female");
            }
            else if (c == 'e' || c=='E')
            {
                Console.WriteLine("Female");
            }
            else if (c == 'i' || c=='I')
            {
                Console.WriteLine("Female");
            }
            else if (c == 'o' || c=='O')
            {
                Console.WriteLine("Female");
            }
            else if (c == 'u' || c=='U')
            {
                Console.WriteLine("Female");
            }
            else
            {
                Console.WriteLine("Male");
            }
             Console.ReadLine();