截图:
本想用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型.