MD5 Java Encryption

Source: Internet
Author: User

Package com. mobi5.commons. utils;

Import java. Security. messagedigest;

Public class md5encrypt {
Public md5encrypt (){}

Private Final Static string [] hexdigits = {
"0", "1", "2", "3", "4", "5", "6", "7 ",
"8", "9", "A", "B", "C", "D", "E", "F "};

/**
* Convert the byte array to a hexadecimal string.
* @ Param B byte array
* @ Return hexadecimal string
*/
Public static string bytearraytostring (byte [] B ){
Stringbuffer resultsb = new stringbuffer ();
For (INT I = 0; I <B. length; I ++ ){
Resultsb. append (bytetohexstring (B [I]); // if you use this function to convert, you can obtain the hexadecimal representation of the encrypted result, that is, the combination of digits and letters.
// Resultsb. append (bytetonumstring (B [I]); // use this function to return the 10-digit numeric string of the encrypted result, that is, the Full Digit form
}
Return resultsb. tostring ();
}

Private Static string bytetonumstring (byte B ){

Int _ B = B;
If (_ B <0 ){
_ B = 256 + _ B;
}

Return string. valueof (_ B );
}

Private Static string bytetohexstring (byte B ){
Int n = B;
If (n <0 ){
N = 256 + N;
}
Int d1 = N/16;
Int D2 = n % 16;
Return hexdigits [D1] + hexdigits [D2];
}

Public static string md5encode (string origin ){
String resultstring = NULL;

Try {
Resultstring = origin;
Messagedigest MD = messagedigest. getinstance ("MD5 ");
Resultstring = bytearraytostring (Md. Digest (resultstring. getbytes ("gb2312 ")));
}
Catch (exception ex ){

}
Return resultstring;
}

Public static void main (string [] ARGs ){
System. Out. println (md5encode ("3481471j2hy6g $ Kuh * grt73fv9kjdy34 "));
// System. Out. println (md5encode (md5encode ("China ")));
}
}

 

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.