MD5加密(文件和字符串,支持拖拽)
时间:2010-08-17 来源:无意创新
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using System.Threading;
namespace MD5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path = "";
private void button1_Click(object sender, EventArgs e)
{
if(this.openFileDialog1.ShowDialog()!=DialogResult.Cancel)
{
path=this.openFileDialog1.FileName;
}
if(!File.Exists(path)||path.Length==0)
{
return;
}
this.textBox3.Text = path;
start();
}
Thread thrad = null;
public void start()
{
this.label3.Visible = true;
this.textBox1.Clear();
thrad = new Thread(new ThreadStart(GetMD5));
thrad.Start();
}
public void GetMD5()
{
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
MD5CryptoServiceProvider getmd5 = new MD5CryptoServiceProvider();
byte[] hash = getmd5.ComputeHash(fs);
string result = BitConverter.ToString(hash);
result = result.Replace("-", "");
this.label3.Visible = false;
this.textBox1.Text = result;
}
catch (Exception ex)
{
ex.ToString();
}
finally
{
if (thrad.IsAlive)
{
thrad.Abort();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (thrad != null)
{
if (thrad.IsAlive)
{
thrad.Abort();
}
}
Application.Exit();
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Clear();
this.textBox2.Clear();
this.textBox3.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim().Equals("") || this.textBox2.Text.Trim().Equals(""))
{
MessageBox.Show("请填写MD5值");
return;
}
if (this.textBox1.Text.Trim().CompareTo(this.textBox2.Text.Trim()) == 0)
{
MessageBox.Show("相同");
}
else
{
MessageBox.Show("不相同");
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.label3.Visible = false;
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
this.textBox3.Clear();
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
this.textBox3.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
path = this.textBox3.Text.Trim();
if (!File.Exists(path) || path.Length == 0)
{
MessageBox.Show("文件有误");
return;
}
start();
}
private void button5_Click(object sender, EventArgs e)
{
if (!File.Exists(path) || path.Length == 0)
{
MessageBox.Show("文件有误");
return;
}
start();
}
private void button6_Click(object sender, EventArgs e)
{
string Test = this.textBox2.Text.Trim();
if (Test.Length == 0)
{
return;
}
GetString(Test);
}
public void GetString(string Test)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
StringBuilder pwd = new StringBuilder();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(Test));
for (int i = 0; i < s.Length; i++)
{
pwd.Append(s[i].ToString("X2"));
}
this.textBox1.Text = pwd.ToString();
}
private void button7_Click(object sender, EventArgs e)
{
string s = string.Format("欢迎你使用MD5加密小软件,\n1: 选择文件就自动计算MD5\n2: 计算按钮基本用不到,是对拖拉的备用\n3: 输入MD5可以像计算出来的比较\n4: 也可以在输入框输入字符串进行加密\n 感谢你的使用_CL!");
MessageBox.Show(s,"CL");
}
}
}
下载使用
相关阅读 更多 +