Este artículo utiliza MessageDigest, que viene con Java, para implementar el algoritmo de cifrado MD5 para el texto. El código específico es el siguiente:
/ ** *@Descripción: Convertir la cadena a md5 */ paquete cn.yicha.novel.util; import java.security.MessageGest; import java.security.nosuchalgorithmexception; clase pública parsemd5 { / ** * @param str * @return * @Description: 32 bit en minúsculas md5 * / public static string parsestrtomd5l32 (string str) {string restr = null; Pruebe {MessageDigest MD5 = MessageDigest.getInStance ("MD5"); byte [] bytes = md5.digest (str.getbytes ()); StringBuffer StringBuffer = new StringBuffer (); para (byte b: bytes) {int bt = b & 0xff; if (bt <16) {stringBuffer.append (0); } StringBuffer.append (Integer.ToHexString (bt)); } restr = stringBuffer.ToString (); } Catch (nosuchalgorithMexception e) {E.PrintStackTrace (); } returnrrt; } / ** * @param str * @return * @Description: 32 bit upcase md5 * / public static string parsestrtomd5u32 (string str) {string reest = parsestrtomd5l32 (str); if (rests! = null) {restr = reest.ToUpperCase (); } returnrrt; } / ** * @param str * @return * @Description: Md5 * / public static string parsestrtomd5u16 (String Str) de 16 bits if (rests! = null) {reestr = restring.ToUpperCase (). Substring (8, 24); } returnrrt; } / ** * @param str * @return * @Description: 16 bit upcase md5 * / public static string parsestrtomd5l16 (string str) {string restrest = parsestrtomd5l32 (str); if (rests! = null) {reestr = restr.substring (8, 24); } returnrrt; }}La segunda situación: durante el proceso de desarrollo de software Java, algunos datos inevitablemente se encriptarán. Por lo tanto, Java proporciona la propia implementación del MessageGest del algoritmo de cifrado para el texto. El siguiente es un ejemplo del código de la herramienta de cifrado MD5 que encripta el texto:
La versión completa del algoritmo de cifrado MD5 en Java :
paquete net.yuerwan.commons.util; import java.security.messagedigest; import java.security.nosuchalgorithmexxception; import org.apache.commons.lang.stringutils; clase pública md5util {/*** 1. 32 bit md5 cifrado para el text* @param que texting textetettexted to será el texttextettextettexted to será el textura de text* @param que se texté de textura de textos que se presentan textéxicos para ser el textura de text* @return contenido cifrado*/public static string textTomD5L32 (string asignext) {string dult = null; // Determine primero si está vacío if (stringUtils.isblank (Ensintext)) {return null;} intente {// primero instantarse e inicialize Messagedigest md = messagedigest.getinstance ("Md5"); // Formato de codificación del sistema de sistema operativo byte [] btinput = asignext.getBytes (); // archivo la matriz de byte obtenida md.update (btinput); // archivar el cálculo hash y devolver el resultado byte [] btresult = md.digest () ;// ajuste de la longitud de los datos obtenidos después del cálculo de hastio sbuffer sb = newfer (neowFer (((); : btresult) {int bt = b & 0xff; if (bt <16) {sb.append (0);} sb.append (integer.toHexString (bt));} result = sb.ToString ();} catch (nosuchalGorithMexception e) {E.PrintstActace ();} Result;} El texto de 32 bits MD5 uppercase* @param entregaxt a estar encriptado* @return contenido cifrado*/public static string textTomd5u32 (string Ensintext) {if (stringUtils.isblank (eMaintext)) {return null;} string result = textTomD5l32 (refertext); return result.toupcase ();};}El tercer caso: Java Implementación del algoritmo de cifrado MD5
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 como número y cadena static 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]; } // El formulario de retorno es solo una cadena estática privada de dígito bytetonum (byte bbyte) {int iret = bbyte; System.out.println ("iret1 =" + iret); if (iret <0) {iret += 256; } return string.ValueOf (iret); } // Convierta la matriz de byte en una cadena hexadecimal de cadena estática privada 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; intente {resultados treTring = new String (stroBJ); MessageDigest MD = MessageGest.getInstance ("MD5"); // md.digest () El valor de retorno de esta función es la matriz de bytes que almacena el hash resultados thetetostring (md.digest (strobj.getbytes ())); } catch (nosuchalgorithMexception ex) {ex.printstackTrace (); } return resultados ttring; } public static void main (string [] args) {md5 getMd5 = new Md5 (); System.out.println (getMd5.getMd5Code ("000000")); }}