The MD5 example uses Java. Security. messagedigest

Source: Internet
Author: User

MD5That is, message-Digest algorithm 5 (Information-SummaryAlgorithm5) Is used to generateSingle hash calculation of digital signaturesLaw, in 1991 by MIT laboratory for computer science (MIT Computer Science Laboratory) and RSA Data Security Inc (RSA Data Security Company) Ronald L. professor Rivest developed and developed through md2, md3 and md4. You do not have to pay any copyright fees for using the MD5 algorithm.

It is used to compress large-capacity information into a confidential format before using the digital signature software to sign a private key (Converts a "Byte string" of any length into A-bit big integer using an irreversible String Conversion Algorithm.In other words, even if you see the sourceProgramAnd algorithm description, it is impossible to convert an MD5 value back to the original string, in terms of mathematical principle, because the original string has an infinite number, this is a bit like a mathematical function without an inverse function .)

In Java, java. Security. messagedigest defines the MD5 calculation. Therefore, we only need to call it to obtain the 128-bit integer of MD5. Then, convert the 128 bits (16 bytes) to a hexadecimal representation.

CodeAs follows:

Package chapter1.lesson3;

Import java. Security. nosuchalgorithmexception;

/**
* the MD5 algorithm is defined in RFC 1321 in rfc1321, and test suite is provided to verify whether your implementation is correct: MD5 ("") =
* digest MD5 ("A") = digest
* MD5 ("ABC") = 900150983cd24fb0d6963f7d28e17f72 MD5 ("Message Digest ") =
* export MD5 ("abcdefghijklmnopqrstuvwxyz") =
* export
* @ author liango
* input parameters: output Parameters of a byte array: MD5 result string of the byte array
*/
public class MD5
{< br>/**
* method 1
* @ Param source
* @ return hexadecimal integer string format
*/
Public static string getmd5 (byte [] Source)
{< br> string S = NULL;
char hexdigits [] =
{// used to convert bytes into hexadecimal characters
'0', '1', '2 ', '3', '4', '5', '6', '7', '8', '9', 'A', 'B ',
'C', 'D', 'E', 'F'
};
try
{< br> JAVA. security. messagedigest MD = Java. security. messagedigest. getinstance ("MD5");
Md. update (source);
// the MD5 calculation result is a 128-Bit Length Integer.
byte TMP [] = md. digest ();

// 16 bytes
Char STR [] = new char [16*2]; // if each byte is in hexadecimal notation, two characters are used,
// Therefore, it must be 32 characters in hexadecimal notation.
Int K = 0; // indicates the character position in the conversion result.
For (INT I = 0; I <16; I ++)
{// Starting from the first byte
// Convert to hexadecimal character conversion
Byte byte0 = TMP [I]; // obtains the I-th byte.
STR [k ++] = hexdigits [byte0> 4 & 0xf]; // converts the numbers of the top 4 digits in a byte,
// >>> Shifts the right of the logic (that is, shifts the unsigned right), and shifts the signed bits together to the right.

// Converts the numbers of the Middle and Lower 4 bits in a byte.
STR [k ++] = hexdigits [byte0 & 0xf];

}
S = new string (STR); // convert the result to a string

}
Catch (exception E)
{
E. printstacktrace ();
}
Return S;
}

/**
* Method 2: string format output, 100 times slower
*
* @ Param bytes
* @ Return hexadecimal integer string format
*/
Private Static string getmd52 (byte [] source)
{
Try
{
Java. Security. messagedigest MD5 = java. Security. messagedigest
. Getinstance ("MD5 ");
Md5.update (source );

Stringbuilder sb = new stringbuilder ();
For (byte B: md5.digest ())
{
// Two digits are used to indicate that the value is less than zero.
SB. append (string. Format ("% 02x", B ));
}

Return sb. tostring ();
}
Catch (nosuchalgorithmexception E)
{
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return NULL;
}

Public static voidMain(String Xu [])
{// Calculate the MD5 code of "A", which should be: 0cc175b9c0f1b6a831c399e269772661
System. Out. println (md5.getmd5 ("A". getbytes ()));

System. Out. println (md5.getmd52 ("A". getbytes ()));
}

}

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.