C # MD5-16 bit encryption instance, 32 bit encryption instance (two methods)

Source: Internet
Author: User
1 . MD5 16-bit encryption instance
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Security. cryptography;

Namespace MD5
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Console. writeline (usermd5 ("8"));
Console. writeline (getmd5str ("8"));
}
/// <Summary>
/// The MD5 16-bit encrypted password is capitalized
/// </Summary>
/// <Param name = "convertstring"> </param>
/// <Returns> </returns>
Public Static String Getmd5str ( String Convertstring)
{
Md5cryptoserviceprovider MD5 = New Md5cryptoserviceprovider ();
String T2 = Bitconverter. tostring (md5.computehash (utf8encoding. Default. getbytes (convertstring )), 4 , 8 );
T2 = T2.replace ( " - " , "" );
Return T2;
}

/// <Summary>
/// The MD5 16-bit encrypted password is lowercase
/// </Summary>
/// <Param name = "convertstring"> </param>
/// <Returns> </returns>
Public Static String Getmd5str ( String Convertstring)
{
Md5cryptoserviceprovider MD5 = New Md5cryptoserviceprovider ();
String T2 = Bitconverter. tostring (md5.computehash (utf8encoding. Default. getbytes (convertstring )), 4 , 8 );
T2 = T2.replace ( " - " , "" );

T2 = t2.tolower ();

ReturnT2;
}


/// <Summary>
/// MD5 32-bit encryption
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Static String Usermd5 ( String Str)
{
String Cl = STR;
String PWD = "" ;
MD5 = Md5.create (); // Instantiate an MD5 object
// After encryption, it is an array of the byte type. Pay attention to the selection of UTF-8/Unicode encoding.
Byte [] S = Md5.computehash (encoding. utf8.getbytes (CL ));
// Converts an array of the byte type to a string through a loop. This string is obtained by regular character formatting.
For ( Int I = 0 ; I < S. length; I ++ )
{
// Use the hexadecimal format of the obtained string. The characters in the format are lowercase letters. If uppercase letters (x) are used, the characters in the format are uppercase letters.

PWD = PWD + S [I]. tostring ( " X " );

}
Return PWD;
}
}
}

Using system. Security. cryptography;
Using system. text;

Public static string stringtomd5hash (string inputstring)
{
Md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();
Byte [] encryptedbytes = md5.computehash (encoding. ASCII. getbytes (inputstring ));
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <encryptedbytes. length; I ++)
{
SB. appendformat ("{0: X2}", encryptedbytes [I]);
}
Return sb. tostring ();
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.