You're here: Snippet Directory » Microsoft .NET » Security (14)
Language:

MD5 mit CSharp

Language: Deutsch
Programming Language: C#
Published by: Thomas
Last Update: 5/3/2006
Views: 1499

Description

Was mit PHP ein Klacks ist, gestaltet sich auch mit C# 2.0 noch immer etwas umfangreicher: das "verschlüsseln" eines Strings via MD5-Algo.

Code

1 public static string MD5(string password) 2 { 3 byte[] textBytes = System.Text.Encoding.Default.GetBytes(password); 4 try 5 { 6 System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler; 7 cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider(); 8 byte[] hash = cryptHandler.ComputeHash(textBytes); 9 string ret = ""; 10 foreach (byte a in hash) 11 { 12 if (a < 16) 13 ret += "0" + a.ToString("x"); 14 else 15 ret += a.ToString("x"); 16 } 17 return ret; 18 } 19 catch 20 { 21 throw; 22 } 23 }

2 comments

1

Großartig, danke!!!

Friday, May 19, 2006 1:12:23 PM from Klaus
2

Die Zeilen 12 bis 15 kann man auch zusammenfassen zu einer Zeile Code.

ALT:
12 if (a < 16)
13 ret += "0" + a.ToString("x");
14 else
15 ret += a.ToString("x");


NEU:
12 ret += a.ToString("x2");

Sunday, July 13, 2008 7:31:02 PM from Mathias

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS