• DES对称加密


    /// <summary>
        /// DES对称加密
        /// </summary>
        public static class DESHelper
        {
            /// <summary>
            /// 根据用户名解密
            /// </summary>
            /// <param name="val"></param>
            /// <param name="userid"></param>
            /// <returns></returns>
            public static string Decrypt(string val, string userid = "")
            {
    
                var key = GetKey(userid);
                var iv = GetDefaultIV();
                return Decrypt(val, key, iv);
            }
    
            /// <summary>
            /// 根据用户名加密
            /// </summary>
            /// <param name="val"></param>
            /// <param name="userid"></param>
            /// <returns></returns>
            public static string Encrypt(string val, string userid = "")
            {
    
                var key = GetKey(userid);
                var iv = GetDefaultIV();
                return Encrypt(val, key, iv);
            }
    
            /// <summary>
            /// Des加密方法
            /// </summary>
            /// <param name="val"></param>
            /// <param name="key"></param>
            /// <param name="IV"></param>
            /// <returns></returns>
            public static string Encrypt(string val, string key, string IV, CipherMode cipherMode = CipherMode.ECB)
            {
                try
                {
                    if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(key))
                    {
                        throw new Exception("密钥和偏移向量不足8位");
                    }
    
                    if (key.Length > 8) key = key.Substring(0, 8);
                    if (IV.Length > 8) IV = IV.Substring(0, 8);
    
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    byte[] btKey = Encoding.Default.GetBytes(key);
                    byte[] btIV = Encoding.Default.GetBytes(IV);
                    StringBuilder builder = new StringBuilder();
                    using (MemoryStream ms = new MemoryStream())
                    {
                        byte[] inData = Encoding.Default.GetBytes(val);
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);
                            cs.FlushFinalBlock();
                        }
    
                        foreach (byte num in ms.ToArray())
                        {
                            builder.AppendFormat("{0:X2}", num);
                        }
                    } 
                    return builder.ToString();
                }
                catch // (Exception ex)
                {
                    return "";
                }
            }
    
            /// <summary>
            /// Des解密方法
            /// cbc模式:(key值和iv值一致)
            /// </summary>
            /// <param name="val"></param>
            /// <param name="key"></param>
            /// <param name="IV"></param>
            /// <returns></returns>
            public static string Decrypt(string val, string key, string IV, CipherMode cipherMode = CipherMode.ECB)
            {
                try
                {
                    if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(key))
                    {
                        throw new Exception("密钥和偏移向量不足8位");
                    }
    
                    if (key.Length > 8) key = key.Substring(0, 8);
                    if (IV.Length > 8) IV = IV.Substring(0, 8);
    
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    byte[] btKey = Encoding.Default.GetBytes(key);
                    byte[] btIV = Encoding.Default.GetBytes(IV);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        byte[] inData = new byte[val.Length / 2];
                        for (int i = 0; i < (val.Length / 2); i++)
                        {
                            int num2 = Convert.ToInt32(val.Substring(i * 2, 2), 0x10);
                            inData[i] = (byte)num2;
                        }
    
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);
                            cs.FlushFinalBlock();
                        }
    
                        return Encoding.Default.GetString(ms.ToArray());
                    }
                }
                catch // (System.Exception ex)
                {
                    return "";
                }
            }
    
            /// <summary>
            /// Md5加密
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string MD5(string str)
            {
                if (string.IsNullOrEmpty(str)) return str;
                System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                string encoded = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", "");
                return encoded;
            }
    
            private static string GetKey(string key)
            {
                string defaultKey = "C560F02F693B4A0BA62D2B3B5FB74534";
                string sTemp = defaultKey;
                if (!string.IsNullOrEmpty(key))
                {
                    sTemp = string.Concat(key, defaultKey.Substring(key.Length, 32 - key.Length));
                }
                return MD5(sTemp).Substring(0, 8);
            }
            /// <summary>   
            /// 获得初始向量IV 
            /// </summary>   
            /// <returns>初试向量IV</returns>   
            private static string GetDefaultIV()
            {
                string sTemp = "87DE696F56DE49C0B96EB85139A48805";
                return MD5(sTemp).Substring(0, 8);
            }
        }
  • 相关阅读:
    201871010115 马北 《面向对象程序设计(java)》 第67周学习总结
    201871010115马北《面向对象程序设计(java)》第一周学习总结
    《面向对象程序设计(Java)》第四周学习总结
    201871010115——马北《面向对象程序设计JAVA》第二周学习总结
    第十一周作业
    《面向对象程序设计(java)》第十周学习总结
    201871010115 马北 第八周作业
    FPGA开发全攻略——FPGA发展以及赛灵思系列产品
    嵌入式系统词汇表
    FPGA开发全攻略——FPGA内部结构
  • 原文地址:https://www.cnblogs.com/zhengwei-cq/p/10316331.html
Copyright © 2020-2023  润新知