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

    region 加密数据
    
            /// <summary> 
            /// 加密数据 
            /// 如秘钥为空 则返回原文
            /// </summary> 
            /// <param name="content">加密前文本</param> 
            /// <param name="desKey">密匙</param> 
            /// <returns></returns> 
            public static string EncryptStr(string content, string desKey)
            {
                try
                {
                    if (string.IsNullOrEmpty(desKey))
                    {
                        return content;
                    }
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    des.Mode = CipherMode.ECB;
                    var keys = new byte[8];
                    if (desKey.Length < 8)
                    {
                        var tmpkeys = System.Text.Encoding.UTF8.GetBytes(desKey);
                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (tmpkeys.Length > i)
                                keys[i] = tmpkeys[i];
                            else
                                keys[i] = 0;
                        }
                    }
                    else
                        keys = System.Text.Encoding.UTF8.GetBytes(desKey.Substring(0, 8));
    
                    des.Key = keys;
                    des.IV = des.Key;
                    byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(content);
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    StringBuilder sb = new StringBuilder();
                    foreach (var item in ms.ToArray())
                    {
                        sb.Append(item.ToString());
                        sb.Append("_");
                    }
                    if (sb.Length > 0)
                    {
                        sb = sb.Remove(sb.Length - 1, 1);
                    }
                    return sb.ToString();
                }
                catch (System.Exception e)
                {
                    return content;
                }
            }
            #endregion
    des加密算法
    #region 解密数据
            /// <summary> 
            /// 解密数据 
            /// 如秘钥为空 则返回原文
            /// </summary> 
            /// <param name="content">密文</param> 
            /// <param name="desKey">密匙</param> 
            /// <returns>解密失败返回空字符串</returns> 
            public static string DecryptStr(string content, string desKey)
            {
                try
                {
                    if (string.IsNullOrEmpty(desKey))
                    {
                        return content;
                    }
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    des.Mode = CipherMode.ECB;
                    var keys = new byte[8];
                    if (desKey.Length < 8)
                    {
                        var tmpkeys = System.Text.Encoding.UTF8.GetBytes(desKey);
                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (tmpkeys.Length > i)
                                keys[i] = tmpkeys[i];
                            else
                                keys[i] = 0;
                        }
                    }
                    else
                        keys = System.Text.Encoding.UTF8.GetBytes(desKey.Substring(0, 8));
    
                    des.Key = keys;
                    des.IV = des.Key;
                    var data = content.Split('_');
                    byte[] inputByteArray = new byte[data.Length]; ;//Convert.FromBase64String(encryptText);
                    for (int i = 0; i < data.Length; i++)
                    {
                        inputByteArray[i] = Convert.ToByte(data[i]);
                    }
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    System.Text.Encoding encoding = new System.Text.UTF8Encoding();
                    return encoding.GetString(ms.ToArray());
                }
                catch (System.Exception e)
                {
                    return "";
                }
    
            }
            #endregion
    des解密算法
    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    软件测试学习总结
    MySQL数据库中主键和索引的区别和联系
    什么是接口测试及其测试流程
    bug生命周期
    啊这...2-get/post请求区别,来给你看看post请求url中传参
    啊这...1-get/post请求区别,你还在认为get只能在url传参吗?传json格式会咋样?
    关于博客园全站内容审核中...如出现此问题:请移步xxx
    git-2-企业级gitlab的使用及管理工具Sourcetree
    fiddler-12-Proxifier+fiddler进行PC端抓包
    微信小程序弹出订阅消息确认弹窗的限制
  • 原文地址:https://www.cnblogs.com/YYkun/p/15079698.html
Copyright © 2011-2022 走看看