zoukankan      html  css  js  c++  java
  • DESC 加密,解密

    一、加密解密方法
    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
        {
            /// <summary>
            /// 对地址加密
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public static string UrlEncode(string url)
            {
                string[] str = url.Split('?');
                return str[0] + "?" + Encode(str[1]) + ".shtml";
            }
    
            /// <summary>
            /// 对地址解密
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            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";
            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            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();
            }
    
            /// <summary>
            /// Des 解密 GB2312 
            /// </summary>
            /// <param name="str">Desc string</param>
            /// <returns></returns>
            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();
            }
    
    
    
     
  • 相关阅读:
    python脚本 – 删除指定天数前的文件
    java 获取屏幕的分辩率
    解决Multi input/output stream coders are not yet supported(org.apache.commons.compress)
    解决tomcat at org.apache.tomcat.util.buf.CharChunk.append(CharChunk.java:355)
    org.quartz.jobStore.misfireThreshold = 60000
    python list 自定义排序
    利用pycron在windows上实现cron定时任务
    [Selenium+Java] Scroll UP or Down a page in Selenium Webdriver
    Python获取硬件信息(硬盘序列号,CPU序列号)
    ChromeDriver自动更新、FirefoxDriver自动更新、InternetExplorerDriver自动更新(Java+Python)
  • 原文地址:https://www.cnblogs.com/Jacob-Wu/p/5978109.html
Copyright © 2011-2022 走看看