加密解密的一些方法

#region ----------------加密解密-------------




    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="pToEncrypt">被加密的字符串</param>
    /// <param name="sKey">加密的字符串</param>
    /// <returns></returns>
    public static string Decrypt(string pToEncrypt, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();


        byte[] inputByteArray = new byte[pToEncrypt.Length / 2];
        for (int x = 0; x < pToEncrypt.Length / 2; x++)
        {
            int i = (Convert.ToInt32(pToEncrypt.Substring(x * 2, 2), 16));
            inputByteArray[x] = (byte)i;
        }


        //建立加密对象的密钥和偏移量,此值重要,不能修改 
        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        MemoryStream ms = new MemoryStream();


        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
         cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();


        //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 
        StringBuilder ret = new StringBuilder();


        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }






    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="pToEncrypt">被加密的字符串</param>
    /// <param name="sKey">加密的字符串</param>
    /// <returns></returns>
    public static string Encrypt(string pToEncrypt, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        //把字符串放到byte数组中 
        //原来使用的UTF8编码,我改成Unicode编码了,不行 
        byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
        //byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); 


        //建立加密对象的密钥和偏移量 
        //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 
        //使得输入密码必须输入英文文本 
        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        //Write the byte array into the crypto stream 
        //(It will end up in the memory stream) 
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        //Get the data back from the memory stream, and into a string 
        StringBuilder ret = new StringBuilder();
        foreach (byte b in ms.ToArray())
        {
            //Format as hex 
            ret.AppendFormat("{0:X2}", b);
        }
        ret.ToString();
        return ret.ToString();
    }






    /// <summary>
    /// MD5加密,但是加密有问题,加密出来不是32位,根据传入的字符的长度数
    /// </summary>
    /// <param name="strMD5">需要加密的字符串</param>
    /// <returns>加密后的字符串</returns>
    public static string EncryptByMD5(string strMD5)
    {
        string result = "";
        MD5 m = MD5.Create();
        byte[] s = m.ComputeHash(Encoding.UTF8.GetBytes(strMD5));
        for (int i = 0; i < s.Length; i++)
        {
            result += s[i].ToString("X");
        }
        return result;
    }






    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="pToEncrypt">需要加密的字符串</param>
    /// <param name="sKey">密钥,必须为8位</param>
    /// <returns>加密后的字符串</returns>
    public static string EncryptDes(string pToEncrypt, string sKey)
    {
        string str = "";
        if (sKey.Length < 8 || sKey.Length > 8)
        {
            return str;
        }
        else
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return str;
            }
        }
    }
    /// <summary>
    /// 进行DES解密。
    /// </summary>
    /// <param name="pToDecrypt">要解密的以Base64</param>
    /// <param name="sKey">密钥,且必须为8位。</param>
    /// <returns>已解密的字符串。</returns>
    public static string DecryptDes(string pToDecrypt, string sKey)
    {
        string str = "";
        if (sKey.Length < 8 || sKey.Length > 8)
        {
            return str;
        }
        else
        {
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        }
    }


    /// <summary>
    /// MD5加密,加密出来都是32位
    /// </summary>
    /// <param name="strMD5">要加密的字符串,返回小写的</param>
    /// <returns></returns>
    public static string EncryptByMD5(string strMD5)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.Default.GetBytes(strMD5);
        byte[] result = md5.ComputeHash(data);
        String ret = "";
        for (int i = 0; i < result.Length; i++)
            ret += result[i].ToString("x").PadLeft(2, '0');
        return ret;
    }
    //返回大写的
    public static string MD51(string inputStr)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(inputStr, "MD5");
    }




    #region ========加密========


    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="Text"></param>
    /// <returns></returns>
    public static string Encrypt(string Text)
    {
        return Encrypt(Text, "litianping");
    }
    /// <summary> 
    /// 加密数据 
    /// </summary> 
    /// <param name="Text"></param> 
    /// <param name="sKey"></param> 
    /// <returns></returns> 
    public static string Encrypt(string Text, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray;
        inputByteArray = Encoding.Default.GetBytes(Text);
        des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
        des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").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();
    }


    #endregion


    #region ========解密========




    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="Text"></param>
    /// <returns></returns>
    public static string Decrypt(string Text)
    {
        return Decrypt(Text, "litianping");
    }
    /// <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(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
        des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").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());
    }


    #endregion


    #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值