In ASP, we have used the MD5 function in ASP used by Rudong.com Forum, etc. The string encrypted by the MD5 function is as follows:
1165d25d8cd021d5
And in Asp.Net the following method:
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text,"MD5")
The result of MD5 encryption is:
12C403B91165D25D8CD021D5F9B5BB7F
The reason is that the MD5 function in ASP uses the 9th to 25th bits of the 32-bit MD5 Hashvalue and then changes it to lowercase as ciphertext. Knowing this, the ASP.NET results can be used to compare password strings in older databases with a little processing.
In ASP’s MD5 function, lines 353 and 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
The first sentence is to take all 32-bit ciphertext, and the second sentence is to take the 9th to 25th bits in the middle as the 16-bit ciphertext.
When it comes to vb.net, the problem comes again. In vb.net, the System.Web.Security namespace cannot be used, and the simple method above cannot be used for MD5 encryption. So I wrote the following function to handle it:
VB.NET:
'MD5 encryption function
Public Shared Function MD5(ByVal strSource As String, ByVal Code As Int16) As String
'The original text of the ascii encoded password is used here. If you want to use Chinese characters as the password, you can use UnicodeEncoding, but it will be incompatible with the MD5 function in ASP.
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 i As Integer
Select Case Code
Case 16 'Select the encryption result of 16-bit characters
For i = 4 To 11
MD5 += Hex(hashvalue(i)).ToLower
Next
Case 32 'Select the encryption result of 32-bit characters
For i = 0 To 15
MD5 += Hex(hashvalue(i)).ToLower
Next
Case Else 'When the Code is wrong, return the entire string, that is, 32-bit characters
For i = 0 To hashvalue.Length - 1
MD5 += Hex(hashvalue(i)).ToLower
Next
End Select
End Function
Source: AndyDavis BLOG