Copy 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 a MD5 converter (if you want to change the SHA1 parameter to "SHA1")
MessageDigest messagedigest =messagedigest.getinstance ("MD5");
Converts the string entered into a byte array
byte[] Inputbytearray = Pw.getbytes ();
Inputbytearray is a byte array of input string conversions
Messagedigest.update (Inputbytearray);
Converts and returns the result, which is also a byte array containing 16 elements
byte[] Resultbytearray = Messagedigest.digest ();
Converts a character array to a string return
Return Bytearraytohex (Resultbytearray);
catch (NoSuchAlgorithmException e) {
return null;
}
}
public static String Bytearraytohex (byte[] bytearray) {
Initializes an array of characters to hold each of the 16 binary characters
Char[] hexdigits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
New a character array, which is used to compose the result string (explanation: A byte is a eight-bit binary, that is, a 2-bit hexadecimal character (2 of 8 times equals 16 2))
Char[] Resultchararray =new char[bytearray.length * 2];
Iterates through byte arrays, converting them into characters in a character array by bitwise operations (high efficiency of bit operations)
int index = 0;
for (byte B:bytearray) {
resultchararray[index++] = hexdigits[b>>> 4 & 0xf];
resultchararray[index++] = hexdigits[b& 0xf];
}
Character array group synthetic string return
return new String (Resultchararray);
}
}
PS: About encryption technology, the site also provides the following encryption tools for your reference to use:
MD5 Online encryption tool: Http://tools.jb51.net/password/CreateMD5Password
Escape Encryption/decryption tool: http://tools.jb51.net/password/escapepwd
Online SHA1 encryption tool: Http://tools.jb51.net/password/sha1encode
short link (short URL) online generation tool: http://tools.jb51.net/password/dwzcreate
short link (short URL) online Restore tool: Http://tools.jb51.net/password/unshorturl
High Intensity password generator: http://tools.jb51.net/password/CreateStrongPassword