Base64、Md5、Des加密

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.Text;
using System.IO;
using System.Security.Cryptography;

public partial class _Default : System.Web.UI.Page
{
    //默认密钥向量
    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region base64
    //加密
    protected void Button1_Click(object sender, EventArgs e)
    {
        string context = this.TextBox1.Text.Trim();
        byte[] arrs = Encoding.GetEncoding("UTF-8").GetBytes(context);
        this.TextBox2.Text = Convert.ToBase64String(arrs);
    }
    //解密
    protected void Button2_Click(object sender, EventArgs e)
    {
        string context = this.TextBox1.Text.Trim();
        char[] chars = context.ToCharArray();
        byte[] byts = Convert.FromBase64CharArray(chars, 0, chars.Length);
        this.TextBox2.Text =Encoding.GetEncoding("UTF-8").GetString(byts);
        //Convert.to
    }
#endregion

    #region MD5
    protected void Button3_Click(object sender, EventArgs e)
    {
        string context =this.TextBox3.Text.Trim();
        this.TextBox4.Text = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(context, "md5");
    }
    #endregion

    #region Des加密解密
    //加密
    protected void Button4_Click(object sender, EventArgs e)
    {
        string key = this.TexKey.Text.Trim();
        //StringBuilder key = new StringBuilder(this.TexKey.Text.Trim());


        if (key.Length < 8)
        {
            for (int i = key.Length; i <= 8; i++)
            {
                key += "-";

            }
            this.TexKey.Text = key.ToString();
        }
        //获取要加密的字符串
        string encryptString = this.TexDes.Text.Trim();


        byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

        //byte[] keyIV = keyBytes;
        byte[] keyIV = Keys;


        byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);

        cStream.Write(inputByteArray, 0, inputByteArray.Length);
        cStream.FlushFinalBlock();
        //return Convert.ToBase64String(mStream.ToArray());
        this.txtResult.Text = Convert.ToBase64String(mStream.ToArray());
    }
    //解密
    protected void Button5_Click(object sender, EventArgs e)
    {
        //获取解密字符串
        string decryptString = this.TexDes.Text.Trim();
        //获取密钥
        string key = this.TexKey.Text.Trim();


        byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

        //byte[] keyIV = keyBytes;
        byte[] keyIV = Keys;
        try
        {
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            //return Encoding.UTF8.GetString(mStream.ToArray());

            this.txtResult.Text = Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch
        {
            TexKey.Text = "密码错误!";

        }
    }
    #endregion
}

posted @ 2010-06-23 16:43  JasonNET  阅读(407)  评论(0编辑  收藏  举报