sqlserver教程网

当前位置: 主页 > 编程语言 > c# >

c# 如何利用md5 类加密解密

时间:2018-05-07 10:53来源:www.sqlserver.net.cn 作者:admin 点击:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; namespace Windowscsharpone { public par



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
 
namespace Windowscsharpone
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
 
            key = "a1ccada";
        }
        string key;
        private void Form4_Load(object sender, EventArgs e)
        {
 
        }
 
        private string criptog(string textto,string strhash) 
        {
            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
 
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
 
            byte[] byehash, byteText;
 
            byehash = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(strhash));
            byteText = UTF8Encoding.UTF8.GetBytes(textto);
 
            tdsp.Key = byehash;
            tdsp.Mode = CipherMode.ECB;
 
            return
             Convert.ToBase64String(   tdsp.CreateEncryptor().TransformFinalBlock(byteText,0,byteText.Length));
 
        }
 
 
        private string descriptog(string textto, string strhash)
        {
            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
 
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
 
            byte[] byehash, byteText;
 
            byehash = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(strhash));
            byteText =Convert.FromBase64String(textto);
 
            tdsp.Key = byehash;
            tdsp.Mode = CipherMode.ECB;
 
            return
            UTF8Encoding.UTF8.GetString(tdsp.CreateDecryptor().TransformFinalBlock(byteText, 0, byteText.Length));
 
        }
 
 
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入字符串");
            }
 
            textBox2.Text = criptog(textBox1.Text,key);
 
            }
 
        private void button2_Click(object sender, EventArgs e)
        {
            textBox3.Text = descriptog(textBox2.Text,key);
        }
    }
}
(责任编辑:admin)
顶一下
(16)
94.1%
踩一下
(1)
5.9%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
验证码: 点击我更换图片