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));
}
}

StringHelpers的更多相关文章

随机推荐

  1. (hdu)1042 N! 大数相乘

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1042 Problem Description Given an integer N( ≤ ...

  2. OpenGL_Qt学习笔记之_05(纹理映射)(转)

    转自:http://www.cnblogs.com/tornadomeet/archive/2012/08/24/2654719.html 纹理映射基础知识 什么叫纹理映射,一开始我也不明白,感觉这个 ...

  3. Stay Hungry, Stay Foolish--2005斯坦福大学05年毕业演讲

    转自http://www.cnblogs.com/daizhj/articles/1493813.html 斯蒂夫•保罗•乔布斯(Steve Paul Jobs,1955年2月24日出生-)是蘋果電腦 ...

  4. Google Test Frame 简单使用例子

    1 序言——为什么折腾Google Test 被逼无奈的. 最近研究google开源的基于列存储的数据库查询引擎supersonic源码.初略的浏览了一遍代码,竟然没有main函数,顿时惊讶的目瞪口呆 ...

  5. nodejs 实现机制

    最近在学习nodejs,作为一个从未学过javascript的程序员,把自己学习的过程贴出来,当做记录和总结吧. 1. nodejs的原理: nodejs完全是基于事件轮询机制的一个javascrip ...

  6. HTTP_USER_AGENT

    <!DOCTYPE html><html><head><meta charset="UTF-8" /><title>We ...

  7. tomcat server.xml配置文件 解析

      把服务拆分出来了.      前几天我也进行了拆分.可是当时服务起不来所以我想会不会有什么设置,使得这个服务在主机中只能启一个.然后我又找了一台服务器,也把代码放了进去.结果仿佛是我料想到的样子, ...

  8. linux文件属性权限相关

    一个linux目录或者文件,都会有一个所属主和所属组. 所属主,即文件的拥有者,而所属组,即该文件所属主所在的一个组. linux文件属性 包括文件类型 - d  l  b c s 依次表示 普通文件 ...

  9. javaScript中with的用法

    1 JavaScript中的with语句的作用是为逐级的对象访问提供命名空间式的速写方式, 也就是在指定的代码区域, 直接通过节点名称调用对象 初次接触到with用法,是这样一段代码: 1 2 3 4 ...

  10. sonar-maven-plugin问题

    问题: jenkins本地构建时sonar报错 StackOverflow问题 [ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven ...