This article uses the Java self-contained messagedigest to implement the MD5 encryption algorithm for the text, the specific code is as follows:
/** * @Description: Convert the string into MD5 * * Package cn.yicha.novel.util;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException; public class ParseMD5 {/** * @param str * @return * @Description: 32-bit lowercase MD5/public static Stri
ng parsestrtomd5l32 (String str) {string restr = null;
try {messagedigest MD5 = messagedigest.getinstance ("MD5");
byte[] bytes = Md5.digest (Str.getbytes ());
StringBuffer StringBuffer = new StringBuffer ();
for (byte b:bytes) {int bt = b&0xff;
if (BT <) {Stringbuffer.append (0);
} stringbuffer.append (Integer.tohexstring (BT));
} restr = Stringbuffer.tostring ();
catch (NoSuchAlgorithmException e) {e.printstacktrace ();
return restr; /** * @param str * @return * @Description: 32-bit UPPERCASE MD5 */public static String parsestrtomd5u32 ( String str) {StRing RESTR = Parsestrtomd5l32 (str);
if (restr!= null) {restr = Restr.touppercase ();
return restr; /** * @param str * @return * @Description: 16-bit lowercase MD5 */public static String Parsestrtomd5u16 (S
Tring str) {String restr = parsestrtomd5l32 (str);
if (restr!= null) {restr = Restr.touppercase (). SUBSTRING (8, 24);
return restr; /** * @param str * @return * @Description: 16-bit UPPERCASE MD5 */public static String Parsestrtomd5l16 (S
Tring str) {String restr = parsestrtomd5l32 (str);
if (restr!= null) {restr = restr.substring (8, 24);
return restr; }
}
The second situation: in the Java software development process, it is inevitable to encrypt some data, so Java provides the messagedigest to implement the text encryption algorithm, the following is a text to encrypt the MD5 encryption tool Class code example:
MD5 encryption algorithm in Java full version:
Package net.yuerwan.commons.util;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
Import Org.apache.commons.lang.StringUtils; public class Md5util {/** * 1.32-bit lowercase MD5 encryption for text * @param plaintext text to be encrypted * @return encrypted content/public static String Textto Md5l32 (String plaintext) {string result = NULL;//First determine if NULL if (Stringutils.isblank (plaintext)) {return null;} try{//First
The MessageDigest MD = messagedigest.getinstance ("MD5") was instantiated and initialized;
Gets an operating system default byte-encoding format for the byte array byte[] Btinput = Plaintext.getbytes ();
The resulting byte array is processed md.update (btinput);
Hash calculation and return the result byte[] Btresult = Md.digest ();
The length of the data obtained after the hash calculation stringbuffer SB = new StringBuffer (); for (byte b:btresult) {int bt = b&0xff; if (bt<16) {sb.append (0);} sb.append (Integer.tohexstring (BT));
Sb.tostring ();
}catch (nosuchalgorithmexception e) {e.printstacktrace (); /** * 2.32-bit MD5 uppercase encryption for text * @param plaintext the text to be encrypted * @return encrypted content/public static string texttomd5u32 (String plainText) {if (Stringutils.isblank (plaintext)) {return null;}
String result = Texttomd5l32 (plaintext);
return Result.touppercase ();
}
The third case: MD5 Cryptographic algorithm Java implementation
package 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 () {}//return form is numeric with 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];
The//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 resultstring = Bytetostring (Md.digest (Strobj.getbytes ()) that holds the result of the hash value;
catch (NoSuchAlgorithmException ex) {ex.printstacktrace ();
return resultstring;
public static void Main (string[] args) {MD5 getMD5 = new MD5 ();
System.out.println (Getmd5.getmd5code ("000000"));
}
}