Package and other;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
/*
* MD5 algorithm
*/
public class MD5 {
Global array
Private final static string[] strdigits = {"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "B", "C", "D", "E", "F"};
Public MD5 () {
}
Returns the form of a number followed by a string
private static String bytetoarraystring (byte bbyte) {
int iRet = Bbyte;
System.out.println ("iret=" +iret);
if (IRet < 0) {
IRet + = 256;
}
int iD1 = IRET/16;
int iD2 = iRet% 16;
return STRDIGITS[ID1] + STRDIGITS[ID2];
}
Return form is only numeric
private static String Bytetonum (byte bbyte) {
int iRet = Bbyte;
System.out.println ("iret1=" + iRet);
if (IRet < 0) {
IRet + = 256;
}
Return string.valueof (IRet);
}
Convert byte array to 16 binary string
private static String bytetostring (byte[] bbyte) {
StringBuffer sbuffer = new StringBuffer ();
for (int i = 0; i < bbyte.length; i++) {
Sbuffer.append (bytetoarraystring (bbyte[i));
}
return sbuffer.tostring ();
}
public static string Getmd5code (String strobj) {
String resultstring = null;
try {
resultstring = new String (strobj);
MessageDigest MD = messagedigest.getinstance ("MD5");
Md.digest () The function returns a byte array that holds the result of the hash value
resultstring = bytetostring (Md.digest (Strobj.getbytes ()));
} catch (NoSuchAlgorithmException ex) {
Ex.printstacktrace ();
}
return resultstring;
}
public static void Main (string[] args) {
MD5 getMD5 = new MD5 ();
System.out.println (Getmd5.getmd5code ("000000"));
}
}
Java Implementation MD5 algorithm