Base64Url C#
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 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