原文:http://www.verydemo.com/demo_c441_i2868.html
采用 Base64 编码不仅比较简短,同时具有不可读性,被编码的数据不会被人用肉眼所直接看到。 但是,标准的 Base64 并不适合直接放在 URL 里使用,因为Base64中含有'+' 和 '/', URL 编码器会 '+' 和 '/' 变为形如 "%XX” 的形式。
修改一下Base64就可以在URL中使用了:
///
<summary>
/// Base64 转换 UrlBase64,使之适合在url中使用
/// </summary>
public static string Base64ToUrlBase64( string base64str)
{
// "+" 换成 "-A"
// "/" 换成 "-S"
// 去掉 "="
return base64str.Replace( " + ", " -A ").Replace( " / ", " -S ").Replace( " = ", string.Empty);
}
/// <summary>
/// UrlBase64 转换 Base64
/// </summary>
public static string UrlBase64ToBase64( string urlBase64str)
{
// "-A" 换成 "+"
// "-S" 换成 "/"
string str = urlBase64str.Replace( " -A ", " + ").Replace( " -S ", " / ");
// 添加"="
int mod = str.Length % 4;
if (mod != 0)
{
str += new string( ' = ', 4 - mod);
}
return str;
}
/// Base64 转换 UrlBase64,使之适合在url中使用
/// </summary>
public static string Base64ToUrlBase64( string base64str)
{
// "+" 换成 "-A"
// "/" 换成 "-S"
// 去掉 "="
return base64str.Replace( " + ", " -A ").Replace( " / ", " -S ").Replace( " = ", string.Empty);
}
/// <summary>
/// UrlBase64 转换 Base64
/// </summary>
public static string UrlBase64ToBase64( string urlBase64str)
{
// "-A" 换成 "+"
// "-S" 换成 "/"
string str = urlBase64str.Replace( " -A ", " + ").Replace( " -S ", " / ");
// 添加"="
int mod = str.Length % 4;
if (mod != 0)
{
str += new string( ' = ', 4 - mod);
}
return str;
}