• 1145 阅读
  • 4 回复

常用类收集 [置顶]

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)
本主题由 张小鱼 于 2015/2/4 12:36:00 执行 设置置顶 [查看记录]
Asp.net,C# 加密解密字符串
首先在web.config | app.config 文件下增加如下代码:

代码 复制 - 运行

<?xml version="1.0"?>
  <configuration>
    <appSettings>
      <add key="IV" value="SuFjcEmp/TE="/>
      <add key="Key" value="KIPSToILGp6fl+3gXJvMsN4IajizYBBT"/>
    </appSettings>
  </configuration>
 



IV:加密算法的初始向量。
Key:加密算法的密钥。

接着新建类CryptoHelper,作为加密帮助类。
首先要从配置文件中得到IV 和Key。所以基本代码如下:


代码 复制 - 运行

public class CryptoHelper
        {
            //private readonly string IV = "SuFjcEmp/TE=";
            private readonly string IV = string.Empty;
            //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
            private readonly string Key = string.Empty;

            /// <summary>
            ///构造函数
            /// </summary>
            public CryptoHelper()
            {
                IV = ConfigurationManager.AppSettings["IV"];
                Key = ConfigurationManager.AppSettings["Key"];
            }
        }



注意添加System.Configuration.dll程序集引用。

在获得了IV 和Key 之后,需要获取提供加密服务的Service 类。
在这里,使用的是System.Security.Cryptography; 命名空间下的TripleDESCryptoServiceProvider类。
获取TripleDESCryptoServiceProvider 的方法如下:

代码 复制 - 运行

/// <summary>
        /// 获取加密服务类
        /// </summary>
        /// <returns></returns>
        private TripleDESCryptoServiceProvider GetCryptoProvider()
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();

            provider.IV = Convert.FromBase64String(IV);
            provider.Key = Convert.FromBase64String(Key);

            return provider;
        }



TripleDESCryptoServiceProvider 两个有用的方法
CreateEncryptor:创建对称加密器对象ICryptoTransform.
CreateDecryptor:创建对称解密器对象ICryptoTransform
加密器对象和解密器对象可以被CryptoStream对象使用。来对流进行加密和解密。
cryptoStream 的构造函数如下:
public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode);
使用transform 对象对stream 进行转换。

完整的加密字符串代码如下:

代码 复制 - 运行

/// <summary>
        /// 获取加密后的字符串
        /// </summary>
        /// <param name="inputValue">输入值.</param>
        /// <returns></returns>
        public string GetEncryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();

            // 创建内存流来保存加密后的流
            MemoryStream mStream = new MemoryStream();

            // 创建加密转换流
            CryptoStream cStream = new CryptoStream(mStream,
            provider.CreateEncryptor(), CryptoStreamMode.Write);

            // 使用UTF8编码获取输入字符串的字节。
            byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue);

            // 将字节写到转换流里面去。
            cStream.Write(toEncrypt, 0, toEncrypt.Length);
            cStream.FlushFinalBlock();

            // 在调用转换流的FlushFinalBlock方法后,内部就会进行转换了,此时mStream就是加密后的流了。
            byte[] ret = mStream.ToArray();

            // Close the streams.
            cStream.Close();
            mStream.Close();

            //将加密后的字节进行64编码。
            return Convert.ToBase64String(ret);
        }



解密方法也类似:

代码 复制 - 运行

/// <summary>
        /// 获取解密后的值
        /// </summary>
        /// <param name="inputValue">经过加密后的字符串.</param>
        /// <returns></returns>
        public string GetDecryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();

            byte[] inputEquivalent = Convert.FromBase64String(inputValue);

            // 创建内存流保存解密后的数据
            MemoryStream msDecrypt = new MemoryStream();

            // 创建转换流。
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                                                        provider.CreateDecryptor(),
                                                        CryptoStreamMode.Write);

            csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);

            csDecrypt.FlushFinalBlock();
            csDecrypt.Close();

            //获取字符串。
            return new UTF8Encoding().GetString(msDecrypt.ToArray());
        }
 

 



完整的CryptoHelper代码如下:

