A simple Java-Class Original Implementation of MD5

Source: Internet
Author: User
A concise Java class that implements MD5
Original jaie (Participation score: 311, expert score: 1500) published: Update: Version: 1.0 read:3266Times
Keywords: MD5

Because the message digest is unique and irreversible, it is a simple and common encryption method. For example, you can use MD5 to encrypt the user password in your application.

        
         
  1. PackageTest;
  2. ImportJava. Security.Messagedigest;
  3. /**
  4. * <P> title: </P>
  5. * <P> Description: </P>
  6. * <P> copyright: Copyright (c) 2003 </P>
  7. * <P> company: </P>
  8. * @ Author unascribed
  9. * @ Version 1.0
  10. */
  11. Public Class Stringutil{
  12. Private Final Static String[] Hexdigits = {
  13. "0", "1", "2", "3", "4", "5", "6", "7 ",
  14. "8", "9", "A", "B", "C", "D", "E", "F "};
  15. /**
  16. * Convert the byte array to a hexadecimal string.
  17. * @ Param B byte array
  18. * @ Return hexadecimal string
  19. */
  20. Public Static StringBytearraytohexstring (Byte[] B ){
  21. StringbufferResultsb =New Stringbuffer();
  22. For(IntI = 0; I <B.Length; I ++ ){
  23. Resultsb. append (bytetohexstring (B [I]);
  24. }
  25. ReturnResultsb. tostring ();
  26. }
  27. Private Static StringBytetohexstring (ByteB ){
  28. IntN = B;
  29. If(N <0)
  30. N = 256 + N;
  31. IntD1 = N/16;
  32. IntD2 = n % 16;
  33. ReturnHexdigits [D1] + hexdigits [D2];
  34. }
  35. Public Static StringMd5encode (StringOrigin ){
  36. StringResultstring =Null;
  37. Try{
  38. Resultstring =New String(Origin );
  39. MessagedigestMD =Messagedigest. Getinstance ("MD5 ");
  40. Resultstring = bytearraytohexstring (Md. Digest (resultstring. getbytes ()));
  41. }
  42. Catch(ExceptionEx ){
  43. }
  44. ReturnResultstring;
  45. }
  46. Public Static VoidMain (String[] ARGs ){
  47. System. Err. println (md5encode (""));
  48. }
  49. }

In RFC 1321, test suite is provided to verify whether your implementation is correct:

MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
MD5 ("A") = 0cc175b9c0f1b6a831c399e269772661
MD5 ("ABC") = 900150983cd24fb0d6963f7d28e17f72
MD5 ("Message Digest") = f96b697d7cb7938d525a2f31aaf161d0
MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b

reference: Java security handbook Jamie Jaworski

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.