Java MD5 encryption Implementation code _java

Source: Internet
Author: User
Tags md5 md5 digest md5 encryption stringbuffer

The following diagram is the first to understand the MD5 encryption implementation, as follows

Package COM.PB;
Import java.io.UnsupportedEncodingException;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;

Import Java.util.Scanner; * * Verify MD5 * 1. Initialize MessageDigest Information Digest Object * 2. Pass in the string update summary information that needs to be computed * 3. Calculation Information Summary * 4. Convert byte[] to 32-bit with 16-digit String */public class MD5 {* * * generate MD5 have incoming parameter string */public void generateMD5 (string input) {try {//1. Initialize messagedigest information pick
       To object, and specifies that the MD5 can be messagedigest md=messagedigest.getinstance ("MD5") without any case. 2. Pass in the string update summary information that needs to be computed, incoming as byte array byte[],///convert string to byte array use GetBytes () method to complete//specify when its character encoding is Utf-8 md.update (input.get
       Bytes ("Utf-8"));
       3. Calculation Information Digest Digest () method//Return value is byte array byte [] hashcode=md.digest ();
      4. Convert byte[] to a 32-bit 16-string/Declaration StringBuffer object to store the final value StringBuffer sb=new stringbuffer (); Traversal byte array for (byte B:hashcode) {//array content converted to 16, Sb.append (Character.fordigit (B>>4&0XF, 16))
        ; 32-bit 16-Sb.ap for 2 timesPend (Character.fordigit (B&0XF, 16));
    SYSTEM.OUT.PRINTLN ("The result of the encryption is:" +sb.tostring ());
    catch (NoSuchAlgorithmException e) {//TODO auto-generated catch block E.printstacktrace ();
    catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();
    The public static void main (string[] args) {//Declares the object of the cryptographic MD5 class MD5 md5=new MD5 ();
    Use Scanner to enter a character Scanner scanner=new Scanner (system.in);
    System.out.println ("Please enter the content to be encrypted:");
    String input = Scanner.nextline ();

  Call encryption Method MD5.GENERATEMD5 (input);
 }
  
}

Results:

Please enter the content to be encrypted:
Learn the MD5 encryption process
The result of encryption is: b826cdac46f01dcc8ccc60a76cebf858

Second paragraph code:

Package test.md5;

Import Java.security.MessageDigest; public class Md5util {public final static string MD5 (string s) {char hexdigits[]={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ',    

    ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
      try {byte[] btinput = S.getbytes ();
      Obtain the MessageDigest object messagedigest mdinst = messagedigest.getinstance ("MD5") of MD5 Digest algorithm;
      Use the specified Byte update digest mdinst.update (Btinput);
      Get ciphertext byte[] MD = Mdinst.digest ();
      Converts cipher text into a 16-binary string form int j = md.length;
      Char str[] = new CHAR[J * 2];
      int k = 0;
        for (int i = 0; i < J; i++) {byte byte0 = md[i];
        str[k++] = hexdigits[byte0 >>> 4 & 0xf];
      str[k++] = hexdigits[byte0 & 0xf];
    return new String (str);
      catch (Exception e) {e.printstacktrace ();
    return null;
    } public static void Main (string[] args) {System.out.println (Md5util.md5 ("20121221")); System.out.println (MD5UTIL.MD5 ("Encryption"));
 }
}

Third paragraph code: MD5 encryption algorithm Java implementation

package other;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException; * * * MD5 Algorithm */public class MD5 {//global array private final static string[] strdigits = {"0", "1", "2", "3", "4", "5"

  , "6", "7", "8", "9", "a", "B", "C", "D", "E", "F"};
    Public MD5 () {}//return form is numeric with string private static string bytetoarraystring (byte bbyte) {int iRet = Bbyte;
    System.out.println ("iret=" +iret);
    if (IRet < 0) {IRet = 256;
    int iD1 = IRET/16;
    int iD2 = iRet% 16;
  return STRDIGITS[ID1] + STRDIGITS[ID2];
    The//Return form is only numeric private static String Bytetonum (byte bbyte) {int iRet = Bbyte;
    System.out.println ("iret1=" + iRet);
    if (IRet < 0) {IRet = 256;
  Return string.valueof (IRet);
    //Convert byte array to 16 binary string private static String bytetostring (byte[] bbyte) {StringBuffer sbuffer = new StringBuffer (); for (int i = 0; i < bbyte.length i++) {sbuffer.append (bytetoarraystRing (Bbyte[i]));
  return sbuffer.tostring ();
    public static string Getmd5code (String strobj) {string resultstring = null;
      try {resultstring = new String (strobj);
      MessageDigest MD = messagedigest.getinstance ("MD5");
    Md.digest () The function returns a byte array resultstring = Bytetostring (Md.digest (Strobj.getbytes ()) that holds the result of the hash value;
    catch (NoSuchAlgorithmException ex) {ex.printstacktrace ();
  return resultstring;
    public static void Main (string[] args) {MD5 getMD5 = new MD5 ();
  System.out.println (Getmd5.getmd5code ("000000"));
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.