博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DESC 加密,解密
阅读量:4983 次
发布时间:2019-06-12

本文共 3477 字,大约阅读时间需要 11 分钟。

一、加密解密方法
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Security.Cryptography;using System.Text;using System.Web;namespace MvcApplication1.Helper{    public class CryptoSecurity    {        ///         /// 对地址加密        ///         ///         /// 
public static string UrlEncode(string url) { string[] str = url.Split('?'); return str[0] + "?" + Encode(str[1]) + ".shtml"; } /// /// 对地址解密 /// /// ///
public static string[] UrlDecode(string url) { string[] urls = new string[2]; try { string[] str = url.Split('?'); urls[0] = str[0]; urls[1] = Decode(str[1].Split('.')[0]); return urls; } catch { urls[0] = "/Home/Error"; urls[1] = ""; return urls; } } private static string key = DateTime.Now.ToString(yyMMdd) + "1jjqsd789"; /// /// 加密 /// /// ///
public static string Encode(string str) { 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 /// /// Desc string ///
public static string Decode(string str) { 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()); } }} 二、global.aspx文件
public void URLRewriting()        {            string AllUrl = Request.RawUrl.ToString();            if (AllUrl.Contains(".shtml"))            {                string[] urls = CryptoSecurity.UrlDecode(AllUrl);                Context.RewritePath(urls[0], String.Empty, urls[1]);            }        }        public void Application_BeginRequest(Object sender, EventArgs e)        {            URLRewriting();        }
 

 

 

 

转载于:https://www.cnblogs.com/Jacob-Wu/p/5978109.html

你可能感兴趣的文章
《SQL 基础教程》第四章:数据更新
查看>>
Nine-patch
查看>>
P1387 最大正方形
查看>>
百度地图 - demo
查看>>
python3之迭代器&生成器
查看>>
《此生未完成》读后感
查看>>
Nexus搭建Maven私服
查看>>
访问者模式
查看>>
CentOS 7安装最新版本Git
查看>>
DTW的原理及matlab实现
查看>>
jQuery EasyUI API 中文文档 - 对话框(Dialog)
查看>>
在Android8.0以上收不到广播问题(AppWidget)
查看>>
SCOI2010 传送带 [三分/模拟退火]
查看>>
C#读取文件,返回字符串形式的文件内容
查看>>
卸载软件时出现的“不能够打开文件INSTALL.LOG”错误-清理注册表即可
查看>>
R学习笔记(3):绘图
查看>>
类的封装
查看>>
命名空间的定义
查看>>
Android 中Json解析的几种框架(Gson、Jackson、FastJson、LoganSquare)使用与对比
查看>>
byte[]与各种数据类型互相转换示例
查看>>