This article describes the Java implementation of MD5 encryption method. Share to everyone for your reference. The implementation method is as follows:
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
public class Md5hashutil {private MessageDigest MD = NULL;
private static md5hashutil MD5 = NULL;
private static final char[] Hexchars ={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; /** * constructor is private, so your must use the GetInstance method */Private Md5hashutil () throws Nosuchalgorit
hmexception {MD = messagedigest.getinstance ("MD5"); }/** * This returns the singleton instance/public static Md5hashutil getinstance () throws Nosuchalgorithmexc
Eption {if (MD5 = = NULL) {MD5 = new md5hashutil ();
return (MD5); The public static string Hashcode (String datatohash) throws nosuchalgorithmexception{return getinstance (). Hashdata (data
Tohash.getbytes ()); public static String hashcode (byte[] datatohash) throws nosuchalgorithmexception{return getinstance (). Hashdata (data
Tohash); } public String HashdaTa (byte[] datatohash) {return hexstringfrombytes ((Calculatehash (Datatohash))). toLowerCase ();
Private byte[] Calculatehash (byte[] datatohash) {md.update (Datatohash, 0, datatohash.length);
Return (Md.digest ());
public string hexstringfrombytes (byte[] b) {string hex = ' ";
int MSB;
int LSB = 0;
int i;
MSB maps to IDX 0 for (i = 0; i < b.length; i++) {MSB = ((int) b[i] & 0x000000ff)/16;
LSB = ((int) b[i] & 0x000000ff)% 16;
Hex = hex + HEXCHARS[MSB] + HEXCHARS[LSB];
return (hex);
public static void Main (String args[]) throws nosuchalgorithmexception {string = ' My name is Zhangli ';
System.out.println (string);
System.out.println (Hashcode (string));
}
}
As on the code for the Java language to implement MD5 encryption algorithm, the output is encrypted after the ciphertext!
The encrypted ciphertext is usually persisted in the database, if it is required to compare only the cipher text after their MD5 encryption.
At the same time, MD5 encryption algorithm is irreversible, the difficulty of cracking is very high, although someone cracked the MD5, but they use the hardware environment is not our ordinary computer compared to the mountain of a very cow female professor also cracked the MD5, but not understand
I hope this article will help you with your Java programming.