MD5 encryption Base64 Conversion method based on Java language

Source: Internet
Author: User
Tags base64 md5 encryption

Provides 16-bit and 32-bit methods based on MD5 encryption

1 Importjava.io.IOException;2 ImportJava.math.BigInteger;3 Importjava.security.MessageDigest;4 Importjava.security.NoSuchAlgorithmException;5 ImportSun.misc.BASE64Decoder;6 ImportSun.misc.BASE64Encoder;7 /**8 * <p> Title: Coding Tool Class </p>9 * <p> function: Encode and convert data </p>Ten * Zhao Li One  */ A  Public classEncodeutil - { -      Public Static voidMain (string[] args)throwsException the     { -System.out.println (Md5encrypt16 ("string required for MD5 encryption")); -     } -  +     /** - * MD5 Encryption 16-bit +      * @paramencryptstr to encrypt data A      * @returnreturns 16-bit encryption results at * Zhaoli -      */ -      Public Staticstring md5encrypt16 (string encryptstr) -     { -         returnMd5encrypt32 (ENCRYPTSTR). SUBSTRING (8, 24); -     } in  -     /** to * MD5 Encryption 32-bit +      * @paramencryptstr to encrypt data -      * @return32-bit encryption results the * Zhaoli *      */ $      Public Staticstring Md5encrypt32 (string encryptstr)Panax Notoginseng     { - messagedigest MD5; the         Try +         { AMD5 = messagedigest.getinstance ("MD5"); the             byte[] Md5bytes =md5.digest (Encryptstr.getbytes ()); +StringBuffer Hexvalue =NewStringBuffer (); -              for(inti = 0; i < md5bytes.length; i++) $             { $                 intval = (Md5bytes[i]) & 0xFF; -                 if(Val < 16) -                 { theHexvalue.append ("0"); -                 }Wuyi Hexvalue.append (Integer.tohexstring (Val)); the             } -             returnhexvalue.tostring (). toLowerCase (); Wu}Catch(Exception e) -         { About             Throw NewRuntimeException (e); $         } -     } -  -     /**  A * Combining Base64 for MD5 encryption +      * @parammsg to encrypt string the      * @returnget MD5 and switch to base64 -      * @throwsException $      */ the      Public StaticString Md5encryptbase64 (String msg)throwsException the     { the         returnmsg = =NULL?NULL: Base64Encode (MD5 (msg)); the     } -  in     /**  the * Convert byte[] to a variety of binary strings the      * @parambytes byte[] About      * @paramradix can be converted into a range, from Character.min_radix to Character.max_radix, out of range and into 10 binary the      * @returnconverted string the      */ the      Public StaticString Binary (byte[] Bytes,intRadix) +     { -         return NewBigInteger (1, bytes). toString (radix);//The 1 here represents a positive number. the     }Bayi  the     /**  the * Base encode -      * @parambytes to encode byte[] -      * @returnencoded Base Code the      */ the      Public StaticString Base64Encode (byte[] bytes) the     { the         return NewBase64encoder (). Encode (bytes); -     } the  the     /**  the * Base Decode94      * @paramBase64code Base code to decode the      * @returnDecoded byte[] the      * @throwsException the      */98      Public Static byte[] Base64decode (String base64code) About     { -         Try101         {102             returnBase64code = =NULL?NULL:NewBase64decoder (). Decodebuffer (Base64code);103}Catch(IOException e)104         { the             Throw NewRuntimeException ("Error content", e);106         }107     }108 109     /**  the * Get the MD5 value of byte[]111      * @parambytes byte[] the      * @returnMD5113      * @throwsException the      */ the      Public Static byte[] MD5 (byte[] bytes) the     {117 MessageDigest MD;118         Try119         { -MD = Messagedigest.getinstance ("MD5");121}Catch(nosuchalgorithmexception e)122         {123             Throw NewRuntimeException ("Error content", e);124         } the md.update (bytes);126         returnmd.digest ();127     } - 129     /**  the * Get string MD5 value131      * @parammsg the      * @returnMD5133      * @throwsException134      */135      Public Static byte[] MD5 (String msg)136     {137         returnmsg = =NULL?NULL: MD5 (Msg.getbytes ());138     }139}

MD5 encryption Base64 Conversion method based on Java language

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.