Java MD5 Algorithm

Source: Internet
Author: User

Reproduced to: http://www.cnblogs.com/youxin/archive/2011/10/16/2214434.html

Java MD5 Algorithm

MD5Message-Digest algorithm 5 (Information-Digest algorithm 5) is a single hash algorithm used to generate digital signatures.

MD5 AlgorithmThe function is to "compress" large-capacity information before using the digital signature software to sign a private key into a confidential format, converts a "Byte string" of any length into A-bit big integer using an irreversible String Conversion Algorithm.

Even if you see the source program and algorithm description, you cannot convert an MD5 value back to the original string. In terms of mathematical principle, it is because there are infinite numbers of original strings, this is a bit like a mathematical function without an inverse function.

JavaJava. Security. messagedigest has defined MD5
To obtain the 128-bit integer of MD5, and then convert the 16 bytes of the 128-bit value to a hexadecimal representation.

Package Edu. bjtu;/*** the MD5 algorithm is defined in rfc1321 * in RFC 1321, test suite is provided to verify whether your implementation is correct: * MD5 ("") = digest * MD5 ("A") = digest * MD5 ("ABC") = 900150983cd24fb0d6963f7d28e17f72 * MD5 ("Message Digest") = digest * MD5 ("abcdefghijklmnopqrstuvwxyz ") = c3fcd3d76192e4007dfb496cca67e13b ** @ author yilee ** incoming: A byte array * outgoing: MD5 result string of the byte array */public class MD5 {public static string getmd5 (byte [] source) {string S = NULL; char hexdigits [] = {// used to convert bytes to '0', '1', '2' in hexadecimal notation ', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C ', 'D', 'E', 'F'}; try {Java. security. messagedigest MD = Java. security. messagedigest. getinstance ("MD5"); Md. update (source); byte TMP [] = md. digest (); // the MD5 calculation result is a 128-bit long integer, // expressed in bytes as 16 bytes char STR [] = new char [16*2]; // if each byte is represented in hexadecimal notation, two characters are used. // Therefore, it must be 32 characters int K = 0 to indicate hexadecimal notation; // indicates the character position in the conversion result for (INT I = 0; I <16; I ++) {// starts from the first byte, convert each MD5 byte // to a hexadecimal character byte byte0 = TMP [I]; // take the I-byte STR [k ++] = hexdigits [byte0> 4 & 0xf]; // convert the numbers of the 4-bit high in the byte, // >>> shifts the right of the logic, and shifts the symbol bit together to the right of STR [k ++] = hexdigits [byte0 & 0xf]; // convert the numbers of the Middle and Lower 4 digits} s = new string (STR); // convert the result to a string} catch (exception e) {e. printstacktrace () ;}return s ;}}

The test is as follows:

Import Edu. bjtu. *; public class test {// calculate the MD5 code of "A", which should be: 0cc175b9c0f1b6a831c399e269772661 public static void main (string Xu []) {system. out. println (md5.getmd5 ("". getbytes ()));}}

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.