Skip to content

Instantly share code, notes, and snippets.

@ichengzi
Forked from igorushko/Base64Url.cs
Created July 19, 2022 12:08
Show Gist options
  • Save ichengzi/b95b597d802ee8c5e9b2ffe5e4254e0c to your computer and use it in GitHub Desktop.
Save ichengzi/b95b597d802ee8c5e9b2ffe5e4254e0c 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