Copy the code code as follows:
/**
* Implement MD5 encryption
*
*/
public class MD5 {
/**
* Get the encrypted string
* @param input
* @return
*/
public static String stringMD5(String pw) {
try {
// Get an MD5 converter (if you want the SHA1 parameter to be changed to "SHA1")
MessageDigest messageDigest =MessageDigest.getInstance("MD5");
//Convert the input string into a byte array
byte[] inputByteArray = pw.getBytes();
// inputByteArray is the byte array converted from the input string
messageDigest.update(inputByteArray);
// Convert and return the result, which is also a byte array, containing 16 elements
byte[] resultByteArray = messageDigest.digest();
// Convert character array to string and return
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
public static String byteArrayToHex(byte[] byteArray) {
// First initialize a character array to store each hexadecimal character
char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A', 'B','C','D','E','F' };
// new a character array, this is used to form the result string (explanation: a byte is an eight-bit binary, that is, 2 hexadecimal characters (2 to the 8th power is equal to 16 to the 2nd power))
char[] resultCharArray =new char[byteArray.length * 2];
// Traverse the byte array, convert it into characters through bit operations (bit operations are highly efficient) and put them into the character array.
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b& 0xf];
}
// Combine character arrays into strings and return
return new String(resultCharArray);
}
}
PS: Regarding encryption technology, this site also provides the following encryption tools for your reference:
MD5 online encryption tool: http://tools.VeVB.COm/password/CreateMD5Password
Escape encryption/decryption tool: http://tools.VeVB.COm/password/escapepwd
Online SHA1 encryption tool: http://tools.VeVB.COm/password/sha1encode
Short link (short URL) online generation tool: http://tools.VeVB.COm/password/dwzcreate
Short link (short URL) online restoration tool: http://tools.VeVB.COm/password/unshorturl
Strong password generator: http://tools.VeVB.COm/password/CreateStrongPassword