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