Java implementation of MD5 encryption method _java

Source: Internet
Author: User

This article describes the Java implementation of MD5 encryption method. Share to everyone for your reference. The implementation method is as follows:

Import Java.security.MessageDigest; 
Import java.security.NoSuchAlgorithmException; 
public class Md5hashutil {private MessageDigest MD = NULL; 
private static md5hashutil MD5 = NULL; 

  private static final char[] Hexchars ={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; /** * constructor is private, so your must use the GetInstance method */Private Md5hashutil () throws Nosuchalgorit 
  hmexception {MD = messagedigest.getinstance ("MD5"); }/** * This returns the singleton instance/public static Md5hashutil getinstance () throws Nosuchalgorithmexc 
   Eption {if (MD5 = = NULL) {MD5 = new md5hashutil (); 
 return (MD5); The public static string Hashcode (String datatohash) throws nosuchalgorithmexception{return getinstance (). Hashdata (data 
 Tohash.getbytes ()); public static String hashcode (byte[] datatohash) throws nosuchalgorithmexception{return getinstance (). Hashdata (data 
 Tohash); } public String HashdaTa (byte[] datatohash) {return hexstringfrombytes ((Calculatehash (Datatohash))). toLowerCase (); 
   Private byte[] Calculatehash (byte[] datatohash) {md.update (Datatohash, 0, datatohash.length); 
Return (Md.digest ()); 
  public string hexstringfrombytes (byte[] b) {string hex = ' "; 
  int MSB; 
  int LSB = 0; 
  int i; 
   MSB maps to IDX 0 for (i = 0; i < b.length; i++) {MSB = ((int) b[i] & 0x000000ff)/16; 
    LSB = ((int) b[i] & 0x000000ff)% 16; 
  Hex = hex + HEXCHARS[MSB] + HEXCHARS[LSB]; 
return (hex); 
   public static void Main (String args[]) throws nosuchalgorithmexception {string = ' My name is Zhangli '; 
   System.out.println (string); 
System.out.println (Hashcode (string)); 

 } 
}

As on the code for the Java language to implement MD5 encryption algorithm, the output is encrypted after the ciphertext!
The encrypted ciphertext is usually persisted in the database, if it is required to compare only the cipher text after their MD5 encryption.

At the same time, MD5 encryption algorithm is irreversible, the difficulty of cracking is very high, although someone cracked the MD5, but they use the hardware environment is not our ordinary computer compared to the mountain of a very cow female professor also cracked the MD5, but not understand

I hope this article will help you with your Java programming.

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.