You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JwKdsV.Core.Utils
{
public class SafeBase64Utils
{
public static string ToBase64(byte[] bytes)
{
string b64 = Convert.ToBase64String(bytes);
return new StringBuilder(b64).Replace('+' , '-').Replace('/' , '_').Replace('=' , '.').ToString();
}
/// <summary>
/// Encodes string into a format that is safe for use with ASCII or HTTP query strings
/// </summary>
public static string ToBase64(string plainString)
{
byte[] bytes = Encoding.UTF8.GetBytes(plainString);
return ToBase64(bytes);
}
public static string Base64ToSafeBase64(string base64)
{
return new StringBuilder(base64).Replace('+' , '-').Replace('/' , '_').Replace('=' , '.').ToString();
}
public static string SafeBase64ToBase64(string safebase64)
{
return new StringBuilder(safebase64).Replace('-' , '+').Replace('_' , '/').Replace('.' , '=').ToString();
}
/// <summary>
/// Gives you back your original string from StringToHttpSafeBase64
/// </summary>
public static string FromSafeBase64(string base64String)
{
string b64 = new StringBuilder(base64String).Replace('-' , '+').Replace('_' , '/').Replace('.' , '=').ToString();
byte[] bytes = Convert.FromBase64String(b64);
return Encoding.UTF8.GetString(bytes , 0 , bytes.Length);
}
}
}