終於實現VB.NET MD5加密

VB.NET MD5加密想着這個功能很廣泛,就在網上找了一個代碼oop

 '對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有時都對不上。若是有小數點的數字,加密還沒法對點加密。ui

今天完善一個支付寶批量轉帳的程序時也須要使用到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加密

相關文章
相關標籤/搜索