1。web项目中方法 :
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("aaaa", "MD5")
查看文档方法:
Public Shared Function HashPasswordForStoringInConfigFile(ByVal password As String, ByVal passwordFormat As String) As String
成员属于: System.Web.Security.FormsAuthentication
摘要:
给定标识哈希类型的密码和字符串,该例程产生一个适合存储在配置文件中的哈希密码。
参数:
password: 要进行哈希运算的密码。
passwordFormat: 要使用的哈希算法。选项有“sha1”或“md5”。
返回值:
返回一个包含哈希密码的 String。
2。vb的应用程序:
Private Function md5()
Dim tmpSource() As Byte
Dim tmpHash() As Byte
tmpSource = ASCIIEncoding.ASCII.GetBytes("aaaa") ' 将指定的 System.String 编码为字节数组。 ASCII(7 位)字符集的编码
tmpHash = New MD5CryptoServiceProvider().ComputeHash(tmpSource) ' 计算指定字节数组的哈希值。
Dim i As Integer
Dim sOutput As New StringBuilder(tmpHash.Length)
For i = 0 To tmpHash.Length - 1
sOutput.Append(tmpHash(i).ToString("x2"))
'ToString:使用指定格式将此实例的数值转换为它的等效字符串值
'x2:转为2位16进制小写 ,X2就是转为2位16进制大写
Next
End Function
Public Shared Function md5str(ByRef strSource As String)
Dim dataToHash As Byte() = (New System.Text.UnicodeEncoding).GetBytes(strSource.ToCharArray)
Dim hashvalue As Byte() = CType(Cryptography.CryptoConfig.CreateFromName("MD5"), Cryptography.HashAlgorithm).ComputeHash(dataToHash)
Dim strSB As New System.Text.StringBuilder
For i As Int16 = 0 To hashvalue.Length - 1
strSB.Append(hashvalue(i).ToString("x2"))
Next
Dim creatMD5 = strSB.ToString
Return creatMD5
End Function