Skip to content

Instantly share code, notes, and snippets.

@igorushko
Created September 15, 2017 13:05
Show Gist options
  • Save igorushko/cccef0561aea7e46ae52bc62270b2b61 to your computer and use it in GitHub Desktop.
Save igorushko/cccef0561aea7e46ae52bc62270b2b61 to your computer and use it in GitHub Desktop.
Base64Url C#
public static class Base64Url
{
public static string Encode(byte[] arg)
{
if (arg == null)
{
throw new ArgumentNullException("arg");
}
var s = Convert.ToBase64String(arg);
return s
.Replace("=", "")
.Replace("/", "_")
.Replace("+", "-");
}
public static string ToBase64(string arg)
{
if (arg == null)
{
throw new ArgumentNullException("arg");
}
var s = arg
.PadRight(arg.Length + (4 - arg.Length % 4) % 4, '=')
.Replace("_", "/")
.Replace("-", "+");
return s;
}
public static byte[] Decode(string arg)
{
var decrypted = ToBase64(arg);
return Convert.FromBase64String(decrypted);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment