Tipp 0054 Hashcodes mit MD5 generieren
Autor/Einsender:
Datum:
  Michael Werner
06.09.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Die einfachste Methode, Passwörter zu speichern, ist es, einen Hashwert zu generieren. Aus dem Passwort wird ein Hashcode errechnet, der dann abgespeichert wird, und, aus dem sich das ursprüngliche Passwort nicht mehr ermitteln lässt.
Für ein eingegebenes Passwort wird dann auch ein Hashcode erstellt, und dieser mit dem abgespeicherten Hashcode verglichen. Im Namespace System.Security.Cryptography befinden sich die Möglichkeit von Verschlüsselungsalgorithmen, u. a. SHA1 oder der noch sicherere MD5-Algorithmus, der hier verwendet wird.
 
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text

Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Dim tmpHash1() As Byte
  Dim tmpHash2() As Byte

  Private Sub String1ToHash1()
    Dim tmpSource() As Byte
    Dim sSourcedata As String = TextBox1.Text
    tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourcedata)
    tmpHash1 = New MD5CryptoServiceProvider(). _
             ComputeHash(tmpSource)
    Label1.Text = ByteArrayToString(tmpHash1)
  End Sub

  Private Sub String2ToHash2()
    Dim tmpSource() As Byte
    Dim sSourcedata As String = TextBox2.Text
    tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourcedata)
    tmpHash2 = New MD5CryptoServiceProvider(). _
             ComputeHash(tmpSource)
    Label2.Text = ByteArrayToString(tmpHash2)
  End Sub

  Private Function CheckHashes(ByVal hash1 As Byte(), _
        ByVal hash2 As Byte()) As Boolean
    Dim bEqual As Boolean = False

    If hash2.Length = hash1.Length Then
      Dim i As Integer
      Do While (i < hash2.Length) AndAlso _
              (hash2(i) = hash1(i))
        i += 1
      Loop
      If i = hash2.Length Then
        bEqual = True
      End If
    End If

    Return bEqual
  End Function

  Private Function ByteArrayToString(ByVal arrInput() _
                As Byte) As String
    Dim i As Integer
    Dim sOutput As New StringBuilder(arrInput.Length)
    For i = 0 To arrInput.Length - 1
      sOutput.Append(arrInput(i).ToString("X2"))
    Next
    Return sOutput.ToString()
  End Function

  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    String1ToHash1()
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button2.Click
    String2ToHash2()
  End Sub

  Private Sub Button3_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button3.Click
    Button1.PerformClick()
    Button2.PerformClick()
    If CheckHashes(tmpHash1, tmpHash2) Then
      MessageBox.Show("Ok. Die Passwörter sind identisch!", _
           "Passwort-Hash prüfen", MessageBoxButtons.OK, _
           MessageBoxIcon.Information)
    Else
      MessageBox.Show( _
           "Falsch. Die Passwörter sind NICHT identisch!", _
           "Passwort-Hash prüfen", MessageBoxButtons.OK, _
           MessageBoxIcon.Exclamation)
    End If
  End Sub

  Private Sub Button4_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
  End Sub
End Class
 
Weitere Links zum Thema
GUID-Generator
Verschlüsseln mit DES und TripleDES

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (6,7 kB) Downloads bisher: [ 1326 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Freitag, 20. Januar 2012