public static byte[] Base64UrlDecode(this string arg) { var copy = (string)arg.Clone(); // 62nd & 63rd chars of encoding copy = SymbolsToReplace.Aggregate(copy, (current, tuple) => current.Replace(tuple.Item1, tuple.Item2)); // Pad with trailing '='s switch (copy.Length % 4) { case 0: break; // No pad chars in this case case 2: copy += "=="; break; // Two pad chars case 3: copy += "="; break; // One pad char default: throw new ArgumentException("Bad or corrupted Base64Url string!"); } return(Convert.FromBase64String(copy)); // Standard base64 decoder }
/// <summary> /// Encode an array by Base64 and convert for Url usage /// </summary> /// <param name="arg"></param> /// <returns></returns> public static string Base64UrlEncode(this byte[] arg) { var copy = Convert.ToBase64String(arg); // Remove any trailing '='s copy = copy.TrimEnd('='); // replace 62nd & 64rd chars of encoding return(SymbolsToReplace.Aggregate(copy, (current, tuple) => current.Replace(tuple.Item2, tuple.Item1))); }