用.net写一个计算字符串MD5码的工具.
来源:互联网 发布:网络优化辛苦吗? 编辑:程序博客网 时间:2024/06/05 22:55
截图:
本想用google相册的.可google的相册现在还没恢复访问.(现在用的是blog.edu.cn的空间,传图片还是它的快.)
一直想写个计算字符串md5码的东西,可惜都得先吧它算法实现.呵呵,.net里已经内置md5和sha1算法了.
正好,我就完成了这个工具.
代码如下:
//http://blog.csdn.net/greenerycn
//请遵守署名非商业CC规则,谢谢
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 WindowsApplication1
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
...{
if (radioButton1.Checked)
...{
//MD5 算法的哈希值大小为 128 位_16字节_十六进制显示的话就是32个字符(每个字节2个字符显示).
string data_source=textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source=ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash=new MD5CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text =ByteArrayToString(tmp_hash);
}
else if (radioButton2.Checked)
...{
//sha1算法为160位.要40个字符
textBox2.Text = "";
string data_source = textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source = ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash = new SHA1CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text =ByteArrayToString(tmp_hash);
}
}
static string ByteArrayToString(byte[] tmp)
...{
StringBuilder Output = new StringBuilder(tmp.Length);
for (int i = 0; i < tmp.Length; i++)
...{
Output.Append(tmp[i].ToString("X2"));
//数字转换为十六进制数字的字符串。格式说明符的大小写指示对大于 9 的十六进制数字使用大写字符还是小写字符。例如,使用“X”产生“ABCDEF”,使用“x”产生“abcdef”。精度说明符指示结果字符串中所需的最少数字个数。如果需要的话,则用零填充该数字的左侧,以产生精度说明符给定的数字个数。只有整型才支持此格式。
}
return Output.ToString();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
...{
string data_source = textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source = ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash = new MD5CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text = ByteArrayToString(tmp_hash);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
...{
textBox2.Text = "";
string data_source = textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source = ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash = new SHA1CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text = ByteArrayToString(tmp_hash);
}
}
}
//请遵守署名非商业CC规则,谢谢
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 WindowsApplication1
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
...{
if (radioButton1.Checked)
...{
//MD5 算法的哈希值大小为 128 位_16字节_十六进制显示的话就是32个字符(每个字节2个字符显示).
string data_source=textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source=ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash=new MD5CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text =ByteArrayToString(tmp_hash);
}
else if (radioButton2.Checked)
...{
//sha1算法为160位.要40个字符
textBox2.Text = "";
string data_source = textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source = ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash = new SHA1CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text =ByteArrayToString(tmp_hash);
}
}
static string ByteArrayToString(byte[] tmp)
...{
StringBuilder Output = new StringBuilder(tmp.Length);
for (int i = 0; i < tmp.Length; i++)
...{
Output.Append(tmp[i].ToString("X2"));
//数字转换为十六进制数字的字符串。格式说明符的大小写指示对大于 9 的十六进制数字使用大写字符还是小写字符。例如,使用“X”产生“ABCDEF”,使用“x”产生“abcdef”。精度说明符指示结果字符串中所需的最少数字个数。如果需要的话,则用零填充该数字的左侧,以产生精度说明符给定的数字个数。只有整型才支持此格式。
}
return Output.ToString();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
...{
string data_source = textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source = ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash = new MD5CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text = ByteArrayToString(tmp_hash);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
...{
textBox2.Text = "";
string data_source = textBox1.Text;
byte[] tmp_source;
byte[] tmp_hash;
tmp_source = ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash = new SHA1CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text = ByteArrayToString(tmp_hash);
}
}
}
恩,当然还参考了网上的前辈得提示,让我明白了怎么把string转为byte型.
- 用.net写一个计算字符串MD5码的工具.
- 计算一个字符串的MD5
- 一个可以计算字符串和文件的MD5的类
- 写一个自己的MD5加密工具类
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- .NET中获取字符串的MD5码
- 分享一个自己写的字符串工具:字符串格式化拼接
- 在Redhat9 Linux下安装汉化eclipse3.1.2的c/c++开发平台
- 如果你是开游乐场的
- 通过C#程序生成数据库的实体类,根据SqlServer存储过程生成数据操作类
- ORACLE SQL and SQL*PLUS 第二部分
- 一个爱情衰人的轮回忏悔
- 用.net写一个计算字符串MD5码的工具.
- what is this?
- 12月21日——培训第27天
- C#经典书籍
- 一介草民对国内电子商务的挫见长篇连载
- HTML语言剖析
- SQL 语法大全
- SqlServer的更新锁(UPDLOCK)
- JavaScript 控制 Form 的 targe 属性