Java MD5 algorithm returns numeric strings

Source: Internet
Author: User
Tags numeric md5 stringbuffer

It is often asked why some program fragments of the MD5 algorithm return full numeric results, while some return mixed strings of numbers and letters.
In fact, the two returned results are only displayed in different formats due to the encryption results. The. Net implementation is already available in the Blog, and the JAVA implementation is attached here for your reference.
JAVA's standard library has powerful functions theoretically. However, due to too many implementations of virtual machines/runtime, and version differences, some code may run in different environments with strange exception results, especially for operations involving character sets.
Package com. bee. framework. common;
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;
Related Article

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.