zoukankan      html  css  js  c++  java
  • ASP.NET中的DEC加密解密过程

    本文章分享自 青青果树园的博客,地址是:http://www.cnblogs.com/qqingmu/archive/2008/01/10/1034168.html

    我们做网页时经常会遇到URL传输(表单提交)参数加密。
    例如:要进行一个用户帐号编辑,要传递用户的ID,URL如下:http://localhost/mysystem/editAccounts.aspx?ID=2
    但又不想让别人知道这个用户的ID为2,恶意的使用者可能还会将2修改,改为别的用户ID。加密传递的参数值可以解决问题。
    1、以下是DEC加密、解密的函数。

    加密过程:

     1  /**//// <summary>
     2         /// DEC 加密过程
     3         /// </summary>
     4         /// <param name="pToEncrypt">被加密的字符串</param>
     5         /// <param name="sKey">密钥(只支持8个字节的密钥)</param>
     6         /// <returns>加密后的字符串</returns>
     7         public string Encrypt(string pToEncrypt, string sKey)
     8         {
     9             //访问数据加密标准(DES)算法的加密服务提供程序 (CSP) 版本的包装对象
    10             DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
    11             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
    12             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
    13 
    14             byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);//把字符串放到byte数组中
    15 
    16             MemoryStream ms = new MemoryStream();//创建其支持存储区为内存的流 
    17             //定义将数据流链接到加密转换的流
    18             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    19             cs.Write(inputByteArray, 0, inputByteArray.Length);
    20             cs.FlushFinalBlock();
    21             //上面已经完成了把加密后的结果放到内存中去
    22 
    23             StringBuilder ret = new StringBuilder();
    24             foreach (byte b in ms.ToArray())
    25             {
    26                 ret.AppendFormat("{0:X2}", b);
    27             }
    28             ret.ToString();
    29             return ret.ToString();
    30         }

    解密过程:

     1  /**//// <summary>
     2         /// DEC 解密过程
     3        /// </summary>
     4         /// <param name="pToDecrypt">被解密的字符串</param>
     5         /// <param name="sKey">密钥(只支持8个字节的密钥,同前面的加密密钥相同)</param>
     6        /// <returns>返回被解密的字符串</returns>
     7         public string Decrypt(string pToDecrypt, string sKey)
     8         {
     9             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    10 
    11             byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
    12             for (int x = 0; x < pToDecrypt.Length / 2; x++)
    13             {
    14                 int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
    15                 inputByteArray[x] = (byte)i;
    16             }
    17 
    18             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量,此值重要,不能修改
    19             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    20             MemoryStream ms = new MemoryStream();
    21             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
    22 
    23             cs.Write(inputByteArray, 0, inputByteArray.Length);
    24             cs.FlushFinalBlock();
    25 
    26             //建立StringBuild对象,createDecrypt使用的是流对象,必须把解密后的文本变成流对象
    27             StringBuilder ret = new StringBuilder(); 
    28 
    29             return System.Text.Encoding.Default.GetString(ms.ToArray());
    30         }

    2、 具体在程序中使用加密解密算法的例子如下:

    1 在发送页面
    2 Response.Redirect("~/GridView.aspx?ID=" + Encrypt("zlh","12345678"));
    3 
    4 在接受页面
    5 string acceptStr;
    6 acceptStr = Decrypt(Request.QueryString["ID"],"12345678");
  • 相关阅读:
    redis conf 中文详解
    sed 用法记录
    MySQL数据库的各种存储引擎详解
    MySQL数据库char与varchar的区别分析及使用建议
    从一个乘法来分析C语言
    排它平方数
    高斯日记
    SUID或SGID程序中能不能用system函数
    【转载】GDB反向调试(Reverse Debugging)
    setuid函数解析
  • 原文地址:https://www.cnblogs.com/ElvisZhongShao/p/3952851.html
Copyright © 2011-2022 走看看