The example of this article tells the method of implementing MD5 encryption in Java. Share it for everyone for your reference. The specific implementation method is as follows:
Import Java.Security.MessageDigest; Import Java.Security.nosuchalgorithmexception; Public Class MD5Hashutil {Private Messagest MD = NU LL; 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', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'F'}; / ** * Constructor is Private so you must use the getinstance method * / Private md5hashutil () Throws noSuchalgEPTION {md = message Digest.getInstance ("MD5");} /** * this returnS the Singlet Instance * / Public Static MD5hashutil Getinstance () Throws NosuchalgorithMexception {if (md5 == NULL) {md5 = new md5hashutil ();} Return (md5);} publi C Static String HashCode (String Datatohash) Throws NoSuchalgorithMexception {Return Getinstance (). Hashdata ( Datatohash.getBytes ());} Public Static String HashCode (byte [] datatohash) Throws NosuchalgorithMexception {RetinStance (). HashData ash);} Public String Hashdata (byte [] Datatohash) {Return HexStringfrombytes )). TOLOWERCASE ();} Private Byte [] Calculatinghash (byte [] datatohash) {md.update (datahash, 0, datatohash.length); Return (md.digest ());} publ IC 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 String = "My name is zhangli"; System.out.println (System.out.println (HashCode string));}}
For example, the above code implements the MD5 encryption algorithm for the Java language, and the output is the ciphertext after encryption!
The encrypted ciphertext is usually preserved in the database.
At the same time, the MD5 encryption algorithm is irreversible, and the difficulty of cracking is very high. Although some people have solved the MD5, the hardware environment they use is not comparable to our ordinary computer. A very good female professor of Shan University also cracked MD5. But don't know much
It is hoped that this article is helpful to everyone's Java program design.