Java generates the MD5 value of the string, java string md5

Source: Internet
Author: User

Java generates the MD5 value of the string, java string md5

The following code generates an MD5 value:

Public class MD5Test2 {public static void main (String [] args) {System. out. println (MD5Test2. MD5Operation ("hello"); System. out. println (MD5Test2. getMD5 ("hello");} // use java. the BigInteger class of the math package implements hexadecimal conversion public final static String MD5Operation (String s) {try {byte strTemp [] = s. getBytes (); MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (strTemp); byte B [] = md. digest (); BigInteger bigInt = new BigInteger (1, B); return bigInt. toString (16);} catch (NoSuchAlgorithmException e) {return null ;}// use the java underlying code to implement hexadecimal conversion public final static String getMD5 (String s) {char hexDigits [] = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F '}; try {byte strTemp [] = s. getBytes (); MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (strTemp); byte B [] = md. digest (); int len = B. length; char str [] = new char [len * 2]; int k = 0; for (int I = 0; I <len; I ++) {str [k ++] = hexDigits [B [I] >>> 4 & 0xf]; str [k ++] = hexDigits [B [I] & 0xf];} return new String (str);} catch (NoSuchAlgorithmException e) {return null ;}}}

Urgent. For example, how to use java to program string HelloWold with MD5 Encryption

Import java. security. MessageDigest;

Public class SpiderMD5 {
Public final static String MD5 (String s ){
String result = "";
Char hexDigits [] =
{'0', '1', '2', '3 ',
'4', '5', '6', '7 ',
'8', '9', 'A', 'B ',
'C', 'D', 'E', 'F '};
Try {

Byte [] strTemp = s. getBytes ();
MessageDigest mdTemp = MessageDigest. getInstance ("MD5 ");
MdTemp. update (strTemp );
Byte [] md = mdTemp. digest ();
Int j = md. length;
Char str [] = new char [j * 2];
Int k = 0;
For (int I = 0; I <j; I ++ ){
Byte B = md [I];
Str [k ++] = hexDigits [B> 4 & 0xf];
Str [k ++] = hexDigits [B & 0xf];
}
Result = new String (str );
} Catch (Exception e)
{E. printStackTrace ();}
Return result;
}

}

The MD5 values in different operating systems using java are different.

Different character set encoding

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.