zoukankan      html  css  js  c++  java
  • DES加密解密

    自己写的DES加密解密类,加密后生成Base64字符串,并去除'='字符。

    加密后替换掉'+',这样加密后的字符串可以作为url参数传递。

    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace QuaEdu.Helper
    {
        /// <summary>
        /// DES加密/解密类。
        /// 2014年3月15日
        /// </summary>
        public class DESHelper
        {
            #region 默认密钥
            /// <summary>
            /// 默认密钥
            /// </summary>
            private static readonly string m_key = "simpo_s0611163";
            #endregion
    
            #region ========加密========
            /// <summary> 
            /// 加密数据 
            /// </summary> 
            /// <param name="Text">原文</param> 
            /// <returns>密文</returns> 
            public static string Encrypt(string Text)
            {
                return Encrypt(Text, m_key);
            }
    
            /// <summary> 
            /// 加密数据 
            /// </summary> 
            /// <param name="Text">原文</param> 
            /// <param name="sKey">密钥</param> 
            /// <returns>密文</returns> 
            public static string Encrypt(string Text, string sKey)
            {
                DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();
    
                byte[] inputByteArray = Encoding.Default.GetBytes(Text);
                byte[] keyByteArray = Encoding.Default.GetBytes(sKey);
    
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                md5.ComputeHash(keyByteArray);
    
                desKey.Key = HalveByteArray(md5.Hash);
                desKey.IV = HalveByteArray(md5.Hash);
    
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, desKey.CreateEncryptor(), CryptoStreamMode.Write);
    
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
    
                return Convert.ToBase64String(ms.ToArray()).Replace("=", "").Replace("+", "-").Replace("/", "*");
            }
            #endregion
    
            #region ========解密========
            /// <summary> 
            /// 解密数据 
            /// </summary> 
            /// <param name="Text">密文</param> 
            /// <returns>原文</returns> 
            public static string Decrypt(string Text)
            {
                return Decrypt(Text, m_key);
            }
    
            /// <summary> 
            /// 解密数据 
            /// </summary> 
            /// <param name="Text">密文</param> 
            /// <param name="sKey">密钥</param> 
            /// <returns>原文</returns> 
            public static string Decrypt(string Text, string sKey)
            {
                StringBuilder sb = new StringBuilder();
                if (Text.Length % 4 != 0)
                {
                    for (int i = 0; i < 4 - Text.Length % 4; i++)
                    {
                        sb.Append("=");
                    }
                }
                Text = Text.Replace("-", "+").Replace("*", "/") + sb.ToString();
    
                DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();
    
                byte[] inputByteArray = Convert.FromBase64String(Text);
                byte[] keyByteArray = Encoding.Default.GetBytes(sKey);
    
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                md5.ComputeHash(keyByteArray);
    
                desKey.Key = HalveByteArray(md5.Hash);
                desKey.IV = HalveByteArray(md5.Hash);
    
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, desKey.CreateDecryptor(), CryptoStreamMode.Write);
    
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
    
                return Encoding.Default.GetString(ms.ToArray());
            }
            #endregion
    
            #region 把字节数组缩短一半
            /// <summary>
            /// 把字节数组缩短一半
            /// </summary>
            private static byte[] HalveByteArray(byte[] data)
            {
                byte[] result = new byte[data.Length / 2];
    
                for (int i = 0; i < result.Length; i++)
                {
                    int x = (data[i * 2] + data[i * 2 + 1]) / 2;
                    result[i] = (byte)x;
                }
    
                return result;
            }
            #endregion
    
        }
    }
    View Code
  • 相关阅读:
    easy Html5 Jquery Mobile之ToolBars(Header and Footer)
    Windows Phone7 性能
    easy Html5 Jquery Mobile之Buttons
    开篇,从这里开始。
    SkyDrive APIs——用户登录和APP授权(2)
    Windows Phone的强大语音控制,让你的Phone大秀一把
    SkyDrive APIs之——操作文件和文件夹(3)
    Mango in my mind 之 Live Tile
    SkyDrive APIs——搭建环境(1)
    windows10安装redis
  • 原文地址:https://www.cnblogs.com/s0611163/p/3601447.html
Copyright © 2011-2022 走看看