Created
September 15, 2017 13:05
-
-
Save igorushko/cccef0561aea7e46ae52bc62270b2b61 to your computer and use it in GitHub Desktop.
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