[Android Pro] AES Encryption

Source: Internet
Author: User

Reference to:http://blog.csdn.net/wfung_kwok/article/details/7766427

PackageCom.secufity.aes;ImportJava.util.UUID;ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;ImportCom.secufity.db.Base64;/*** *@authorWfung_kwok **/PublicClassAES {static String e = "9238513401340235";//EncryptionPublicstatic string Encrypt (string src, string key)ThrowsException {if (key = =Null{System.out.print ("key is empty null");ReturnNull; }//Determine if key is 16-bitif (Key.length ()! = 16{System.out.print ("key length is not 16 bits"));ReturnNull; }Byte[] Raw =Key.getbytes (); Secretkeyspec Skeyspec =New Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");//"Algorithm/Mode/complement Method" 0102030405060708 Ivparameterspec IV =New Ivparameterspec (E.getbytes ());//Using CBC mode, a vector IV is required to increase the strength of the encryption algorithmCipher.init (Cipher.encrypt_mode, Skeyspec, iv);Byte[] Encrypted =Cipher.dofinal (Src.getbytes ());ReturnBase64.encodebytes (encrypted);//The BASE64 is used here to do transcoding functions, and can play 2 times encryption function.}//DecryptPublicstatic string Decrypt (string src, string key)ThrowsException {Try{//Determine if key is correctif (key = =Null{System.out.print ("key is empty null");ReturnNull; }//Determine if key is 16-bitif (Key.length ()! = 16{System.out.print ("key length is not 16 bits"));ReturnNull; }Byte[] Raw = key.getbytes ("ASCII"); Secretkeyspec Skeyspec =New Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding"); Ivparameterspec IV =NewIvparameterspec (E.getbytes ()); Cipher.init (Cipher.decrypt_mode, Skeyspec, iv);byte[] encrypted1 = Base64.decode (src);//First decrypt with Base64.Try{Byte[] Original =Cipher.dofinal (encrypted1); String originalstring =NewString (original);Returnoriginalstring; }Catch(Exception e) {System.out.println (e.tostring ());ReturnNull; } }Catch(Exception ex) {System.out.println (ex.tostring ());ReturnNull; } }PublicStaticvoid Main (string[] args)ThrowsException {String key = Uuid.randomuuid (). toString (). substring (0, 16); String src = "email: [Email protected]"; SYSTEM.OUT.PRINTLN (SRC);//Encryptionlong start = system.currenttimemillis (); String enstring = AES. Encrypt (SRC, key); System.out.println ("Encrypted string is:" + enstring); Long usetime = System.currenttimemillis ()- start; SYSTEM.OUT.PRINTLN ("Encryption Time:" + Usetime + "milliseconds"); // Decrypt start = System.currenttimemillis (); String destring = AES. Decrypt (enstring, key); System.out.println ("Decrypted string is:" + destring); usetime = System.currenttimemillis ()- start; System.out.println ("Decryption time:" + usetime + "millisecond");}}            

Console:

Email: [Email protected] encrypted string is: gkl3xeihndi1w8cn9fkbvem3fzo9kjsfvvqpwrkh0wi= encryption time: 234 milliseconds after the decrypted string is: email: [Email Protected] Decryption time: 0 Ms

[Android Pro] AES Encryption

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.