zoukankan      html  css  js  c++  java
  • C# DESC加密

    DESC加密方法

    直接上代码:

    1、加密

    /// <summary>
            /// 加密
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static string EncryptParameter(object obj)
            {
                if (obj == null)
                {
                    return "";
                }
                StringBuilder builder = new StringBuilder();
                try
                {
                    string str = obj.ToString();
                    DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
    
                    provider.Key = Encoding.UTF8.GetBytes(_securityKey);
    
                    provider.IV = Encoding.UTF8.GetBytes(_securityKey);
    
                    byte[] bytes = Encoding.UTF8.GetBytes(str);
    
                    MemoryStream stream = new MemoryStream();
    
                    CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
    
                    stream2.Write(bytes, 0, bytes.Length);
    
                    stream2.FlushFinalBlock();
    
                    foreach (byte num in stream.ToArray())
                    {
    
                        builder.AppendFormat("{0:X2}", num);
                    }
    
                    stream.Close();
    
                }
                catch (Exception ex)
                {
                    LogHelper.LogError(ex);
                }
                return builder.ToString();
    
            }
    View Code

    2、解密

            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string DecryptParameter(string str)
            {
                try
                {
                    DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
    
                    provider.Key = Encoding.ASCII.GetBytes(_securityKey);
    
                    provider.IV = Encoding.ASCII.GetBytes(_securityKey);
    
                    byte[] buffer = new byte[str.Length / 2];
    
                    for (int i = 0; i < (str.Length / 2); i++)
                    {
    
                        int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
    
                        buffer[i] = (byte)num2;
    
                    }
    
                    MemoryStream stream = new MemoryStream();
    
                    CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
    
                    stream2.Write(buffer, 0, buffer.Length);
    
                    stream2.FlushFinalBlock();
    
                    stream.Close();
    
                    return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
                }
                catch
                {
                    return string.Empty;
                }
            }
    View Code

    设置一个_securityKey字符串,加密、解密的密钥要同一个才行。

    因此正如其它人加密后的文件,用常用方法是打不开的,密钥不一样~

  • 相关阅读:
    【代码笔记】iOS-判断是否是模拟机
    【代码笔记】iOS-拍照动画
    【代码笔记】iOS-轮询弹出框
    【代码笔记】iOS-iCarouselDemo
    【代码笔记】iOS-UIScrollerView里有两个tableView
    【代码笔记】iOS-两个时间字符串的比较
    【代码笔记】iOS-利用图片序列创建动态图片效果
    【代码笔记】iOS-离线地图
    【代码笔记】iOS-看图听声音
    【代码笔记】iOS-看图听故事
  • 原文地址:https://www.cnblogs.com/kybs0/p/5816195.html
Copyright © 2011-2022 走看看