Causa la necessità di memorizzare dati non in chiaro e di utilizzare l'algoritmo MD5, mi sono creato una piccola classe helper, che a sua volta utilizza la classe System.Security.Cryptography.MD5. Il codice della classe è il seguente:
 1         private string _dummyString = "!@HASH#!";
 2         System.Security.Cryptography.MD5 _md5 = null;
 3         public MD5Helper (){
 4   5             _md5 = System.Security.Cryptography.MD5.Create();            
 6         }
 7   8         public byte[] ComputeHash(string text)
 9         {
10             if (!string.IsNullOrEmpty(text))
11             {
12                 //Concatena il testo da cifrare con una stringa 13                 text += _dummyString;
14                 //Ritorna l'array di byte 15                 byte[] hashedBytes = _md5.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(text));
16                 return hashedBytes;
17             }else{
18                 throw new ArgumentException("Il valore del parametro text non può essere Null o Vuoto");
19             }
20         }
21  22         public bool Verify(string text, byte[] hashedBytes)
23         {
24             try 25             {
26                 if (!string.IsNullOrEmpty(text))
27                 {                    
28                     //Computa l'hash 29                     byte[] verifyHashedBytes = ComputeHash(text);
30                     string str1=  BitConverter.ToString(verifyHashedBytes).Replace ("-","");
31                     string str2 = BitConverter.ToString(hashedBytes).Replace("-", "");
32                     return str1.Equals(str2);
33                 }
34                 else 35                 {
36                     throw new ArgumentException("Il valore del parametro textToValidate non può essere Null o Vuoto");
37                 }
38             }
39             catch 
40             {
41                 return false;
42             }
43         }
44  45         public void Close()
46         {
47             _md5.Clear();
48         }
 
Esempio di utilizzo:
   
1 MD5Helper md5 = new MD5Helper();
2 hashed = md5.ComputeHash("PIPPO");
3 MessageBox.Show(md5.Verify("PIPPO", hashed).ToString ());