日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#MD5加密解密

發布時間:2023/12/8 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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"; 向量IV必須是?8?字節長度的十六進制數。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;}}} }

2.實驗效果

?

轉載于:https://www.cnblogs.com/kalezhangtao/p/9085698.html

總結

以上是生活随笔為你收集整理的c#MD5加密解密的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。