Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to BASE64 encode URLs?

Tags:

jquery

c#

base64

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 ?!

like image 860
Rashad Ahmad Avatar asked May 11 '26 07:05

Rashad Ahmad


1 Answers

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;
}
MessageBox.Show(DecodeFrom64("aHR0cDovL3d3dy5pbWRiLmNvbS90aXRsZS90dDA0MDE3Mjk="));

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)

like image 170
Eugene Ryabtsev Avatar answered May 12 '26 20:05

Eugene Ryabtsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!