3

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

deej
  • 2,536
  • 4
  • 29
  • 51
Rashad Ahmad
  • 71
  • 1
  • 1
  • 6

5 Answers5

8

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)

Community
  • 1
  • 1
Eugene Ryabtsev
  • 2,232
  • 1
  • 23
  • 37
5

It is better to use code below from https://stackoverflow.com/a/1789179

///<summary>
/// Base 64 Encoding with URL and Filename Safe Alphabet using UTF-8 character set.
///</summary>
///<param name="str">The origianl string</param>
///<returns>The Base64 encoded string</returns>
public static string Base64ForUrlEncode(string str)
{
    byte[] encbuff = Encoding.UTF8.GetBytes(str);
    return HttpServerUtility.UrlTokenEncode(encbuff);
}
///<summary>
/// Decode Base64 encoded string with URL and Filename Safe Alphabet using UTF-8.
///</summary>
///<param name="str">Base64 code</param>
///<returns>The decoded string.</returns>
public static string Base64ForUrlDecode(string str)
{
    byte[] decbuff = HttpServerUtility.UrlTokenDecode(str);
    return Encoding.UTF8.GetString(decbuff);
}

Reason is that Base64 includes invalid URL characters.

Community
  • 1
  • 1
David Hardin
  • 167
  • 1
  • 6
2

JavaScript-

var encodedStr = window.btoa("StringToEncode");

var decodedStr = window.atob( encodedStr );  //"StringToEncode"
Kara
  • 6,115
  • 16
  • 50
  • 57
Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76
0

I don't think that the accepted answer from Eugene Ryabtsev is correct. If you try it with "\xff", you'll see that :

DecodeFrom64(EncodeTo64("\xff")) == "?" (i.e. "\x3f")

The reason is that ASCIIEncoding doesn't go further than the code 127. All characters from 128 to 255 won't be understood and will be converted to "?".

So, an extended encoding is necessary as follow :

static public string EncodeTo64(string toEncode) {
    var e = Encoding.GetEncoding("iso-8859-1");
    byte[] toEncodeAsBytes = e.GetBytes(toEncode);
    string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
    return returnValue;
}
static public string DecodeFrom64(string encodedData) {
    var e = Encoding.GetEncoding("iso-8859-1");
    byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
    string returnValue = e.GetString(encodedDataAsBytes);
    return returnValue;
}
user217447
  • 143
  • 2
  • 3
  • There are only two encodings on the net: ASCII and UTF-8. Using anything else is either madness or applied archeology, depending on intent. Use UTF-8 and get an upvote. – Eugene Ryabtsev Jul 06 '16 at 06:19
0

To encode/decode strings to/from Base64 Url Safe (more info: https://en.wikipedia.org/wiki/Base64#URL_applications) best way I found out it's using Base64UrlEncoder from Microsoft.IdentityModel.Tokens namespace https://learn.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.tokens.base64urlencoder?view=msal-web-dotnet-latest

var enc = Base64UrlEncoder.Encode("https://stackoverflow.com/questions/10773339/how-to-base64-encode-urls");
var dec = Base64UrlEncoder.Decode(enc);
Piotr
  • 21
  • 2