首页 > 代码库 > 制作URL以GET方式提交的简单加密程序

制作URL以GET方式提交的简单加密程序

首先我们用到的是

DESCryptoServiceProvider 类

对此微软给出的解释是

定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象。无法继承此类。

接下来是接受参数页面的方法:

 

using System;using System.Collections.Generic;using System.IO;using System.Security.Cryptography;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;namespace Url加密{    public partial class Content : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if(!IsPostBack)            {                GetDate();            }        }        Byte[] byKey64 = { 10, 20, 30, 40, 50, 60, 70, 80 };        Byte[] Iv64 = { 11, 22, 33, 44, 55, 66, 77, 85 };        public string Decrypt(string strText)        {            Byte[] inputByteArray = new byte[strText.Length];            try            {                DESCryptoServiceProvider des = new DESCryptoServiceProvider();                inputByteArray = Convert.FromBase64String(strText);                MemoryStream ms = new MemoryStream();                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey64, Iv64), CryptoStreamMode.Write);                cs.Write(inputByteArray, 0, inputByteArray.Length);                cs.FlushFinalBlock();                System.Text.Encoding encoding = System.Text.Encoding.UTF8;                return encoding.GetString(ms.ToArray());            }            catch (Exception ex)            {                return ex.Message;            }        }        public string content = string.Empty;        public void GetDate()        {            string sql = "SELECT * FROM [Info] WHERE [Id]=@id";            string ConStr = "Data Source=.;Initial Catalog=DBTest;User ID=sa;Password=123456";            SqlParameter para = new SqlParameter("@id", Decrypt(Request.QueryString["id"]));                       using (SqlConnection conn = new SqlConnection(ConStr))            {                conn.Open();                SqlCommand comm = new SqlCommand(sql.ToString(), conn);                comm.Parameters.Add(para);                SqlDataReader reader = comm.ExecuteReader();                while (reader.Read())                {                    content = reader["Details"].ToString();                }                reader.Close();            }        }    }}

 

我用的是repeater的绑定方式

 

然后是传入ID参数的页面后台代码如下:

 

using System;using System.Collections.Generic;using System.IO;using System.Security.Cryptography;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;namespace Url加密{    public partial class _Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if(!IsPostBack)            {                GetDate();            }        }        Byte[] Iv64 = { 11, 22, 33, 44, 55, 66, 77, 85 };        Byte[] byKey64 = { 10, 20, 30, 40, 50, 60, 70, 80 };        public string Encrypt(string strText)        {            try            {                DESCryptoServiceProvider des = new DESCryptoServiceProvider();                Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);                MemoryStream ms = new MemoryStream();                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey64, Iv64), CryptoStreamMode.Write);                cs.Write(inputByteArray, 0, inputByteArray.Length);                cs.FlushFinalBlock();                return Convert.ToBase64String(ms.ToArray());            }            catch (Exception ex)            {                return ex.Message;            }        }        public string id = string.Empty;                public void GetDate()        {            string sql = "SELECT * FROM [dbo].[Info]";            string ConStr="Data Source=.;Initial Catalog=DBTest;User ID=sa;Password=123456";            using(SqlConnection conn=new SqlConnection(ConStr))            {                conn.Open();                SqlCommand comm=new SqlCommand(sql.ToString(),conn);                DataSet dt = new DataSet();                SqlDataAdapter sdt = new SqlDataAdapter(sql.ToString(),conn);                sdt.Fill(dt);                this.Repeater1.DataSource = dt.Tables[0];                this.Repeater1.DataBind();            }        }        }}

他的前台参数绑定:

 

<asp:Repeater ID="Repeater1" runat="server">            <ItemTemplate>                <a href=http://www.mamicode.com/"Content.aspx?id=<%#Encrypt(Eval("Id").ToString())%>"><%#Eval("Title") %></a>            </ItemTemplate>        </asp:Repeater>

请注意,对于ID转换成为字符串这一步很重要。以上就是我的个人心得了。希望有大神能够继续指教完善,同时也参考了网上的代码资料。对此表示感谢