Skip to content

Instantly share code, notes, and snippets.

@igorushko
Created September 15, 2017 13:05
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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