用.net写一个计算字符串MD5码的工具.

截图:

本想用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
... {
publicpartialclassForm1:Form
...{
publicForm1()
...{
InitializeComponent();
}


privatevoidtextBox1_TextChanged(objectsender,EventArgse)
...{
if(radioButton1.Checked)
...{
//MD5算法的哈希值大小为128位_16字节_十六进制显示的话就是32个字符(每个字节2个字符显示).

stringdata_source=textBox1.Text;
byte[]tmp_source;
byte[]tmp_hash;
tmp_source
=ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash
=newMD5CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text
=ByteArrayToString(tmp_hash);
}

elseif(radioButton2.Checked)
...{
//sha1算法为160位.要40个字符
textBox2.Text="";
stringdata_source=textBox1.Text;
byte[]tmp_source;
byte[]tmp_hash;
tmp_source
=ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash
=newSHA1CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text
=ByteArrayToString(tmp_hash);

}



}

staticstringByteArrayToString(byte[]tmp)
...{

StringBuilderOutput
=newStringBuilder(tmp.Length);
for(inti=0;i<tmp.Length;i++)
...{
Output.Append(tmp[i].ToString(
"X2"));
//数字转换为十六进制数字的字符串。格式说明符的大小写指示对大于9的十六进制数字使用大写字符还是小写字符。例如,使用“X”产生“ABCDEF”,使用“x”产生“abcdef”。精度说明符指示结果字符串中所需的最少数字个数。如果需要的话,则用零填充该数字的左侧,以产生精度说明符给定的数字个数。只有整型才支持此格式。

}

returnOutput.ToString();
}


privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)
...{
stringdata_source=textBox1.Text;
byte[]tmp_source;
byte[]tmp_hash;
tmp_source
=ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash
=newMD5CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text
=ByteArrayToString(tmp_hash);
}


privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)
...{
textBox2.Text
="";
stringdata_source=textBox1.Text;
byte[]tmp_source;
byte[]tmp_hash;
tmp_source
=ASCIIEncoding.ASCII.GetBytes(data_source);
tmp_hash
=newSHA1CryptoServiceProvider().ComputeHash(tmp_source);
textBox2.Text
=ByteArrayToString(tmp_hash);
}

}

}

恩,当然还参考了网上的前辈得提示,让我明白了怎么把string转为byte型.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值