MessageDigest class provides encryption algorithms such as MD5 or Sha

Source: Internet
Author: User
Tags md5 encryption

MessageDigest can use the encryption method has md2\md5\sha-1\sha-256\sha-384\sha-512, use time only to replace the corresponding parameter value can be

MessageDigest MD5 = messagedigest.getinstance ("MD5");

MessageDigest MD5 = messagedigest.getinstance ("MD2");

MessageDigest MD5 = messagedigest.getinstance ("SHA-1");

````````````````````````````````````````````````

Code that uses MD5 encryption as an example, in which the code is converted to hexadecimal format data output in two ways

@Test Public voidTest ()throwsnosuchalgorithmexception, unsupportedencodingexception {String str= "a"; MessageDigest MD5= Messagedigest.getinstance ("MD5"); byte[] bytes = Md5.digest (str.getbytes ("Utf-8")); StringBuilder StringBuilder=NewStringBuilder ();  for(inti = 0; i < bytes.length; i++) {            //0x is hexadecimal, using &0xff is a byte to int, do the complement processing, to ensure the consistency of binary data, the computer stores the complement//integer to hexadecimal using the Tohexstring method of integerString TempStr = integer.tohexstring (Bytes[i] & 0xFF); if(tempstr.length () = = 1)                //if the length is 1 complement 0Stringbuilder.append ("0"). Append (TEMPSTR); Elsestringbuilder.append (TEMPSTR); //delimiters such as: 0c:c1:75:b9:c0:f1:b6:a8:31:c3:99:e2:69:77:26:61            if(I < Bytes.length-1) Stringbuilder.append (":");        } System.out.println (Stringbuilder.tostring ()); StringBuilder=NewStringBuilder ();  for(inti = 0; i < bytes.length; i++) {            intChartindex = bytes[i] & 0xff; //integer to hex, less than 16 for 0~f, 0 output            if(Chartindex < 16) {stringbuilder.append ("0");            } stringbuilder.append (Integer.tohexstring (Chartindex)); if(I < Bytes.length-1) Stringbuilder.append (":");    } System.out.println (Stringbuilder.tostring ()); }

MessageDigest class provides encryption algorithms such as MD5 or Sha

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.