c#MD5加密解密

MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展而来。

是让大容量信息在用数字签名软件签署私人密匙前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数)。不管是MD2、MD4还是MD5,它们都需要获得一个随机长度的信息并产生一个128位的信息摘要。虽然这些算法的结构或多或少有些相似,但MD2的设计与MD4和MD5完全不同,那是因为MD2是为8位机器做过设计优化的,而MD4和MD5却是面向32位的电脑。

1.新建一个窗体应用程序

有三种方式来加密,上两种注释的是不需要秘钥加密的,下一种是是需要秘钥和向量的;

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AES加密解密
{
    public partial class MD5_DES_加密解密 : Form
    {
        public MD5_DES_加密解密()
        {
            InitializeComponent();
        }

        private void MD5_DES_加密解密_Load(object sender, EventArgs e)
        {

        }

        /// <summary>

        /// 32位MD5加密

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>
        /*  private void button1_Click(object sender, EventArgs e)
          {//这两种方式都可以
              /*  byte[] result = Encoding.Default.GetBytes(this.textBox1.Text.Trim());    //textBox1为输入密码的文本框
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] output = md5.ComputeHash(result);
                this.richTextBox1.Text = BitConverter.ToString(output).Replace("-", "");  //richTextBox1为输出加密文本的文本框
           */
        /*  if (richTextBox1.Text!=null)
         {
             richTextBox1.Text = "";
             string str = textBox1.Text;

             MD5 md = MD5.Create();

             byte[] bytes = md.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));


             foreach (byte b in bytes)

             {

                 richTextBox1.Text += b.ToString();

             }

         }

     }*/

        private void button1_Click(object sender, EventArgs e)
        {//加密-此种方法要输入秘钥键值
            richTextBox1.Text = Encode(textBox1.Text, textBox2.Text, textBox3.Text);
        }

        private void button2_Click(object sender, EventArgs e)

        {//解密-此种方法要输入秘钥键值

            richTextBox1.Text = Decode(textBox4.Text, textBox2.Text, textBox3.Text);

        }

       
        //加密

        public static string Encode(string data, string Key_64, string Iv_64)

        {

            string KEY_64 = Key_64;// "VavicApp";

            string IV_64 = Iv_64;// "VavicApp";//64位的键值和IV值要为8位

            try

            {

                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);

                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

                int i = cryptoProvider.KeySize;

                MemoryStream ms = new MemoryStream();

                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

                StreamWriter sw = new StreamWriter(cst);

                sw.Write(data);

                sw.Flush();

                cst.FlushFinalBlock();

                sw.Flush();

                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

            }

            catch (Exception x)

            {

                return x.Message;

            }

        }
        //解密

        public static string Decode(string data, string Key_64, string Iv_64)

        {

            string KEY_64 = Key_64;// "VavicApp";密钥

            string IV_64 = Iv_64;// "VavicApp"; 向量

            try

            {

                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);

                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

                byte[] byEnc;

                byEnc = Convert.FromBase64String(data); //把需要解密的字符串转为8位无符号数组

                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream(byEnc);

                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);

                StreamReader sr = new StreamReader(cst);

                return sr.ReadToEnd();

            }

            catch (Exception x)

            {

                return x.Message;

            }

        }

    }
}

 实验效果明天加

猜你喜欢

转载自www.cnblogs.com/kalezhangtao/p/9085698.html