StringHelpers
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的更多相关文章
随机推荐
- WPF简单拖拽功能实现
1.拖放操作有两个方面:源和目标. 2.拖放操作通过以下三个步骤进行: ①用户单击元素,并保持鼠标键为按下状态,启动拖放操作. ②用户将鼠标移到其它元素上.如果该元素可接受正在拖动的内容的类型,鼠标指 ...
- WPF动画之线性插值动画(1)
XAML代码: <Window x:Class="线性插值动画.MainWindow" xmlns="http://schemas.microsoft.com/wi ...
- Ngen生成Native代码实战及优缺点分析
先科普一下,.Net是一个用于Windows的托管代码模型,用于高效构建具有视觉上引人注目的用户体验的应用程序.但这个模型生成的代码并非可执行代码,而是由.Net公共语言运行库环境执行的IL代码.所以 ...
- C#中2、8、16进制 有符号转换10进制正负数
曾经让我苦想的其他进制转有符号整型问题,结果自己想到方法解决后才发现原来如此简单. 1.Int16(2个byte长度 ) : 方法 :Convert.ToInt16(进制编码,进制) a.16进制转1 ...
- jQuery 常用代码集锦
1. 选择或者不选页面上全部复选框 var tog = false; // or true if they are checked on load $('a').click(function() { ...
- HDU_2014 青年歌手大奖赛_评委会打分
Problem Description 青年歌手大奖赛中,评委会给参赛选手打分.选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分. Input 输入数据有多组,每 ...
- 九度OJ 1079 手机键盘
题目地址:http://ac.jobdu.com/problem.php?pid=1079 题目描述: 按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次, ...
- javascript 写策略模式,商场收银打折优惠策略
[Decode error - output not utf-8] ----------------------------- 购物清单 方便面 : 100 x 50 = 5000 | 4000 菊花 ...
- 采用python获得并修改文件编码(原创)
windows和linux采用了不同的编码,这让很多人伤透了脑经,这里我采用了Python的chardet库获得代码的编码,然后修改编码. 1.首先需要安装chardet库,有很多方式,我才用的是比较 ...
- [python]类与类中的列表
最近在用类中的列表时出现一件怪事 实例2中的列表,竟然有实例1中的数据. 查了半天发现是list的append方法的问题. 将全部的list.append(value) 换成 list = list ...