Java Encryption Algorithm-MD5

Source: Internet
Author: User

import java.security.MessageDigest; Public classMd5util {/** * MD5 encryption generates 32-bit MD5 code * @param string to be encrypted * @return return 32-bit MD5 code*/     Public Staticstring MD5 (String inStr) throws Exception {messagedigest MD5=NULL; Try{MD5= Messagedigest.getinstance ("MD5"); } Catch(Exception e) {System. out. println (E.tostring ());            E.printstacktrace (); return ""; }        byte[] ByteArray = Instr.getbytes ("UTF-8"); byte[] Md5bytes =md5.digest (ByteArray); StringBuffer Hexvalue=NewStringBuffer ();  for(inti =0; i < md5bytes.length; i++) {            intval = ((int) Md5bytes[i]) &0xFF; if(Val < -) {hexvalue.append ("0");        } hexvalue.append (Integer.tohexstring (Val)); }        returnhexvalue.tostring (); }    //encrypt two times at a time to decrypt     Public Staticstring convertMD5 (String inStr) {Char[] A =Instr.tochararray ();  for(inti =0; i < a.length; i++) {A[i]= (Char) (A[i] ^'T'); } String S=NewString (a); returns; }    //Reversible encryption Algorithm         Public Staticstring KL (String inStr) {//string s = new string (INSTR);            Char[] A =Instr.tochararray ();  for(inti =0; i < a.length; i++) {A[i]= (Char) (A[i] ^'T'); } String S=NewString (a); returns; }        //decryption after encryption         Public Staticstring JM (String inStr) {Char[] A =Instr.tochararray ();  for(inti =0; i < a.length; i++) {A[i]= (Char) (A[i] ^'T'); } String k=NewString (a); returnK; }    /** * Test main function * * @param args * @throws Exception*/    //passwords are encrypted first and then compared for equality     Public Static voidMain (String args[]) throws Exception {string str=NewString ("ABC"); System. out. println ("Original:"+str); System. out. println ("after MD5:"+MD5 (str)); System. out. println (convertMD5 (str)); System. out. println (convertMD5 (convertMD5 (str))); String s=NewString ("123123");//4297f44b13955235245b2497399d7a93System. out. println ("Original:"+str); System. out. println ("after MD5:"+MD5 (str)); System. out. println ("MD5 after re-encryption:"+KL (MD5 (str))); System. out. println ("after decrypting for MD5:"+JM (KL (MD5 (str))); }}

Java Encryption Algorithm-MD5

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.