Java implementation DES encryption and decryption, MD5 encryption

Source: Internet
Author: User
Tags md5 encryption

Most of the time to the secret to be persistent encryption, at this time the encryption using MD5. When symmetric encryption is used, the Des method is used.

1 Importjava.io.IOException;2 Importjava.security.MessageDigest;3 ImportJava.security.SecureRandom;4 5 ImportJavax.crypto.Cipher;6 ImportJavax.crypto.SecretKey;7 Importjavax.crypto.SecretKeyFactory;8 ImportJavax.crypto.spec.DESKeySpec;9 Ten ImportSun.misc.BASE64Decoder; One ImportSun.misc.BASE64Encoder; A  - /** - * Key Tool Class (contains DES encryption and MD5 encryption) the  * @authorMingge -  * -  */ -  Public classKeysutil { +  -         Private Final StaticString des = "des"; +          A         Private Final StaticString MD5 = "MD5"; at          -         Private Final StaticString key= "opeddsaead323353484591dadbc382a18340bf83414536"; -       -         /** - * MD5 Encryption algorithm -          * @paramData in          * @return -          */ to          Public Staticstring Md5encrypt (String data) { +String resultstring =NULL; -             Try { theResultstring =NewString (data); *MessageDigest MD =messagedigest.getinstance (MD5); $Resultstring =byte2hexstring (Md.digest (Resultstring.getbytes ()));Panax Notoginseng}Catch(Exception ex) { -             } the             returnresultstring; +         } A          the  +         Private  StaticString byte2hexstring (byte[] bytes) { -StringBuffer BF =NewStringBuffer (Bytes.length * 2); $              for(inti = 0; i < bytes.length; i++) { $                 if((Bytes[i] & 0xff) < 0x10) { -Bf.append ("T0"); -                 } theBf.append (Long.tostring (bytes[i) & 0xFF, 16)); -             }Wuyi             returnbf.tostring (); the         } -          Wu         /** - * Description is encrypted based on key value About          * @paramData $          * @paramkey Encryption key byte array -          * @return -          * @throwsException -          */ A          Public StaticString Desencrypt (String data, string key)throwsException { +             if(key==NULL) { thekey=KEY; -             } $             byte[] bt =Encrypt (Data.getbytes (), key.getbytes ()); theString STRs =NewBase64encoder (). Encode (BT); the             returnSTRs; the         } the       -         /** in * Description is decrypted based on key value the          * @paramData the          * @paramkey Encryption key byte array About          * @return the          * @throwsIOException the          * @throwsException the          */ +          Public StaticString Desdecrypt (String data, string key)throwsIOException, - Exception { the             if(Data = =NULL){Bayi                 return NULL; the             } the             if(key==NULL) { -key=KEY; -             } theBase64decoder decoder =NewBase64decoder (); the             byte[] buf =decoder.decodebuffer (data); the             byte[] bt =Decrypt (Buf,key.getbytes ()); the             return NewString (BT); -         } the       the         /** the * Description is encrypted based on key value94          * @paramData the          * @paramkey Encryption key byte array the          * @return the          * @throwsException98          */ About         Private Static byte[] Encrypt (byte[] Data,byte[] key)throwsException { -             //generate a trustworthy random number source101SecureRandom sr =Newsecurerandom ();102             //Create a Deskeyspec object from the original key data103Deskeyspec DKs =NewDeskeyspec (key);104             //Create a key factory and use it to convert Deskeyspec to Secretkey objects theSecretkeyfactory keyfactory =secretkeyfactory.getinstance (DES);106Secretkey SecureKey =Keyfactory.generatesecret (DKS);107             //The cipher object actually completes the cryptographic operation108Cipher Cipher =cipher.getinstance (DES);109             //initializing a Cipher object with a key the Cipher.init (Cipher.encrypt_mode, SecureKey, SR);111             returncipher.dofinal (data); the         }113           the           the         /** the * Description is decrypted based on key value117          * @paramData118          * @paramkey Encryption key byte array119          * @return -          * @throwsException121          */122         Private Static byte[] Decrypt (byte[] Data,byte[] key)throwsException {123             //generate a trustworthy random number source124SecureRandom sr =Newsecurerandom (); the             //Create a Deskeyspec object from the original key data126Deskeyspec DKs =NewDeskeyspec (key);127             //Create a key factory and use it to convert Deskeyspec to Secretkey objects -Secretkeyfactory keyfactory =secretkeyfactory.getinstance (DES);129Secretkey SecureKey =Keyfactory.generatesecret (DKS); the             //The cipher object actually completes the decryption operation131Cipher Cipher =cipher.getinstance (DES); the             //initializing a Cipher object with a key133 Cipher.init (Cipher.decrypt_mode, SecureKey, SR);134             returncipher.dofinal (data);135         }136}

I was drunk at the end.

Java implementation DES encryption and decryption, MD5 encryption

Related Article

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.