代码 复制 - 运行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public class CryptoHelper
    {
        //private readonly string IV = "SuFjcEmp/TE=";
        private readonly string IV = string.Empty;
        //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
        private readonly string Key = string.Empty;

        public CryptoHelper()
        {
            IV = ConfigurationManager.AppSettings["IV"];
            Key = ConfigurationManager.AppSettings["Key"];
        }

        /// <summary>
        /// 获取加密后的字符串
        /// </summary>
        /// <param name="inputValue">输入值.</param>
        /// <returns></returns>
        public string GetEncryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();

            // 创建内存流来保存加密后的流
            MemoryStream mStream = new MemoryStream();

            // 创建加密转换流
            CryptoStream cStream = new CryptoStream(mStream,

            provider.CreateEncryptor(), CryptoStreamMode.Write);
            // 使用UTF8编码获取输入字符串的字节。
            byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue);

            // 将字节写到转换流里面去。
            cStream.Write(toEncrypt, 0, toEncrypt.Length);
            cStream.FlushFinalBlock();

            // 在调用转换流的FlushFinalBlock方法后,内部就会进行转换了,此时mStream就是加密后的流了。
            byte[] ret = mStream.ToArray();

            // Close the streams.
            cStream.Close();
            mStream.Close();

            //将加密后的字节进行64编码。
            return Convert.ToBase64String(ret);
        }

        /// <summary>
        /// 获取加密服务类
        /// </summary>
        /// <returns></returns>
        private TripleDESCryptoServiceProvider GetCryptoProvider()
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();

            provider.IV = Convert.FromBase64String(IV);
            provider.Key = Convert.FromBase64String(Key);

            return provider;

        }

        /// <summary>
        /// 获取解密后的值
        /// </summary>
        /// <param name="inputValue">经过加密后的字符串.</param>
        /// <returns></returns>
        public string GetDecryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
            byte[] inputEquivalent = Convert.FromBase64String(inputValue);

            // 创建内存流保存解密后的数据
            MemoryStream msDecrypt = new MemoryStream();

            // 创建转换流。
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
            provider.CreateDecryptor(),
            CryptoStreamMode.Write);

            csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
            csDecrypt.FlushFinalBlock();

            csDecrypt.Close();

            //获取字符串。
            return new UTF8Encoding().GetString(msDecrypt.ToArray());
        }
    }
}



使用例子:


原文地址:http://blogs.msdn.com/b/nikhiln/archive/2008/05/18/net-2-0-symmetric-encryption-code-sample.aspx


http://www.cnblogs.com/LoveJenny/archive/2011/06/15/2081116.html


小鱼整理:

代码 复制 - 运行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;

namespace FishSoft.Common
{
    /// <summary>
    /// 如果是GET传输时需要用System.Web.HttpUtility.UrlEncode()来编码一下
    /// 因为有+等传输会变成空格
    /// http://www.cnblogs.com/LoveJenny/archive/2011/06/15/2081116.html
    /// 原文地址:http://blogs.msdn.com/b/nikhiln/archive/2008/05/18/net-2-0-symmetric-encryption-code-sample.aspx
    /// </summary>
    public class CryptoHelper
    {
        //private readonly string IV = "SuFjcEmp/TE=";
        private readonly string IV = string.Empty;//IV:加密算法的初始向量。
        //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
        private readonly string Key = string.Empty;//Key:加密算法的密钥。
        public CryptoHelper()
        {
            //注意添加System.Configuration.dll程序集引用。
            IV = ConfigurationManager.AppSettings["IV"];
            Key = ConfigurationManager.AppSettings["Key"];
        }
        /// <summary>
        /// 获取加密后的字符串
        /// </summary>
        /// <param name="inputValue">输入值.</param>
        /// <returns></returns>
        public string GetEncryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
            // 创建内存流来保存加密后的流
            MemoryStream mStream = new MemoryStream();
            // 创建加密转换流
            CryptoStream cStream = new CryptoStream(mStream,
            provider.CreateEncryptor(), CryptoStreamMode.Write);
            // 使用UTF8编码获取输入字符串的字节。
            byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue);
            // 将字节写到转换流里面去。
            cStream.Write(toEncrypt, 0, toEncrypt.Length);
            cStream.FlushFinalBlock();
            // 在调用转换流的FlushFinalBlock方法后,内部就会进行转换了,此时mStream就是加密后的流了。
            byte[] ret = mStream.ToArray();
            // Close the streams.
            cStream.Close();
            mStream.Close();
            //将加密后的字节进行64编码。
            return Convert.ToBase64String(ret);
            //return Convert.ToBase64String(ret).Replace("+", "%2B").Replace("=", "%3d");
        }
        /// <summary>
        /// 获取加密服务类
        /// </summary>
        /// <returns></returns>
        private TripleDESCryptoServiceProvider GetCryptoProvider()
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
            provider.IV = Convert.FromBase64String(IV);
            provider.Key = Convert.FromBase64String(Key);
            return provider;
        }
        /// <summary>
        /// 获取解密后的值
        /// </summary>
        /// <param name="inputValue">经过加密后的字符串.</param>
        /// <returns></returns>
        public string GetDecryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
            byte[] inputEquivalent = Convert.FromBase64String(inputValue);
            // 创建内存流保存解密后的数据
            MemoryStream msDecrypt = new MemoryStream();
            // 创建转换流。
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
            provider.CreateDecryptor(),
            CryptoStreamMode.Write);
            csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
            csDecrypt.FlushFinalBlock();
            csDecrypt.Close();
            //获取字符串。
            return new UTF8Encoding().GetString(msDecrypt.ToArray());
        }
    }
}




