Skip to content

Instantly share code, notes, and snippets.

@surgicalcoder
Created January 2, 2017 14:25
Show Gist options
  • Save surgicalcoder/378a7685baa174e4fac730851145ad6f to your computer and use it in GitHub Desktop.
Save surgicalcoder/378a7685baa174e4fac730851145ad6f to your computer and use it in GitHub Desktop.
URL Friendly Base64'ness
public static class Base64Extension
{
static readonly char[] padding = { '=' };
public static string EncodeURLBase64(this string Input)
{
var base64 = Convert.ToBase64String(Encoding.Default.GetBytes(Input)).TrimEnd(padding).Replace('+', '-').Replace('/', '_');
return base64;
}
public static string DecodeURLBase64(this string Input)
{
string incoming = Input.Replace('_', '/').Replace('-', '+');
switch (Input.Length % 4)
{
case 2: incoming += "=="; break;
case 3: incoming += "="; break;
}
byte[] bytes = Convert.FromBase64String(incoming);
string originalText = Encoding.Default.GetString(bytes);
return originalText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment