`
冷寒冰
  • 浏览: 243865 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

查询字符串加密解密

阅读更多

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
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics