Java implementation MD5 encryption example _java

Source: Internet
Author: User
Tags md5 md5 encryption sha1 sha1 encryption

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.