Monday, May 27, 2013

Base 64 Encoding in ASP.NET with C#

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding. Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML. Base64 is not encryption but simply encoding.

Base64 Encoding:

 private string base64Encode(string sData)
    {
        try
        {
            byte[] encData_byte = new byte[sData.Length];

            encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);

            string encodedData = Convert.ToBase64String(encData_byte);

            return encodedData;

        }
        catch (Exception ex)
        {
            throw new Exception("Error in base64Encode" + ex.Message);
        }
    }

Base64 Decoding:

 public string base64Decode(string sData)
    {

        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

        System.Text.Decoder utf8Decode = encoder.GetDecoder();

        byte[] todecode_byte = Convert.FromBase64String(sData);

        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

        char[] decoded_char = new char[charCount];

        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

        string result = new String(decoded_char);

        return result;

    }

Implementing in a login module:

try
        {
            var id = (from a in linq_obj.login_msts
                      select new
                      {
                          user_name = a.user_name,
                          password = a.password,
                          role = a.role,
                          int_glcode=a.int_glcode
                      }).ToList();
            int flag = 0;

            for (int i = 0; i < id.Count(); i++)
            {
                if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "Admin")
                {
                    flag = 1;
                    Session["username1"] = txtadmin.Text;
                    Session["referense"] = id[i].int_glcode;
                    break;
                }
                else if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "User")
                {
                    flag = 2;
                    Session["username1"] = txtadmin.Text;
                    Session["userid"] = id[i].user_name; // save a member Code
                    Session["referense"] = id[i].int_glcode;
                    //Session["code"] = id[i].login_id;
                    break;
                }
                else if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "Super Admin")
                {
                    flag = 3;
                    Session["username1"] = txtadmin.Text;
                    Session["userid"] = id[i].user_name; // save a member Code
                    Session["referense"] = id[i].int_glcode;
                    //Session["code"] = id[i].login_id;
                    break;
                }
                else if (id[i].user_name == txtadmin.Text && txtpassword.Text == base64Decode(id[i].password) && id[i].role == "Employee")
                {
                    flag = 4;
                    Session["username1"] = txtadmin.Text;
                    Session["userid"] = id[i].user_name; // save a member Code
                    Session["referense"] = id[i].int_glcode;
                    //Session["code"] = id[i].login_id;
                    break;
                }
            }
            if (flag == 1)
            {
                Response.Redirect("admin_welcome.aspx", false);
            }
            else if (flag == 2)
            {
                Response.Redirect("changePassword.aspx", false);
            }
            else if (flag == 3)
            {
                Response.Redirect("AddAdmin.aspx", false);
            }
            else if (flag == 4)
            {
                Response.Redirect("~\\style-demo.html", false);
            }

            else
            {
                Page.RegisterStartupScript("onload", "<script language='javascript'>alert('** Incorrect UserName or Password**')</script> ");
            }
            txtadmin.Text = "";
            txtpassword.Text = "";

        }
        catch (Exception ex)
        {
            Response.Write("<script laguage='javascript'>alert('** Some Error is occured During Login**')</Script>");
        }
        finally
        {
        }

All CPD Materials Download

CPD (Contributor Personality Development) is taught to all engineering branches of B.E. and M.E. under GTU (Gujarat Technological University). It is an objective type paper which has 100 marks weightage. I have about 32 files worth 50 MB. It has both old and new units and papers. It includes 7-14 units of old syllabus manual and all 1-15 units of new syllabus manual. It also has exam pattern information, GTU final paper and some test papers to be solved by yourself for practice.

To find more about CPD go to: http://www.knowcrazy.com/search?q=cpd

All CPD Materials Free Download Link: https://sites.google.com/site/09cegit/downloads

Top 10 websites for online learning

Here is the list of top 10 websites for e-learning. Mostly are related to programming and engineering. Share this post if you find it useful and also share your views by commenting or contact me on devharsh.knowcrazy.com or mail me on devharsh@facebook.com.

ICC Champions Trophy 2013 Official Broadcasters


  1. India: Star Cricket, ESPN, Doordarshan
  2. Pakistan: PTV Sports, Star Cricket, ESPN, Geo Super
  3. South Africa: Supersport 2 & 5
  4. Sri Lanka: CSN
  5. United Kingdom: Sky Sports
  6. United States: ESPN3.com
  7. New Zealand: Sky Sport
  8. Australia: Fox Sports