1

I am using this encoding/decoding javascript 64bit from here http://www.webtoolkit.info/javascript-base64.html

so here is the scanrio: from JavaScript i am redireting to a new aspx page and on the page_load i am reading the QueryString id.

Everything working fine but the question is, if i want to encode/decode in asp.net in codebehind how would i do?

i am planning to encode before i redirect to a page from .JS but how would i read the encoded in asp.net code behind?

0

1 Answer 1

1

Everything working fine but the question is, if i want to encode/decode in asp.net in codebehind how would i do?

use below methods in code behind to encode and decode.

static public string DecodeFrom64(string encodedData)
{
    byte[] encodedDataAsBytes
        = System.Convert.FromBase64String(encodedData);
    string returnValue =
        System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
    return returnValue;
}

static public string EncodeTo64(string toEncode)
{
    byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
    string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
    return returnValue;
}

i am planning to encode before i redirect to a page from .JS but how would i read the encoded in asp.net code behind?

you can call DecodeFrom64 method to decode encoded text using JS.

Test :

 input = "Abu Hamzah"
 JS encoded text = "QWJ1IEhhbXphaA=="

DecodeFrom64("QWJ1IEhhbXphaA==") result is "Abu Hamzah" and EncodeTo64("Abu Hamzah") result is "QWJ1IEhhbXphaA==" as expected.

Edit:

Add base64 encode decode java script

<script type="text/javascript" src="/js/webtoolkit.base64.js">

you can download it from here http://www.webtoolkit.info/djs/webtoolkit.base64.js

in your javascript you can call this method as below

window.location.href="mypage.aspx?id=" + Base64.encode('Test');

if you want to decode this query string parameter from server side, then you can use DecodeFrom64 method

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for the detail answer, but i am still not sure, how the encode/decode will work from js, somethign like this.. window.location.href="mypage.aspx?id= + EncodeTo64("Test") ? how does the javascript have a ref to EncodeTo64 method?
you don't need to call C# method from js for encode and decode. use js functions for that. when you need to do it from code behind you can use C# methods. both encode and decode will give you same results.
i am getting this error Microsoft JScript runtime error: Object expected i tried using like this in js window.location.href="mypage.aspx?id= + EncodeTo64("Test")
i am getting this error now in DecodeFrom64 method Invalid character in a Base-64 string.
i have a guid id 37945704-cf86-4b2e-a4b5-0db0204902c8
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.