public class StringHelpers
{
public const char QUERY_STRING_DELIMITER = '&'; private static RijndaelManaged _cryptoProvider;
//128 bit encyption: DO NOT CHANGE
private static readonly byte[] Key = { , , , , , , , , , , , , , , , };
private static readonly byte[] IV = { , , , , , , , , , , , , , , , };
static StringHelpers()
{
_cryptoProvider = new RijndaelManaged();
_cryptoProvider.Mode = CipherMode.CBC;
_cryptoProvider.Padding = PaddingMode.PKCS7;
} public static string Encrypt(string unencryptedString)
{
return Encrypt(unencryptedString, string.Empty);
} public static string Encrypt(string unencryptedString, string myKey)
{ //byte[] bytIn = ASCIIEncoding.ASCII.GetBytes(unencryptedString);
//如果內容有unicode的話,要用utf8編碼
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(unencryptedString);
byte[] bytKey;
if(string.IsNullOrEmpty(myKey)){
bytKey = Key;
}else{
bytKey = ASCIIEncoding.ASCII.GetBytes(myKey);
Array.Resize(ref bytKey, );
}
// Create a MemoryStream
MemoryStream ms = new MemoryStream();
// Create Crypto Stream that encrypts a stream
CryptoStream cs = new CryptoStream(ms, _cryptoProvider.CreateEncryptor(bytKey, IV), CryptoStreamMode.Write);
// Write content into MemoryStream
cs.Write(bytIn, , bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.ToArray();
//因為url不能吃+所以要轉成@@@
return Convert.ToBase64String(bytOut).Replace("+", "@@@");
} public static string Decrypt(string encryptedString)
{
return Decrypt(encryptedString, string.Empty);
} public static string Decrypt(string encryptedString, string myKey)
{
if (encryptedString.Trim().Length != )
{
//如果有人改加密字串的話,解就會發生錯誤,所以錯誤就回傳空字串
try
{
// Convert from Base64 to binary 在解開前要先將@@@轉成+
byte[] bytIn = Convert.FromBase64String(encryptedString.Replace("@@@", "+"));
byte[] bytKey;
if(string.IsNullOrEmpty(myKey)){
bytKey = Key;
}else{
bytKey = ASCIIEncoding.ASCII.GetBytes(myKey);
Array.Resize(ref bytKey, );
}
// Create a MemoryStream
MemoryStream ms = new MemoryStream(bytIn, , bytIn.Length);
// Create a CryptoStream that decrypts the data
CryptoStream cs = new CryptoStream(ms, _cryptoProvider.CreateDecryptor(bytKey, IV), CryptoStreamMode.Read); // Read the Crypto Stream
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
catch
{
return "";
}
}
else
{
return "";
}
} public static NameValueCollection DecryptQueryString(string queryString)
{
return DecryptQueryString(queryString, string.Empty);
} public static NameValueCollection DecryptQueryString(string queryString, string myKey)
{
if (queryString.Length != )
{
//Decode the string
string decodedQueryString = HttpUtility.UrlDecode(queryString);
//Decrypt the string
string decryptedQueryString = StringHelpers.Decrypt(decodedQueryString, myKey );
//Now split the string based on each parameter
string[] actionQueryString = decryptedQueryString.Split(new char[] { QUERY_STRING_DELIMITER });
NameValueCollection newQueryString = new NameValueCollection();
//loop around for each name value pair.
for (int index = ; index < actionQueryString.Length; index++)
{
string[] queryStringItem = actionQueryString[index].Split(new char[] { '=' });
if(queryStringItem.Length > )
newQueryString.Add(queryStringItem[], queryStringItem[]);
}
return newQueryString;
}
else
{
//No query string was passed in.
return null;
}
} public static string EncryptQueryString(NameValueCollection queryString)
{
return EncryptQueryString(queryString, string.Empty);
} public static string EncryptQueryString(NameValueCollection queryString, string myKey)
{
//create a string for each value in the query string passed in.
string tempQueryString = "";
for (int index = ; index < queryString.Count; index++)
{
tempQueryString += queryString.GetKey(index) + "=" + queryString[index];
if (index != queryString.Count - )
{
tempQueryString += QUERY_STRING_DELIMITER;
}
}
return EncryptQueryString(tempQueryString, myKey);
} public static string EncryptQueryString(string queryString)
{
return EncryptQueryString(queryString, string.Empty);
} public static string EncryptQueryString(string queryString, string myKey)
{
return "?" + HttpUtility.UrlEncode(StringHelpers.Encrypt(queryString, myKey));
}
}