• 用C#实现DES加密解密解决URL参数明文的问题


    啥也不说,直接上代码。
    加密解码,封装到一个类,key可以自己修改。




    using System; 
    using System.Security.Cryptography; 
    using System.Text; 
    using System.IO; 
     
    namespace Common 
    ...{ 
            /**//// <summary> 
            /// DESEncrypt加密解密算法。 
            /// </summary> 
            public sealed class DESEncrypt 
            ...{ 
                    private DESEncrypt() 
                    ...{ 
                            // 
                            // TODO: 在此处添加构造函数逻辑 
                            // 
                    } 
     
                    private static string key = "joke168"; 
     
                    /**//// <summary> 
                    /// 对称加密解密的密钥 
                    /// </summary> 
                    public static string Key 
                    ...{ 
                            get 
                            ...{ 
                                    return key; 
                            } 
                            set 
                            ...{ 
                                    key = value; 
                            } 
                    } 
     
                    /**//// <summary> 
                    /// DES加密 
                    /// </summary> 
                    /// <param name="encryptString"></param> 
                    /// <returns></returns> 
                    public static string DesEncrypt(string encryptString) 
                    ...{ 
                            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); 
                            byte[] keyIV = keyBytes; 
                            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 
                            DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); 
                            MemoryStream mStream = new MemoryStream(); 
                            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write); 
                            cStream.Write(inputByteArray, 0, inputByteArray.Length); 
                            cStream.FlushFinalBlock(); 
                            return Convert.ToBase64String(mStream.ToArray()); 
                    } 
     
                    /**//// <summary> 
                    /// DES解密 
                    /// </summary> 
                    /// <param name="decryptString"></param> 
                    /// <returns></returns> 
                    public static string DesDecrypt(string decryptString) 
                    ...{ 
                            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); 
                            byte[] keyIV = keyBytes; 
                            byte[] inputByteArray = Convert.FromBase64String(decryptString); 
                            DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); 
                            MemoryStream mStream = new MemoryStream(); 
                            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write); 
                            cStream.Write(inputByteArray, 0, inputByteArray.Length); 
                            cStream.FlushFinalBlock(); 
                            return Encoding.UTF8.GetString(mStream.ToArray()); 
                    } 
            } 
    } 
    

     欢迎批评指正。  

  • 相关阅读:
    JPA01
    mybatis入门
    PHP 循环- While循环
    PHP超级全局变量
    PHP 数组排序
    PHP数组
    PHP Switch语句
    PHP IF...Else语句
    PHP运算符
    PHP字符串变量
  • 原文地址:https://www.cnblogs.com/joke168/p/3546263.html
Copyright © 2020-2023  润新知