ASP.Net中MD5加密-16位32位

None.gif public   string  md5( string  str, int  code)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(code==16//16位MD5加密(取32位加密的9~25字符)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower().Substring(8,16) ;
ExpandedSubBlockEnd.gif}
 
InBlock.gif
else//32位加密
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower();
ExpandedSubBlockEnd.gif}
 
ExpandedBlockEnd.gif}

None.gif

   关于ASP,ASP.NET,VB.NET里的MD5加密函数 在ASP中,我们使用过如动网论坛等用的ASP中MD5函数加密出的字符串则如:
1165d25d8cd021d5

   而在Asp.Net中下面的方法: 代码

None.gif System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text, " MD5 " ) // 进行MD5加密出来的结果却是: 
None.gif
12C403B91165D25D8CD021D5F9B5BB7F
None.gif

   究其原因,是因为,在ASP中的MD5函数是使用了32位MD5 Hashvalue中的第9~25位再变小写作为密文。知道这个原因,把ASP.NET的结果稍加处理就可以用来比较老数据库中的密码字串了。
在ASP的MD5函数中第353、354行: 代码
´MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
MD5=LCase(WordToHex(b) & WordToHex(c)) ´I crop this to fit 16byte database password :D

第一句是取全部的32位密文,第二句则是取中间第9~25位为16位密文。

到了vb.net问题又来了,在vb.net里,无法用到System.Web.Security名称空间,无法用上面简单的办法进行MD5加密。故我写了下面这个函数来处理:

VB.NET: 代码´MD5 加密函数 

ExpandedBlockStart.gif ContractedBlock.gif Public   Shared   Function MD5() Function MD5(ByVal strSource As StringByVal Code As Int16) As String 
InBlock.gif´这里用的是ascii编码密码原文,如果要用汉字做密码,可以用UnicodeEncoding,但会与ASP中的MD5函数不兼容 
InBlock.gif
Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(strSource) 
InBlock.gif
Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash) 
InBlock.gif
Dim i As Integer 
InBlock.gif
Select Case Code 
InBlock.gif
Case 16 ´选择16位字符的加密结果 
InBlock.gif
For i = 4 To 11 
InBlock.gifMD5 
+= Hex(hashvalue(i)).ToLower 
InBlock.gif
Next 
InBlock.gif
Case 32 ´选择32位字符的加密结果 
InBlock.gif
For i = 0 To 15 
InBlock.gifMD5 
+= Hex(hashvalue(i)).ToLower 
InBlock.gif
Next 
InBlock.gif
Case Else ´Code错误时,返回全部字符串,即32位字符 
InBlock.gif
For i = 0 To hashvalue.Length - 1 
InBlock.gifMD5 
+= Hex(hashvalue(i)).ToLower 
InBlock.gif
Next 
InBlock.gif
End Select 
ExpandedBlockEnd.gif
End Function

 

原文链接:http://dev.poptool.net/asp/2005-11-22/0511221855.html

转载于:https://www.cnblogs.com/ghx88/archive/2006/06/16/427219.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值