加密类

来源:互联网 发布:短融网靠谱吗 知乎 编辑:程序博客网 时间:2024/06/09 12:17
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// DEcrypto 的摘要说明
/// </summary>
public class DEcrypto
{
    public DEcrypto()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    #region ==========加密(不可逆)==========
    public static string MD5(string text)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] inputText = Encoding.Default.GetBytes(text);
        byte[] buffer = md5.ComputeHash(inputText);
        md5.Clear();
        string str = string.Empty;
        for (int i = 0; i < buffer.Length; i++)
        {
            str += buffer[i].ToString("x").PadLeft(2, '0');
        }
        return str;
    }
    #endregion
    #region ==========加密(可逆)==========
    public static string Encrypt(string text)
    {
        return Encrypt(text, "ghiegcge");
    }
    public static string Encrypt(string text, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inpuText = Encoding.Default.GetBytes(text);
        des.IV = Encoding.Default.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile("sKey", "md5").Substring(0, 8));
        des.Key = Encoding.Default.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile("sKey", "md5").Substring(0, 8));
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(inpuText, 0, inpuText.Length);
        cs.FlushFinalBlock();
        StringBuilder sb = new StringBuilder();
        foreach (byte b in ms.ToArray())
        {
            sb.AppendFormat("{0:x2}", b);
        }
        return sb.ToString();
    }
    #endregion
    #region ============解密(可逆)=========
    public static string Decrypt(string text)
    {
        return Decrypt(text, "ghiegcge");
    }
    public static string Decrypt(string text, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputText = new byte[text.Length / 2];
        int i, x;
        for (x = 0; x < text.Length / 2; x++)
        {
            i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
            inputText[x] = (byte)i;
        }
        des.IV = Encoding.Default.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile("sKey", "md5").Substring(0, 8));
        des.Key = Encoding.Default.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile("sKey", "md5").Substring(0, 8));
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(inputText, 0, inputText.Length);
        cs.FlushFinalBlock();
        return Encoding.Default.GetString(ms.ToArray());
    }
    #endregion
}
原创粉丝点击