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"));
}
}