洪星的博客(原创版,新闻除外)

信息技术 软件开发 电信 移动通信(欢迎和我交流:QQ219402,15152399197)

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

代码
//如果是文本,先转换为字节序列,如果有汉字,请不要使用 ASCII 编码,会导致汉字变成问号
byte[] buffer = Encoding.UTF8.GetBytes("洪星的博客");//UTF-8 编码
buffer = Encoding.Default.GetBytes("洪星的博客");//GBK/GB2312 编码


//16字节,128位
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
byte[] h1 = MD5.ComputeHash(buffer);


//20字节,160位
SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
byte[] h2 = SHA1.ComputeHash(buffer);


//32字节,256位
SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider();
byte[] h3 = SHA256.ComputeHash(buffer);


//48字节,384位
SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider();
byte[] h4 = SHA384.ComputeHash(buffer);


//64字节,512位
SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider();
byte[] h5 = SHA512.ComputeHash(buffer);


string s1 = BitConverter.ToString(h1).Replace("-", string.Empty);
string s2 = BitConverter.ToString(h2).Replace("-", string.Empty);
string s3 = BitConverter.ToString(h3).Replace("-", string.Empty);
string s4 = BitConverter.ToString(h4).Replace("-", string.Empty);
string s5 = BitConverter.ToString(h5).Replace("-", string.Empty);


Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
Console.WriteLine(s4);
Console.WriteLine(s5);

 

测试网址:

http://www.hongcing.com/tool/md5

http://www.hongcing.com/tool/sha1

posted on 2010-02-03 13:54  洪星  阅读(2342)  评论(0编辑  收藏  举报