终于实现VB.NET MD5加密

VB.NET MD5加密想着这个功能很普遍,就在网上找了一个代码

 '对strSource进行转码,然后再变成大写,再进行加密
    Function MD51(ByVal strSource As String, ByVal Code As Int16) As String
        Dim dataToHash As Byte()

        dataToHash = (New System.Text.UTF8Encoding).GetBytes(UCase(URLEncode(strSource)))


        Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash)
        Dim ATR As String = ""
        Dim i As Integer
        Select Case Code
            Case 16      '选择16位字符的加密结果   
                For i = 4 To 11
                    ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower

                Next
            Case 32      '选择32位字符的加密结果   
                For i = 0 To 15
                    ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
                Next
            Case Else       'Code错误时,返回全部字符串,即32位字符   
                For i = 0 To 15
                    ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
                Next
        End Select
        Return ATR
    End Function
    ''把带来中文的URL编码,都转换成GBK的编码方式
    Public Function URLEncode(ByRef strEnc As String) As String
        Dim strTmp2, strChar, strTmp, strRet As String
        strRet = ""
        Dim lngLoop As Integer
        For lngLoop = 0 To strEnc.Length - 1
            strChar = strEnc.Substring(lngLoop, 1)
            Select Case Asc(strChar)
                Case 48 To 57, 65 To 90, 97 To 122
                    strRet &= strChar
                Case 32
                    strRet &= "+"
                Case Else
                    strTmp = Hex(Asc(strChar))
                    If strTmp.Length > 4 Then
                        strTmp = strTmp.Substring(4)
                        strRet &= "%" & strTmp.Substring(0, 2)
                        If strTmp.Length > 2 Then
                            strTmp2 = strTmp.Substring(2)
                            strRet &= IIf(IsNumeric(strTmp.Substring(2, 1)), Chr(Val("&H" & strTmp2)), "%" & strTmp2)
                        End If
                    End If
            End Select
        Next
        URLEncode = strRet
    End Function

    ''把带来中文的URL编码,都转换成GBK的编码方式
    Public Function URLenc(ByVal strEnc As String) As String
        Dim lngLoop, lngAsc As Long
        URLenc = ""
        Dim strChr As String
        For lngLoop = 0 To strEnc.Length - 1
            strChr = strEnc.Substring(lngLoop, 1)
            If Math.Abs(Asc(strChr)) < 255 Then
                URLenc &= strChr
            Else
                lngAsc = Asc(strChr)
                If lngAsc < 0 Then lngAsc = lngAsc + 65536
                URLenc &= "%" & Hex((lngAsc And -256)  255) & "%" & Hex(lngAsc And 255)
            End If
        Next
    End Function

使用这个MD5加密后,有时对,有时不对,特别跟ASP的MD5有时都对不上。如果有小数点的数字,加密还无法对点加密。

今天完善一个支付宝批量转账的程序时也需要使用到MD5就再百度了一下。运气好看到了微软的VB.NET MD5的帮助。终于实现的简单的MD5加密。结果初步对比是正确的。

Public Function funcMD5(ByVal str As String) As String
        Using md5Hash As MD5 = MD5.Create()
            Dim hash As String = GetMd5Hash(md5Hash, str)
            Return hash
        End Using

    End Function
    Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String

        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data 
        ' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string.
        Return sBuilder.ToString()

    End Function 'GetMd5Hash

文章在:https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.md5.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

原文地址:https://www.cnblogs.com/xing979020/p/5111506.html