Código um:
/** * Implementar criptografia MD5* */public class MD5 { /** * Obter a string criptografada* @param input * @return */ public static String stringMD5(String pw) { try { // Obter um conversor MD5 (se você deseja que o parâmetro SHA1 seja alterado para "SHA1") MessageDigest messageDigest =MessageDigest.getInstance("MD5"); // Converta a string de entrada em uma matriz de bytes byte[] inputByteArray = pw.getBytes(); // inputByteArray é o array de bytes obtido pela conversão da string de entrada messageDigest.update(inputByteArray); // Converte e retorna o resultado, que também é um array de bytes, contendo 16 elementos byte[] resultByteArray = messageDigest; .digest (); // Converte array de caracteres em string e retorna return byteArrayToHex(resultByteArray } catch (NoSuchAlgorithmException); e) { return null; } } public static String byteArrayToHex(byte[] byteArray) { // Primeiro inicialize uma matriz de caracteres para armazenar cada caractere hexadecimal char[] hexDigits = {'0','1', '2',' 3','4','5','6','7','8','9', 'A','B','C','D','E','F' }; // new um array de caracteres, isso é usado para formar a string de resultado (explicação: um byte tem oito bits binários, ou seja, 2 caracteres hexadecimais (2 elevado à 8ª potência é igual a 16 elevado à 2ª potência)) char[] resultCharArray =new char[byteArray.length * 2]; Percorra a matriz de bytes, converta-a em caracteres por meio de operações de bits (as operações de bits são altamente eficientes) e coloque-os na matriz de caracteres int index = 0 for (byte b : byteArray) { resultCharArray[index++] = hexDigits[b>>>; > 4 & 0xf]; resultCharArray[index++] = hexDigits[b& 0xf] } // Combine a matriz de caracteres em uma string e retorne return new String(resultCharArray); }}
Método dois:
package other;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * Algoritmo MD5*/public class MD5 { // Matriz global private final static String[] strDigits = { "0", "1", " 2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; public MD5() { } // O formulário de retorno é um número e uma string private static String byteToArrayString(byte bByte) { int iRet = bByte; println( "iRet="+iRet); if (iRet < 0) { iRet += 256; iD2 = iRet % 16; return strDigits[iD1] + strDigits[iD2] } // O formulário de retorno é apenas números private static String byteToNum(byte bByte) { int iRet = bByte; + iRet); if (iRet < 0) { iRet += 256; String.valueOf(iRet); // Converte a matriz de bytes em uma string hexadecimal private static String byteToString(byte[] bByte) { StringBuffer sBuffer = new StringBuffer(); i++) { sBuffer.append(byteToArrayString(bByte[i]) } return sBuffer.toString() }; public static String GetMD5Code(String strObj) { String resultString = null; try { resultString = new String(strObj); armazenamento A matriz de bytes do valor hash resultString = byteToString(md.digest(strObj.getBytes())); (NoSuchAlgorithmException ex) { ex.printStackTrace(); return resultString; } public static void main(String[] args) { MD5 getMD5 = new MD5(); ;}}