Java MD5 algorithm returns numeric string

Source: Internet
Author: User
Tags array character set numeric md5 mixed return string stringbuffer
Algorithm

It is often asked why the MD5 algorithm has some program fragments that return full numeric results and some return a mixed string of numbers and letters.

In fact, two return results just because of the different display of encryption results, blog has a. NET implementation, this additional Java implementation for reference.

Java's standard class library is also powerful in theory, but because of the virtual machine/runtime implementation of too much, combined with version differences, some code in different environments run there will be strange abnormal results, especially in relation to the operation of the character set.

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 byte array to 16 string
* @param B-byte array
* @return 16 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 get a 16-binary representation of the result of the encryption, that is, the form of a mixed number of letters.
Resultsb.append (bytetonumstring (b[i)); Use this function to return the encrypted result of the 10-digit string, that is, full digital 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 = new String (origin);
MessageDigest MD = messagedigest.getinstance ("MD5");
Resultstring =
ByteArrayToString (Md.digest (Resultstring.getbytes ()));
}
catch (Exception ex) {

}
return resultstring;
}

public static void Main (string[] args) {
Md5encrypt md5encrypt = new Md5encrypt ();
System.out.println (Md5encode ("10000000"));
}
}




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.