Encoding and Decoding Method in ASP.NET

Introduction

Below mentioned are 2 functions used for Encoding and Decoding the text.

ASPX Page

<table style="border-color: Black;">
    <tr>
        <td><asp:label id="lblText" runat="server" text="Enter text to be encoded" /></td>
        <td><asp:textbox id="txtText" runat="server" /></td>
        <td><asp:button id="btnSubmit" text="Submit" runat="server" onclick="btnSubmit_Click" /></td>
    </tr>
    <tr>
        <td><asp:label id="lblEncodedText" runat="server" /></td>
    </tr>

</
table>

.Aspx.cs Page

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string EncodingText = txtText.Text.Trim(); //Textbox used to enter values which needs to be encoded.
    EncodingText = Encoding.Base64DecodingMethod(EncodingText); // for decoding call decoding function.
    lblEncodedText.Text = EncodingText; // retreives the encoded/decoded data in label.
}
public
class Encoding
{
    //Encoding
    public static string Base64EncodingMethod(string Data)
    {
        byte[] toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(Data);
        string sReturnValues = System.Convert.ToBase64String(toEncodeAsBytes);
        return sReturnValues;
    }
    //Decoding
    public static string Base64DecodingMethod(string Data)
    {
        byte[] encodedDataAsBytes = System.Convert.FromBase64String(Data);
        string returnValue = System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
        return returnValue;
    }
}

Ebook Download
View all
Learn
View all