vb.net 使用MD5密碼加密

时间:2022-03-13 05:43:10

Function MD5(ByVal strSource As String, ByVal Code As Int16) As String'使用MD5加密
Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(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

Private Sub A1()
TextBox2.Text = MD5(TextBox1.Text, 16) '16位字符的加密结果
End Sub

Private Sub A2()
TextBox2.Text = MD5(TextBox1.Text, 32) '32位字符的加密结果
End Sub