Created
January 2, 2017 14:25
-
-
Save surgicalcoder/378a7685baa174e4fac730851145ad6f to your computer and use it in GitHub Desktop.
URL Friendly Base64'ness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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