FishSoft.Common.CryptoHelper ch = new FishSoft.Common.CryptoHelper();
string verify = ch.GetEncryptedValue(“字符串”);


如果是GET传输时需要用System.Web.HttpUtility.UrlEncode()来编码一下
因为有+等传输会变成空格

小鱼的淘宝店铺-多多支持哇

完整的DataTable和list转换 老外写的

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

代码 复制 - 运行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.ComponentModel;
namespace FishSoft.Common
{
    //http://blog.csdn.net/educast/article/details/5256137
    //完整的DataTable和list转换 老外写的
    public class CollectionHelper
    {
        private CollectionHelper()
        {
        }

        public static DataTable ConvertTo<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach(T item in list)
            {
                DataRow row = table.NewRow();

                foreach(PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item);
                }

                table.Rows.Add(row);
            }

            return table;
        }

        public static IList<T> ConvertTo<T>(IList<DataRow> rows)
        {
            IList<T> list = null;

            if(rows != null)
            {
                list = new List<T>();

                foreach(DataRow row in rows)
                {
                    T item = CreateItem<T>(row);
                    list.Add(item);
                }
            }

            return list;
        }

        public static IList<T> ConvertTo<T>(DataTable table)
        {
            if(table == null)
            {
                return null;
            }

            List<DataRow> rows = new List<DataRow>();

            foreach(DataRow row in table.Rows)
            {
                rows.Add(row);
            }

            return ConvertTo<T>(rows);
        }

        public static T CreateItem<T>(DataRow row)
        {
            T obj = default(T);
            if(row != null)
            {
                obj = Activator.CreateInstance<T>();

                foreach(DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        prop.SetValue(obj, value, null);
                    }
                    catch
                    {
                        // You can log something here  
                        throw;
                    }
                }
            }

            return obj;
        }

        public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach(PropertyDescriptor prop in properties)
            {
                table.Columns.Add(prop.Name, prop.PropertyType);
            }

            return table;
        }

        //http://www.okbase.net/doc/details/3282 ============================
        //通用类,可以把DataTable转换为任何模型,呵呵,这就需要利用反射和泛型了 
       // 使用方式:  
        // 获得查询结果  
        //DataTable dt = DbHelper.ExecuteDataTable(...);  
        // 把DataTable转换为IList<UserInfo>  
        //IList<UserInfo> users = ModelConvertHelper<UserInfo>.ConvertToModel(dt);
        /// <summary>    
        /// 实体转换辅助类    
        /// </summary>    
        public class ModelConvertHelper<T> where T : new()
        {
            public static IList<T> ConvertToModel(DataTable dt)
            {
                // 定义集合    
                IList<T> ts = new List<T>();

                // 获得此模型的类型   
                Type type = typeof(T);
                string tempName = "";

                foreach(DataRow dr in dt.Rows)
                {
                    T t = new T();
                    // 获得此模型的公共属性      
                    PropertyInfo[] propertys = t.GetType().GetProperties();
                    foreach(PropertyInfo pi in propertys)
                    {
                        tempName = pi.Name;  // 检查DataTable是否包含此列    

                        if(dt.Columns.Contains(tempName))
                        {
                            // 判断此属性是否有Setter      
                            if(!pi.CanWrite) continue;

                            object value = dr[tempName];
                            if(value != DBNull.Value)
                                pi.SetValue(t, value, null);
                        }
                    }
                    ts.Add(t);
                }
                return ts;
            }
        }    
    }  
}




调用:

代码 复制 - 运行

IList<FishSoft.Model.Partake> nList = FishSoft.Common.CollectionHelper.ModelConvertHelper<FishSoft.Model.Partake>.ConvertToModel(dt);
helper.Put("AllCount", nList.Count);//输出数据列表
helper.Put("CommentList", nList);//输出数据列表
小鱼的淘宝店铺-多多支持哇

楼上一个新的

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

作者:陈太汉
DataTable转换成IList

