zoukankan      html  css  js  c++  java
  • DES数据解密

            /// <summary>
            /// DES数据解密
            /// </summary>
            /// <param name="targetValue"></param>
            /// <param name="key"></param>
            /// <returns></returns>
            public static string Decrypt(string targetValue, string key = "test")
            {
                if (string.IsNullOrEmpty(targetValue))
                {
                    return string.Empty;
                }
                // 定义DES加密对象
                try
                {
                    var des = new DESCryptoServiceProvider();
                    int len = targetValue.Length / 2;
                    var inputByteArray = new byte[len];
                    int x, i;
                    for (x = 0; x < len; x++)
                    {
                        i = Convert.ToInt32(targetValue.Substring(x * 2, 2), 16);
                        inputByteArray[x] = (byte)i;
                    }
                    // 通过两次哈希密码设置对称算法的初始化向量   
                    des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
                                                          (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").
                                                               Substring(0, 8), "sha1").Substring(0, 8));
                    // 通过两次哈希密码设置算法的机密密钥   
                    des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
                                                         (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5")
                                                              .Substring(0, 8), "md5").Substring(0, 8));
                    // 定义内存流
                    var ms = new MemoryStream();
                    // 定义加密流
                    var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    return Encoding.Default.GetString(ms.ToArray());
                }
                catch
                {
                }
                return string.Empty;
            }
    

      

  • 相关阅读:
    虚拟机centOs和主机ssh互连
    centos7 利用yum安装mysql8.0
    MySQL触发器trigger的使用
    VMware虚拟机优化
    VM tools的安装
    CentOS下tar包和rpm包的区别
    如何解决VMware-vmx.exe无法彻底删除的问题
    JVM结构及堆的划分
    MYSQL: set names utf8是什么意思?
    collate utf8_bin和uft-8_general_ci是什么意思?
  • 原文地址:https://www.cnblogs.com/wohexiaocai/p/7050002.html
Copyright © 2011-2022 走看看