Source Code: Windows Forms Password Encryption Visual Basic 2005
This article and source code explain how to use the .NET Cryptography MD5CryptoServiceProvider method to encrypt a string used as a password in a Windows Forms application.
The .NET MD5CryptoServiceProvider computes the MD5 hash value for input data using the implementation provided by the cryptographic service provider.
Hash functions map binary strings of an arbitrary length to small binary strings of a fixed length. A cryptographic hash function has the property that it is computationally infeasible to find two distinct inputs that hash to the same value; that is, hashes of two sets of data should match if the corresponding data also matches. Small changes to the data result in large, unpredictable changes in the hash.
The hash size for the MD5 algorithm is 128 bits.
Included in the source code is a class named EncryptPassword. This class contains a method named 'EncryptString' that uses the Text namespace's UnicodeEncoding method to convert a string passed into the class into a Byte array, encrypts the string using the Cryptography namespace's MD5CryptoServiceProvider and returns the encrypted string to the caller.
Code Example
Public Class EncryptPassword
Public Shared Function EncryptString(ByVal sourceString As String) As String
' Declare a variable of type Byte array named sourceStringToBytes.
' Call a UnicodeEncoding object's GetBytes method, passing it the sourceString.
' Assign the resulting byte array to the sourceStringToBytes variable.
Dim sourceStringToBytes As Byte() = (New UnicodeEncoding()).GetBytes(sourceString)
' Declare a variable of type Byte array named hashedBytes.
' Call a MD5CryptoServiceProvider objects's ComputeHash method
' passing it the sourceStringToBytes array.
' Assign the resulting byte array to the hashedBytes variable.
Dim hashedBytes As Byte() = New MD5CryptoServiceProvider().ComputeHash(sourceStringToBytes)
' Use the BitConverter class's shared ToString method to return
' the bytes encrypted as a string.
Return BitConverter.ToString(hashedBytes)
End Function
End Class
The Windows Forms examples demonstrates the EncryptPassword class in action.

Behind the test form are two methods that use the EncryptPassword class.
The first, 'EncryptAndSave', takes a password entered by a user, encrypts it with the EncryptPassword class, then saves the encrypted password to file.
The second, 'ValidatePassword', takes a password entered by a user, encrypts it with the EncryptPassword class, retrieves a encrypted password from file, and validates that the password provided by the user matches the password stored to file.
For more information visit the links below:
MD5CryptoServiceProvider Class
Click the link above to download Visual Basic source code in a Visual Studio solution which demonstrates how to how to use the .NET Cryptography MD5CryptoServiceProvider method to encrypt a string used as a password in a Windows Forms application.
Source code for Visual Basic 2002 and Visual Basic 2003 can be found at:
Windows Forms Password Encryption With Visual Basic 2002 and 2003
mike mcintyre http://www.getdotnetcode.com