查询字符串加密解密_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 查询字符串加密解密

查询字符串加密解密

 2010/12/28 8:16:15  冷寒冰  http://pmandy-163-com.javaeye.com  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Xml.Linq;usingSystem.Security.Cryptography;usingSystem.IO;usingSystem.Text;usingSystem.Collections.Specialized;usingSystem.Web.UI;///<summary>
  • 标签:字符串 字符串加密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

?

using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Collections.Specialized;
using System.Web.UI;

/// <summary>
///StringHelpers 的摘要说明
/// </summary>
public class StringHelpers
{
??
??? #region constants
??? private const char QUERY_STRING_DELIMITER='&';
??? #endregion
??? #region Members
??? private static RijndaelManaged _cryptoProvider;
??? //128 bit encyption:DO NOT Change
??? private static readonly byte[] key = { 18, 19, 8, 24, 36, 22, 4, 22, 17, 5, 11, 9, 13, 15, 06, 23 };
??? //initialize vectors(初始化向量)
??? private static readonly byte[] IV = { 14, 2, 16, 7, 5, 9, 17, 8, 4, 47, 16, 12, 1, 32, 25, 18 };
??? #endregion

??? #region? constructor
??? public StringHelpers()
??? {
??????? _cryptoProvider = new RijndaelManaged();
??????? _cryptoProvider.Mode = CipherMode.CBC;
??????? _cryptoProvider.Padding = PaddingMode.PKCS7;
???????
??? }
??? #endregion

??? #region Methods
??? /// <summary>
??? /// Encrypts a given string.
??? /// </summary>
??? /// <param name="unencryptedString"></param>
??? /// <returns>returns an encrypted string</returns>
??? public static string Encrypt(string unencryptedString)
??? {
??????? byte[] bytIn = ASCIIEncoding.ASCII.GetBytes(unencryptedString);
??????? //create m MemoryStream
??????? MemoryStream ms = new MemoryStream();
??????? //create? crypto(密码 加密 模块) stream that encrypts a stream
??????? CryptoStream cs = new CryptoStream(ms, _cryptoProvider.CreateEncryptor(key, IV), CryptoStreamMode.Write);
??????? //write content into MemoryStream
??????? cs.Write(bytIn, 0, bytIn.Length);
??????? cs.FlushFinalBlock();
??????? byte[] bytOut = ms.ToArray();
??????? return Convert.ToBase64String(bytOut);
??? }
??? /// <summary>
??? /// Decrypt a given string.
??? /// </summary>
??? /// <param name="encryptedString"></param>
??? /// <returns></returns>
??? public static string Decrypt(string encryptedString)
??? {
??????? if (encryptedString.Trim().Length != 0)
??????? {
??????????? byte[] bytIn = Convert.FromBase64String(encryptedString);
??????????? //created a memorystream
??????????? MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
??????????? //create a cryptoStream that decrypts the data
??????????? CryptoStream cs = new CryptoStream(ms, _cryptoProvider.CreateDecryptor(key, IV), CryptoStreamMode.Read);
??????????? //read the crypto stream
??????????? StreamReader sr = new StreamReader(cs);
??????????? return sr.ReadToEnd();

??????? }
??????? else
??????? {
??????????? return "";
??????? }
??? }

??? public static NameValueCollection DecryptQueryString(string queryString)
??? {
??????? if (queryString.Length != 0)
??????? {
??????????? //Decode the string
??????????? string decodedQueryString = HttpUtility.UrlDecode(queryString);
??????????? //Decrypt the string
??????????? string decryptedQueryString = StringHelpers.Decrypt(decodedQueryString);
??????????? //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 = 0; index < actionQueryString.Length; index++)
??????????? {
??????????????? string[] queryStringItem = actionQueryString[index].Split(new char[] { '=' });
??????????????? newQueryString.Add(queryStringItem[0], queryStringItem[1]);
??????????? }
??????????? return newQueryString;
??????? }
??????? else
??????? {
??????????? //No query string was passed in.
??????????? return null;
??????? }
??? }

??? public static string EncryptQueryString(NameValueCollection queryString)
??? {
??????? //create a string for each value in the query string passed in.
??????? string tempQueryString = "";
??????? for (int index = 0; index < queryString.Count; index++)
??????? {
??????????? tempQueryString+=queryString.GetKey(index)+"="+queryString[index];
??????????? if (index != queryString.Count - 1)
??????????? {
??????????????? tempQueryString += QUERY_STRING_DELIMITER;
??????????? }
??????? }
??????? return EncryptQueryString(tempQueryString);
??? }
??? public static string EncryptQueryString(string queryString)
??? {
??????? return "?" + HttpUtility.UrlEncode(StringHelpers.Encrypt(queryString));
??? }
??? #endregion
}

发表评论
用户名: 匿名