在用C#作开发的时候经常要把DataTable转换成IList;操作DataTable比较麻烦,把DataTable转换成IList,以对象实体作为IList的元素,操作起来就非常方便。
注意:实体的属性必须和数据库中的字段必须一一对应,或者数据库字段名.ToLower().Contains(实体属性名.ToLower())
         数据类型暂时至支持int、string、DateTime、float、double


代码 复制 - 运行

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace TBToListTest
{
    public class TBToList<T> where T : new()
    {
        /// <summary>
        /// 获取列名集合
        /// </summary>
        private IList<string> GetColumnNames(DataColumnCollection dcc)
        {
            IList<string> list = new List<string>();
            foreach (DataColumn dc in dcc)
            {
                list.Add(dc.ColumnName);
            }
            return list;
        }

        /// <summary>
        ///属性名称和类型名的键值对集合
        /// </summary>
        private Hashtable GetColumnType(DataColumnCollection dcc)
        {
            if (dcc == null || dcc.Count == 0)
            {
                return null;
            }
            IList<string> colNameList = GetColumnNames(dcc);

            Type t = typeof(T);
            PropertyInfo[] properties = t.GetProperties();
            Hashtable hashtable = new Hashtable();
            int i = 0;
            foreach (PropertyInfo p in properties)
            {
                foreach (string col in colNameList)
                {
                    if (col.ToLower().Contains(p.Name.ToLower()))
                    {
                        hashtable.Add(col, p.PropertyType.ToString() + i++);
                    }
                }
            }

            return hashtable;
        }

        /// <summary>
        /// DataTable转换成IList
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IList<T> ToList(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }

            PropertyInfo[] properties = typeof(T).GetProperties();//获取实体类型的属性集合
            Hashtable hh = GetColumnType(dt.Columns);//属性名称和类型名的键值对集合
            IList<string> colNames = GetColumnNames(hh);//按照属性顺序的列名集合
            List<T> list = new List<T>();
            T model = default(T);
            foreach (DataRow dr in dt.Rows)
            {
                model = new T();//创建实体
                int i = 0;
                foreach (PropertyInfo p in properties)
                {
                    if (p.PropertyType == typeof(string))
                    {
                        p.SetValue(model, dr[colNames[i++]], null);
                    }
                    else if (p.PropertyType == typeof(int))
                    {
                        p.SetValue(model, int.Parse(dr[colNames[i++]].ToString()), null);
                    }
                    else if (p.PropertyType == typeof(DateTime))
                    {
                        p.SetValue(model, DateTime.Parse(dr[colNames[i++]].ToString()), null);
                    }
                    else if (p.PropertyType == typeof(float))
                    {
                        p.SetValue(model, float.Parse(dr[colNames[i++]].ToString()), null);
                    }
                    else if (p.PropertyType == typeof(double))
                    {
                        p.SetValue(model, double.Parse(dr[colNames[i++]].ToString()), null);
                    }
                }

                list.Add(model);
            }

            return list;
        }

        /// <summary>
        /// 按照属性顺序的列名集合
        /// </summary>
        private IList<string> GetColumnNames(Hashtable hh)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();//获取实体类型的属性集合
            IList<string> ilist = new List<string>();
            int i = 0;
            foreach (PropertyInfo p in properties)
            {
                ilist.Add(GetKey(p.PropertyType.ToString() + i++, hh));
            }
            return ilist;
        }

        /// <summary>
        /// 根据Value查找Key
        /// </summary>
        private string GetKey(string val, Hashtable tb)
        {
            foreach (DictionaryEntry de in tb)
            {
                if (de.Value.ToString() == val)
                {
                    return de.Key.ToString();
                }
            }
            return null;
        }
          
    }
}








namespace TBToListTest
{

   //实体
    public class Person
    {
        public int ID
        {
            set;
            get;
        }

        public string Name
        {
            set;
            get;
        }

        public string Age
        {
            set;
            get;
        }

        public string Lover
        {
            set;
            get;
        }

    }
}




using System;
using System.Data;

namespace TBToListTest
{
    class Program
    {
        static void Main(string[] args)
        {
            TBToList<Person> tol = new TBToList<Person>();
            Console.WriteLine();
            DataTable dt =  GetTable();
            tol.ToList(dt);
            Console.Read();
        }

        public static DataTable GetTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Age");
            dt.Columns.Add("Lover");
            dt.Columns.Add("Name");
            DataRow dr = dt.NewRow();
            dr["ID"] = 1;
            dr["Age"] = "Age1";
            dr["Lover"] = "Lover1";
            dr["Name"] = "Name1";
            dt.Rows.Add(dr);
            DataRow dr1 = dt.NewRow();
            dr1["ID"] = 2;
            dr1["Age"] = "Age2";
            dr1["Lover"] = "Lover2";
            dr1["Name"] = "Name2";
            dt.Rows.Add(dr1);
            return dt;
        }
    }
}



