Rashad Ahmad Rashad Ahmad - 8 months ago 71
jQuery Question

How to BASE64 encode URLs?

I was browsing torrents at KickAssTorrents.com, when I visit a link it is encoded in the url in a different way;

for example (IMDb link): http://kat.ph/account/confirm/url/aHR0cDovL3d3dy5pbWRiLmNvbS90aXRsZS90dDA0MDE3Mjk=/

Now, when I change a character something changes in the link and I want to know how to do this in C# or jQuery ?!


In C#:

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

Use System.Text.UTF8Encoding.UTF8.GetBytes(...) if string toEncode contains characters outside of ASCII. Note that in this case any party that decodes the URL will have to be able to correctly handle these characters.

Also look at the case of =, + and / mentioned by David Hardin to see if any of the problems mentioned apply to you. Or just use David's answer.

jQuery: google 'jquery base64 encode' (the site plugins.jquery.com seems to be offline at the moment, so I cannot check it for sure)