Este artigo usa o Messagedigest, que vem com Java, para implementar o algoritmo de criptografia MD5 para texto. O código específico é o seguinte:
/ ** *@Descrição: Converta String para md5 */ pacote cn.yicha.novel.util; importar java.Security.Messagedigest; importar java.security.nosuchalgorithMexception; classe pública parsemd5 { / ** * @param str * @return * @Description: minúsculo md5 * / public static de 32 bits parsesttomd5l32 (string str) {string rest = null; tente {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)); } rest = stringBuffer.toString (); } catch (nosuchalgorithMexception e) {e.printStackTrace (); } retornar restos; } / ** * @param str * @return * @Description: 32 bits md5 * / public static string parsestrtomd5u32 (string str) {string rest = parsesttomd5l32 (str); if (restp! = null) {restr.toupPercase (); } retornar restos; } / ** * @param str * @return * @description: minúsculo md5 * / public static string parsestrtomd5u16 (string str) {string rest = parsestrtomd5l32 (str); if (rest! = null) {rest = rest.ToupPercase (). Substring (8, 24); } retornar restos; am if (restp! = null) {rest = rest.substring (8, 24); } retornar restos; }}A segunda situação: durante o processo de desenvolvimento de software Java, alguns dados serão inevitavelmente criptografados. Portanto, o Java fornece a própria implementação Messagedigest do algoritmo de criptografia para texto. A seguir, é apresentado um exemplo do código da ferramenta de criptografia MD5 que criptografa o texto:
A versão completa do algoritmo de criptografia MD5 em Java :
pacote net.yuerwan.commons.util; importar java.security.messagedigest; importar java.security.nosuchalgorithMexception; importar org.apache.commons.lang.stringUtils; classe pública md5util {*** 1. Conteúdo criptografado*/public static string textTomd5l32 (string linetext) {string result = null; // primeiro determina se está vazio se (stringutils.isblank (lineartxt)) {return null;} tente {// primeiro instantiado e inicializar mensagens) formato do sistema operacional byte [] btinput = plainText.getBytes (); // Arquive a matriz de bytes obtida md.update (btInput); // arquive o cálculo de hash e retorno o resultado do string (] stringBoftB; bTResult) {int bt = b & 0xff; if (bt <16) {sb.append (0);} sb.append (integer.tohexstring (bt));} resultado = sb.ToString ();} catch (nosuchalGorithMexception {E.PRArNCTrack; 32 bits md5 em maiúsculas* @param texto simples texto a ser criptografado* @return Conteúdo criptografado*/public static string textTomd5u32 (string planyText) {if (stringUtils.isblank (simples) {return null;} resultado = textomd5l32 (simples);O terceiro caso: implementação Java do algoritmo de criptografia 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 () {} // retorna o formulário como número e string string estática privada bytetoarraystring (byte bbyte) {int iret = bbyte; // System.out.println ("Iret ="+IRET); if (IRET <0) {Iret += 256; } int id1 = IRET / 16; int id2 = IRET % 16; retornar strdigits [id1] + strdigits [id2]; } // O formulário de retorno é apenas um dígito de string estática privada bytetonum (byte bbyte) {int iret = bbyte; System.out.println ("Iret1 =" + IRET); if (IRET <0) {Iret += 256; } return string.valueof (IRET); } // Converta a matriz de byte em uma string hexadecimal string privada 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 resgationtring = null; tente {ResultsTring = new String (strobj); Messagedigest md = Messagedigest.getInstance ("md5"); // md.digest () O valor de retorno desta função é a matriz de bytes que armazena a troca de resultados de hash = bytetoString (md.digest (strobj.getbytes ())); } catch (nosuchalgorithMexception ex) {ex.printStackTrace (); } retornar a troca de resultados; } public static void main (string [] args) {md5 getMd5 = novo md5 (); System.out.println (getMd5.getMd5Code ("000000")); }}