c#MD5加密解密
MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest開發(fā)出來,經(jīng)MD2、MD3和MD4發(fā)展而來。
是讓大容量信息在用數(shù)字簽名軟件簽署私人密匙前被"壓縮"成一種保密的格式(就是把一個(gè)任意長度的字節(jié)串變換成一定長的大整數(shù))。不管是MD2、MD4還是MD5,它們都需要獲得一個(gè)隨機(jī)長度的信息并產(chǎn)生一個(gè)128位的信息摘要。雖然這些算法的結(jié)構(gòu)或多或少有些相似,但MD2的設(shè)計(jì)與MD4和MD5完全不同,那是因?yàn)镸D2是為8位機(jī)器做過設(shè)計(jì)優(yōu)化的,而MD4和MD5卻是面向32位的電腦。
1.新建一個(gè)窗體應(yīng)用程序
有三種方式來加密,上兩種注釋的是不需要秘鑰加密的,下一種是是需要秘鑰和向量的;
代碼如下:
?
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"; 向量IV必須是?8?字節(jié)長度的十六進(jìn)制數(shù)。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); //把需要解密的字符串轉(zhuǎn)為8位無符號數(shù)組 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;}}} }2.實(shí)驗(yàn)效果
?
轉(zhuǎn)載于:https://www.cnblogs.com/kalezhangtao/p/9085698.html
總結(jié)
- 上一篇: 读烦恼杂货铺后感
- 下一篇: 解决vs2019 16.5版本中 编译虚