Using Java to generate MD5 encoding

Source: Internet
Author: User

Using Java to generate MD5 encoding MD5 that is message-digest algorithm 5 (Information-Digest algorithm 5), is a single hash algorithm for generating digital signatures, in 1991 by MIT Laboratory for Computer Scienc E (IT computer Science Lab) and Ronald L. Rivest of RSA Data Security Inc., developed through MD2, MD3 and MD4. The use of the MD5 algorithm does not require any copyright fees to be paid. Its role is to allow bulk information to be "compressed" into a confidential format before signing private keys with digital signatures (an arbitrary length "byte string" is transformed into a 128bit large integer by an irreversible string transformation algorithm, in other words, even if you see the source program and the algorithm description, It is also not possible to transform a MD5 value back into the original string, mathematically, because the original string has infinitely many, which is somewhat like a mathematical function with no inverse function. )

In Java, MD5 calculations have been defined in java.security.MessageDigest, so we simply call to get a MD5 128-bit integer. The 128-bit 16 bytes are then converted to a 16-binary representation.

The code is as follows:
Import Java.security.MessageDigest; public class MD5 {public static string getMD5 (string source) {string s = null;//character char hexdigits[used to convert bytes into a 16-binary notation] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; try {messagedigest MD = messagedigest.getinstance ("MD5"); Md.update (Source.getbytes ());//MD5 The result is a 128-bit long integer, byte t Mp[] = Md.digest (); The byte representation is 16 bytes//each byte is expressed in 16, the use of two characters,//So the expression of 16 in the system requires 32 characters char str[] = new char[16 * 2]; Represents the corresponding character position int k = 0 in the transformation result; Conversion for each byte of MD5//converted to 16 binary characters starting from first byte (int i = 0; i < i++) {//byte bytes byte BYTE0 = tmp;//bytes High 4 digits Convert,>>> to logical right, move symbol bit right together str[k++] = hexdigits[byte0 >>> 4 & 0xf]; 4-digit digital conversion str[k++] = hexdigits[byte0 & 0xf]; The result is converted to a string s = new string (str); catch (Exception e) {e.printstacktrace ();} return s; }//Method two public static string Getmd5second (String str) {StringBuilder SB = new StringBuilder (); try {messagedigest MD5 = MessaGedigest.getinstance ("MD5"); Md5.update (Str.getbytes ()); For (Byte b:md5.digest ()) {Sb.append (String.Format ("%02x", b));} catch (NoSuchAlgorithmException e) {e.printstacktrace ();} return sb.tostring (); }//MD5 code for "A" should be: 0cc175b9c0f1b6a831c399e269772661 public static void Main (String xu[]) {System.out.println (md5.ge TMD5 ("a")); } }

Related Article

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.