Java various encryption algorithms

Source: Internet
Author: User
Tags base64 hmac md5 encryption

1 ImportJava.math.BigInteger;2 Importjava.security.InvalidKeyException;3 Importjava.security.MessageDigest;4 Importjava.security.NoSuchAlgorithmException;5 ImportJavax.crypto.KeyGenerator;6 ImportJavax.crypto.Mac;7 ImportJavax.crypto.SecretKey;8 ImportJavax.crypto.spec.SecretKeySpec;9 Importorg.apache.commons.codec.binary.Base64;Ten  One  Public classMyencrypt { A  Public Static FinalString Key_sha = " sha"; -  Public Static FinalString KEY_MD5 = " md5"; -  Public Static FinalString Key_mac = " hmacmd5"; the  -  - //Sun does not recommend the use of their own base64, with Apache very good - /** + * BASE64 Decryption - */ +  Public Static byte[] decryptBASE64 (byte[] dest) { A if(Dest = =NULL) { at return NULL; - } - returnbase64.decodebase64 (dest); - } -  - /** in * BASE64 Encryption - */ to  Public Static byte[] encryptBASE64 (byte[] origin) { + if(Origin = =NULL) { - return NULL; the } * returnBase64.encodebase64 (origin); $ }Panax Notoginseng  - /** the * MD5 Encryption + *  A * @throwsnosuchalgorithmexception the */ +  Public Static byte[] EncryptMD5 (byte[] data) - throwsnosuchalgorithmexception { $ if(Data = =NULL) { $ return NULL; - } -MessageDigest MD5 =messagedigest.getinstance (KEY_MD5); the md5.update (data); - returnmd5.digest ();Wuyi } the  - /** Wu * SHA Encryption - *  About * @throwsnosuchalgorithmexception $ */ -  Public Static byte[] Encryptsha (byte[] data) - throwsnosuchalgorithmexception { - if(Data = =NULL) { A return NULL; + } theMessageDigest sha =messagedigest.getinstance (Key_sha); - sha.update (data); $ returnsha.digest (); the } the  the /** the * Initialize HMAC key - *  in * @throwsnosuchalgorithmexception the */ the  Public StaticString Initmackey ()throwsnosuchalgorithmexception { AboutKeygenerator Keygenerator =keygenerator.getinstance (KEY_MAC); theSecretkey Secretkey =Keygenerator.generatekey (); the return NewString (encryptBASE64 (secretkey.getencoded ())); the } +  - /** the * HMAC EncryptionBayi *  the * @throwsnosuchalgorithmexception the * @throwsinvalidkeyexception - */ -  Public Static byte[] Encrypthmac (byte[] data, String key) the throwsnosuchalgorithmexception, invalidkeyexception { theSecretkey Secretkey =NewSecretkeyspec (decryptBASE64 (Key.getbytes ()), the Key_mac); theMac Mac =mac.getinstance (Secretkey.getalgorithm ()); - Mac.init (secretkey); the returnmac.dofinal (data); the  the }94  the  Public Static voidMain (string[] args)throwsException { the //TODO auto-generated Method Stub theString data = " Simple Encryption ";98System.out.println (NewBigInteger (encryptBASE64 (Data.getbytes ())). ToString (16)); AboutSystem.out.println (NewBigInteger (encryptBASE64 (Data.getbytes ())). ToString (32)); -System.out.println (NewString (decryptBASE64 (encryptBASE64 (Data.getbytes () )));101 102System.out.println (NewBigInteger (EncryptMD5 (Data.getbytes ())). ToString ());103System.out.println (NewBigInteger (Encryptsha (Data.getbytes ())). ToString ());104  theSystem.out.println (NewBigInteger (Encrypthmac (Data.getbytes (), Initmackey ()). ToString ());106 }107 108}

Java various encryption algorithms

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.