Java implementation AES Encryption

Source: Internet
Author: User

Encryption and decryption is for binary operation, because the code of the results or parameters of the 16 binary, 2 conversion, to avoid the output decrypted results garbled.

//AES Encryption     Public Staticstring Encryptaccesstoken (string content, string password) {Try{keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init (128,NewSecureRandom (Password.getbytes ())); Secretkey Secretkey=Kgen.generatekey (); byte[] Encodeformat =secretkey.getencoded (); Secretkeyspec Key=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES");//Create a password device            byte[] bytecontent = Content.getbytes ("Utf-8"); Cipher.init (Cipher.encrypt_mode, key);//Initialize            byte[] result =cipher.dofinal (bytecontent); returnPARSEBYTE2HEXSTR (result);//Encrypt}Catch(Exception e) {e.printstacktrace (); }        return NULL; }    //AES Decryption     Public Staticstring Decrypt (string content, string password) {Try {            byte[] Contentbytes =parsehexstr2byte (content); Keygenerator KGen= Keygenerator.getinstance ("AES"); Kgen.init (128,NewSecureRandom (Password.getbytes ())); Secretkey Secretkey=Kgen.generatekey (); byte[] Encodeformat =secretkey.getencoded (); Secretkeyspec Key=NewSecretkeyspec (Encodeformat, "AES"); Cipher Cipher= Cipher.getinstance ("AES");//Create a password deviceCipher.init (Cipher.decrypt_mode, key);//Initialize            byte[] result =cipher.dofinal (contentbytes); return NewString (result);//Encrypt}Catch(Exception e) {e.printstacktrace (); }        return NULL; }    //2-in-turn 16-in system    Private StaticString Parsebyte2hexstr (bytebuf[]) {StringBuffer SB=NewStringBuffer ();  for(inti = 0; i < buf.length; i++) {String hex= Integer.tohexstring (Buf[i] & 0xFF); if(hex.length () = = 1) {hex= ' 0 ' +Hex;        } sb.append (Hex.touppercase ()); }        returnsb.tostring (); }    //16-in-Turn 2-in system    Private Static byte[] Parsehexstr2byte (String hexstr) {if(Hexstr.length () < 1)            return NULL; byte[] result =New byte[Hexstr.length ()/2];  for(inti = 0; I < Hexstr.length ()/2; i++) {            intHigh = Integer.parseint (Hexstr.substring (i * 2, I * 2 + 1), 16); intLow = Integer.parseint (Hexstr.substring (i * 2 + 1, I * 2 + 2),                    16); Result[i]= (byte) (High * 16 +Low ); }        returnresult; }

Java implementation 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.