前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ASP.MVC当URL跳转时候参数的安全性

ASP.MVC当URL跳转时候参数的安全性

作者头像
用户1055830
发布2018-01-18 15:55:12
1.3K0
发布2018-01-18 15:55:12
举报
文章被收录于专栏:飞扬的花生飞扬的花生

      一个页面跳转到另外一个页面直接将参数写在URL上面并不安全比如 http://XXXXXXXXXXX/meeting/shakeGroup?id=5381&uid=o0En_sj1J0bFgIBMPG37WjWMXpqY

参数id和uid需要进行加密,写个简单的例子来实现:

加密类:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace CnbLogsProject.Util
{
    public class EnCodeHelper
    {
        // url传输参数加密密钥
        public static string strKeys = "abdfajrtrgjfg";

        #region 加密字符串
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string GetEncryption(string strValue)
        {
            //加密标准算法的对象
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            //建立加密对象的密钥和偏移量
            provider.Key = Encoding.ASCII.GetBytes(strKeys.Substring(0, 8));
            //原文使用Encoding.ASCII方法的GetBytes方法
            provider.IV = Encoding.ASCII.GetBytes(strKeys.Substring(0, 8));
            //将要加密的字符放到byte数组中
            byte[] bytes = Encoding.UTF8.GetBytes(strValue);
            //输入的文本必须是英文文本
            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();
        }
        #endregion

        #region 解密字符串
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string GetDecryption(string strValue)
        {
            //解密标准算法的对象
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            //建立解密密对象的密钥和偏移量
            provider.Key = Encoding.ASCII.GetBytes(strKeys.Substring(0, 8));
            //原文使用Encoding.ASCII方法的GetBytes方法
            provider.IV = Encoding.ASCII.GetBytes(strKeys.Substring(0, 8));
            //将要解密的字符放到byte数组中
            byte[] buffer = new byte[strValue.Length / 2];
            //循环遍历遍历
            for (int i = 0; i < (strValue.Length / 2); i++)
            {
                int num2 = Convert.ToInt32(strValue.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());
        }
        #endregion
    }
}
代码语言:javascript
复制
strKeys 为秘钥可以写在配置文件里面控制器(将A页面的参数加密后暴露给客户端跳转到B页面时候解密):
代码语言:javascript
复制
     /// <summary>
        /// A页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            string id = "5381";
            string uid = "o0En_sj1J0bFgIBMPG37WjWMXpqY";
            id = EnCodeHelper.GetEncryption(id);
            uid = EnCodeHelper.GetEncryption(uid);

            ViewBag.id = id;
            ViewBag.uid = uid;
            return View();
        }


        /// <summary>
        /// B页面
        /// </summary>
        /// <param name="id"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public ActionResult Home(string id="",string uid="")
        {
            ViewBag.id =EnCodeHelper.GetDecryption(id);
            ViewBag.uid =EnCodeHelper.GetDecryption(uid);
            return View();
        }

视图:

代码语言:javascript
复制
@{
    ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<button id="re">跳转</button>
<script>

    $(function () {

        $("#re").click(function () {
            location.href = "Home?id="+"@ViewBag.id"+"&uid="+"@ViewBag.uid";
        });
    });
</script>
代码语言:javascript
复制
@{
    ViewBag.Title = "Home";
}

<input value="@ViewBag.id" />
<input value="@ViewBag.uid"/>
<h2>Home</h2>

效果:

原来的URL:http://localhost:63792/Home/Home?id=282D147B1B12BAE3&uid=29732D957DD4EF753BC3E94797D1018D230457174ABD43EF1ED2FEA651E8351E

跳转到B页面后成功解密:

对应上我们开头的

http://XXXXXXXXXXX/meeting/shakeGroup?id=5381&uid=o0En_sj1J0bFgIBMPG37WjWMXpqY

参数id和uid需要进行加密,写个简单的例子来实现:

当然还有其他很多方法 

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-09-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档