Java MD5 Encryption and decryption

Source: Internet
Author: User
Tags md5 md5 encryption stringbuffer

Simple Demo:java code   import  java.security.*;   Import  java.security.spec.*;       Class  MD5_test {    public   final   static &NBSP;STRING&NBSP;MD5 (String  s)  {     char  hexDigits[] = {  ' 0 ',  ' 1 ',  ' 2 ',  ' 3 ',  ' 4 ',  ' 5 ',  ' 6 ',  ' 7 ',  ' 8 ',  ' 9 ',        ' a ', &NB Sp ' B ',  ' C ',  ' d ',  ' e ',  ' F '  };     try  {     & Nbsp;byte [] strtemp = s.getbytes ();      messagedigest mdtemp  = messagedigest.getinstance ("MD5");      mdtemp.update (strTemp);       byte [] md = mdtemp.digest ();      int  j  = md.length;      char  str[] =  new   Char [j *  2];      int  k =  0;   &nbsp ;   for   (int  i =  0;  i < j; i++)  {        byte  byte0 = md[i];       str[k++] =  Hexdigits[byte0 >>> 4  &  0xf];       str[k++] &NBSP;=&NBSP;HEXDIGITS[BYTE0&NBSP;&AMP;&NBSP;0XF];      }       return   New  string (str);     } catch   (exception e)  {       return   NULL;     }    }    & nbsp  public   static   void  main (String[] args)  {     // md5_ Test aa = new md5_test ();      system.out.print (MD5_test. MD5 ("B"));    }  }  

Import java.security.*;
Import java.security.spec.*;

Class Md5_test {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[] strtemp = S.getbytes ();
   MessageDigest mdtemp = messagedigest.getinstance ("MD5");
   Mdtemp.update (strtemp);
   byte[] MD = Mdtemp.digest ();
   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) {return
   null;
  }
 }

 public static void Main (string[] args) {
  //md5_test AA = new Md5_test ();
  System.out.print (md5_test. MD5 ("B"));
 }



Java Code   Import  java.security.MessageDigest;         public   class  MD5andKL {   &NBSP;//&NBSP;MD5 Overweight. 32-bit     public   static  string md5 (STRING&NBSP;INSTR)  {      Messagedigest md5 = null;     try  {      md5  = messagedigest.getinstance ("MD5");     } catch   (Exception e)  {      system.out.println (e.tostring ());       E.printstacktrace ();      return   "";     }      char [] chararray = instr.tochararray ();     byte [] byteArray  =  new   byte [chararray.length];        for   (int  i =& nbsp 0.  i < chararray.length; i++)       byteArray[i] =  (byte)  charArray[i];        byte [] md5bytes = md5.digest (ByteArray);         Stringbuffer hexvalue = new  stringbuffer ();        for   ( int  i =  0;  i < md5bytes.length; i++)  {       int  val =  ((int)  md5bytes[i])  &  0xff;      if & nbsp (val < )        hexvalue.append ("0");       hexvalue.append (Integer.tohexstring (val));     }         Return  hexvalue.tostring ();    }       //  reversible encryption Algorithm     public   Static &NBSP;STRING&NBSP;KL (STRING&NBSP;INSTR)  {     // string s = new string (INSTR);      char [] a = instr.tochararray ();     for   (int  i =  0  i < a.length; i++)  {      a[i]  =  (char)   (a[i] ^  ' t ');     }     string s  = new  string (a);     return  s;    }        //  encryption after decryption     public   static &NBSP;STRING&NBSP;JM (STRING&NBSP;INSTR)  {      char [] a = instr.tochararray ();     for   (int  i =  0  i < a.length; i++)  {      a[i]  =  (char)   (a[i] ^  ' t ');     }     string k  = new  string (a);   &Nbsp; return  k;    }        //  Test main function     public   static   void  main (string args[])  {     string s  = new  string ("a");     system.out.println ("Original:"  + s);     system.out.println (after "MD5:"  + md5 (s));     system.out.println (" MD5 after encryption: "&NBSP;+&NBSP;KL (MD5 (s));     system.out.println (" Decrypted to MD5: "&NBSP;+&NBSP;JM ( MD5 (s)));    }  }  

Import Java.security.MessageDigest; public class Md5andkl {//MD5 overweight.
  32-bit public static string MD5 (String inStr) {messagedigest MD5 = NULL;
  try {MD5 = messagedigest.getinstance ("MD5");
   catch (Exception e) {System.out.println (e.tostring ());
   E.printstacktrace ();
  Return "";
  } char[] Chararray = Instr.tochararray ();

  byte[] ByteArray = new Byte[chararray.length];

  for (int i = 0; i < chararray.length i++) bytearray[i] = (byte) chararray[i];

  byte[] md5bytes = Md5.digest (ByteArray);

  StringBuffer hexvalue = new StringBuffer ();
   for (int i = 0; i < md5bytes.length i++) {int val = ((int) md5bytes[i]) & 0xFF;
   if (Val <) hexvalue.append ("0");
  Hexvalue.append (Integer.tohexstring (Val));
 return hexvalue.tostring ();
  }//Reversible cryptographic algorithm public static string KL (String inStr) {//String s = new String (INSTR);
  Char[] A = Instr.tochararray ();
  for (int i = 0; i < a.length i++) {A[i] = (char) (a[i] ^ ' t '); } STring s = new String (a);
 return s;
  Decrypt public static String JM (String inStr) {char[] a = Instr.tochararray () after encryption;
  for (int i = 0; i < a.length i++) {A[i] = (char) (a[i] ^ ' t ');
  String k = new String (a);
 return k;
  //test main function public static void main (string args[]) {string s = new String ("a");
  System.out.println ("Original:" + s);
  System.out.println ("MD5 after:" + MD5 (s));
  System.out.println ("MD5 after encryption:" + KL (MD5 (s)));
 System.out.println ("Decrypted as MD5:" + JM (KL (MD5 (s)));

 }
}

 

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.