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

    官方文档

    https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.hashalgorithm?redirectedfrom=MSDN&view=netcore-3.0

    加密--HashPasswordForStoringInConfigFile过时问题

    http://www.bubuko.com/infodetail-1112769.html

    public static string Encrypt(string Text, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray;
                inputByteArray = Encoding.Default.GetBytes(Text);
                des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(sKey.Substring(0, 8));
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
     
            public static string MD5(string str)
            {
                //微软md5方法参考return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
                byte[] b = Encoding.Default.GetBytes(str);
                b = new MD5CryptoServiceProvider().ComputeHash(b);
                string ret = "";
                for (int i = 0; i < b.Length; i++)
                    ret += b[i].ToString("X").PadLeft(2, '0');
                return ret;
            }
    /// <summary> 
            /// 解密数据 
            /// </summary> 
            /// <param name="Text"></param> 
            /// <param name="sKey"></param> 
            /// <returns></returns> 
            public static string Decrypt(string Text, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                int len;
                len = Text.Length / 2;
                byte[] inputByteArray = new byte[len];
                int x, i;
                for (x = 0; x < len; x++)
                {
                    i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }
    
                des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8)); 
                des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8)); 
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Encoding.Default.GetString(ms.ToArray());
            }
    #region 加密字符串
            /// <summary> /// 加密字符串   
            /// </summary>  
            /// <param name="str">要加密的字符串</param>  
            /// <param name="key">秘钥</param>  
            /// <returns>加密后的字符串</returns>  
            //public static string Encrypt(string str, string myKey)
            //{
            //    string encryptKeyall = Convert.ToString(myKey);    //定义密钥  
            //    if (encryptKeyall.Length < 9)
            //    {
            //        for (; ; )
            //        {
            //            if (encryptKeyall.Length < 9)
            //                encryptKeyall += encryptKeyall;
            //            else
            //                break;
            //        }
            //    }
            //    string encryptKey = encryptKeyall.Substring(0, 8);
            //    DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();   //实例化加/解密类对象   
    
            //    byte[] key = Encoding.UTF8.GetBytes(encryptKey); //定义字节数组,用来存储密钥    
            //    byte[] data = Encoding.UTF8.GetBytes(str);//定义字节数组,用来存储要加密的字符串  
    
            //    MemoryStream MStream = new MemoryStream(); //实例化内存流对象      
            //    //使用内存流实例化加密流对象   
            //    CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
            //    CStream.Write(data, 0, data.Length);  //向加密流中写入数据      
            //    CStream.FlushFinalBlock();              //释放加密流      
    
            //    return Convert.ToBase64String(MStream.ToArray());//返回加密后的字符串  
            //}

            /// <summary>  
            /// 解密字符串   
            /// </summary>  
            /// <param name="str">要解密的字符串</param>  
            ///  <param name="myKey">秘钥</param>  
            /// <returns>解密后的字符串</returns>  
            public static string Decrypt(string str, string myKey)
            {
                string encryptKeyall = Convert.ToString(myKey);    //定义密钥  
                if (encryptKeyall.Length < 9)
                {
                    for (; ; )
                    {
                        if (encryptKeyall.Length < 9)
                            encryptKeyall += encryptKeyall;
                        else
                            break;
                    }
                }
                string encryptKey = encryptKeyall.Substring(0, 8);
                DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();   //实例化加/解密类对象    
                byte[] key = Encoding.UTF8.GetBytes(encryptKey); //定义字节数组,用来存储密钥    
                byte[] data = Convert.FromBase64String(str);//定义字节数组,用来存储要解密的字符串  
    
                MemoryStream MStream = new MemoryStream(); //实例化内存流对象      
                //使用内存流实例化解密流对象       
                CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
                CStream.Write(data, 0, data.Length);      //向解密流中写入数据     
                CStream.FlushFinalBlock();               //释放解密流      
    
                return Encoding.UTF8.GetString(MStream.ToArray());       //返回解密后的字符串  
            }
  • 相关阅读:
    49. 字母异位词分组
    73. 矩阵置零
    Razor语法问题(foreach里面嵌套if)
    多线程问题
    Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
    How to execute tons of tasks parallelly with TPL method?
    How to sort the dictionary by the value field
    How to customize the console applicaton
    What is the difference for delete/truncate/drop
    How to call C/C++ sytle function from C# solution?
  • 原文地址:https://www.cnblogs.com/hwubin5/p/10808680.html
Copyright © 2011-2022 走看看