2012-03-08 6 views
1

MD5CryptoServiceProviderとHashbytesが同じMD5値を返すようにするにはどうすればよいですか?MD5CryptoServiceProviderとHashbytes

+0

私は実際に質問を理解しますが、 '選択HASHBYTESをhttp://msdn.microsoft.com/en-us/library/system.security.cryptography.md5cryptoserviceprovider.aspxに例を使用して呼び出すことはありません( 'md5'、 'Hello World!') 'は同じ値を返します 私はそれが16進数への変換と関係していると思います: ' for(int i = 0; i

答えて

1

おそらくこれが役立ちます。

Public Function ComputeMD5Hash(ByVal strPlainText As String, Optional ByVal bytSalt() As Byte = Nothing) As String 
    Try 
     Dim bytPlainText As Byte() = Encoding.UTF8.GetBytes(strPlainText) 
     Dim hash As HashAlgorithm = New MD5CryptoServiceProvider() 

     If bytSalt Is Nothing Then 
      Dim rand As New Random 
      Dim intSaltSize As Integer = rand.Next(intMinSalt, intMaxSalt) 

      bytSalt = New Byte(intSaltSize - 1) {} 

      Dim rng As New RNGCryptoServiceProvider 
      rng.GetNonZeroBytes(bytSalt) 
     End If 

     Dim bytPlainTextWithSalt() As Byte = New Byte(bytPlainText.Length + bytSalt.Length - 1) {} 

     bytPlainTextWithSalt = ConcatBytes(bytPlainText, bytSalt) 

     Dim bytHash As Byte() = hash.ComputeHash(bytPlainTextWithSalt) 
     Dim bytHashWithSalt() As Byte = New Byte(bytHash.Length + bytSalt.Length - 1) {} 

     bytHashWithSalt = ConcatBytes(bytHash, bytSalt) 

     Return Convert.ToBase64String(bytHashWithSalt) 
    Catch ex As Exception 
     Return String.Format(strTextErrorString, ex.Message) 
    End Try 
End Function 

'Verify a string against a hash generated with the ComputeMD5Hash function above. 
Public Function VerifyHash(ByVal strPlainText As String, ByVal strHashValue As String) As Boolean 
    Try 
     Dim bytWithSalt As Byte() = Convert.FromBase64String(strHashValue) 

     If bytWithSalt.Length < intHashSize Then Return False 

     Dim bytSalt() As Byte = New Byte(bytWithSalt.Length - intHashSize - 1) {} 

     Array.Copy(bytWithSalt, intHashSize, bytSalt, 0, bytWithSalt.Length - intHashSize) 

     Dim strExpectedHashString As String = ComputeMD5Hash(strPlainText, bytSalt) 

     Return strHashValue.Equals(strExpectedHashString) 
    Catch ex As Exception 
     Return Nothing 
    End Try 
End Function 

'Simple function to concatenate two byte arrays. 
Private Function ConcatBytes(ByVal bytA() As Byte, ByVal bytB() As Byte) As Byte() 
    Try 
     Dim bytX() As Byte = New Byte(((bytA.Length + bytB.Length)) - 1) {} 

     Array.Copy(bytA, bytX, bytA.Length) 
     Array.Copy(bytB, 0, bytX, bytA.Length, bytB.Length) 

     Return bytX 
    Catch ex As Exception 
     Return Nothing 
    End Try 

End Function 

'A function to convert a string into a 32 byte key. 
Private Function ConvertKeyToBytes(ByVal strKey As String) As Byte() 
    Try 
     Dim intLength As Integer = strKey.Length 

     If intLength < intKeySize Then 
      strKey &= Strings.StrDup(intKeySize - intLength, chrKeyFill) 
     Else 
      strKey = strKey.Substring(0, intKeySize) 
     End If 

     Return Encoding.UTF8.GetBytes(strKey) 
    Catch ex As Exception 
     Return Nothing 
    End Try 
End Function 
関連する問題