• 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解密算法
    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    Fiddler 捕获HTTPS流量
    Fiddler 弱网测试
    Fiddler Composer设计器, Fitter过滤器,断点
    Fiddler Statistics统计,Inspector检查器,AutoResponder自动响应器
    Fiddler Session List会话列表、命令行与状态栏
    Fiddler菜单栏工具栏
    HTTP协议请求与响应详解
    Postman 运用Cookie
    Postman 文件上传请求
    Postman 头域操作
  • 原文地址:https://www.cnblogs.com/YYkun/p/15079698.html
Copyright © 2020-2023  润新知