Reference namespace first:
Using system. Security. cryptography;
Using system. text;
Then:
Md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();
String source = "helloworld ";
Byte [] message;
Message = encoding. Default. getbytes (source );
// Method 1
// The computehash method is used to calculate the MD5 value of a simple string.
Md5.computehash (Message );
Console. writeline (convert. tobase64string (md5.hash ));
// Method 2
// Use the transformfinalblock method, which is suitable for the use of a small amount of raw data
Md5.initialize ();
Md5.transformfinalblock (message, 0, message. Length );
Console. writeline (convert. tobase64string (md5.hash ));
// Method 3
// This method is equivalent to method 2
Md5.initialize ();
Md5.transformblock (message, 0, message. length,
Message, 0); // Note: Output bytes must equal input bytes
Md5.transformfinalblock (message, 0, 0 );
Console. writeline (convert. tobase64string (md5.hash ));
// Method 4
// The original message is encoded twice and the result is the same as above. It is suitable for calculating a large amount of raw data, such as calculating the MD5 value of a file.
Md5.initialize ();
Message = encoding. Default. getbytes ("hello ");
Md5.transformblock (message, 0, message. length,
Message, 0 );
Message = encoding. Default. getbytes ("world ");
Md5.transformfinalblock (message, 0, message. Length );
Console. writeline (convert. tobase64string (md5.hash ));