http://www.cnblogs.com/hlxs/archive/2011/05/09/2087976.html
小鱼的淘宝店铺-多多支持哇
视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

.net使用linq进行分页
昨天看了一会linq的书.今天哪来练练手.写了个简单的linq分页.在这里拿出来与大家共享一下.适合小数据型分页.因为他是先读取完全数据,然后在进行分页.
好了直接贴出代码
首页新建一个aspx页面
然后在后台aspx.cs文件中


代码 复制 - 运行

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int CurPage = 1;
                int PageSize = 5;
                int.TryParse(Request["CurPage"], out CurPage);
                if (CurPage == 0)
                    CurPage = 1;
                //获取总数据集合
                IList<ContentsModel> objs = new List<ContentsModel>();
                objs = Contents.GetAllContents();
                //绑定数据显示控件
                GridView1.DataSource = QueryByPage(PageSize, CurPage, objs);
                GridView1.DataBind();
                //获取分页目录
                this.Label1.Text = GetQueryPagesMenu(PageSize, objs);
            }
        }
        /// <summary>
        /// 输入分页字符页码
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="objs"></param>
        /// <returns></returns>
        protected string GetQueryPagesMenu(int PageSize, IList<ContentsModel> objs)
        {
            int Count;
            var db = objs;
            var query = from cms_contents in db select cms_contents;
            Count = (query.Count() / PageSize + 1);//不足一页按一页算
            string PageMenu = "";
            //拼接分页目录
            for (int i = 1; i <= Count; i++)
            {
                PageMenu += "<a href='?CurPage=" + i.ToString() + "'>" + i.ToString() + "</a> | ";
            }
            return PageMenu;
        }
        /// <summary>
        /// 获取分页后的数据集
        /// </summary>
        /// <param name="PageSize">每页显示的记录数</param>
        /// <param name="CurPage">页数</param>
        /// <param name="objs">数据总集合</param>
        /// <returns></returns>
        protected List<ContentsModel> QueryByPage(int PageSize, int CurPage, IList<ContentsModel> objs)
        {
            var query = from cms_contents in objs select cms_contents;
            return query.Take(PageSize * CurPage).Skip(PageSize * (CurPage - 1)).ToList();

小鱼的淘宝店铺-多多支持哇

适合url传输的DES加解密

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)
http://blog.csdn.net/rjzou2006/article/details/1596752


您是游客您没有权限查看该图片





代码 复制 - 运行

//http://blog.csdn.net/rjzou2006/article/details/1596752
        /// <summary>
        /// If don't input key , Use default key
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Encode(string str)
        {
            return Encode(str, "Rainight");
        }
        /// <summary>
        /// Des 加密 GB2312 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encode(string str, string key)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (byte num in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            stream.Close();
            return builder.ToString();
        } 
///Des 解密 GB2312 :
        /// <summary>
        /// if don't input key , Use default key
        /// </summary>
        /// <param name="str">Desc string </param>
        /// <returns></returns>
        public static string Decode(string str)
        {
            return Decode(str, "Rainight");
        }
        /// <summary>
        /// Des 解密 GB2312 
        /// </summary>
        /// <param name="str">Desc string</param>
        /// <param name="key">Key ,必须为8位 </param>
        /// <returns></returns>
        public static string Decode(string str, string key)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            byte[] buffer = new byte[str.Length / 2];
            for (int i = 0; i < (str.Length / 2); i++)
            {
                int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
                buffer[i] = (byte) num2;
            }
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            stream.Close();
            return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
        }



           string ecode = Encode("我是个中国人", "abcdefgh");
           this.label1.Text = ecode;
           this.label2.Text = Decode(ecode, "abcdefgh");



代码 复制 - 运行

    using System.Security.Cryptography;
    using System.Text;
    using System.IO;



加密之后url如:
http://127.0.0.1/erp/Realty/HouseOwner/PropertyForm.aspx?paras=B7F4A2EBDBDD924E129B87408B954F82&ran=1178325737843
paras是我们加密的参数。
ran 为一个随机 数,确保每次都不读缓存的
ran要以等于这个
   function RefreshNumber() {
       var d = new Date();
       return d.valueOf();
   }




另附:重出江湖|C#.net 加密大全


http://blog.csdn.net/chenandczh/article/details/7385244
本帖最后由 张小鱼 于 2015-02-09 15:49 编辑
小鱼的淘宝店铺-多多支持哇