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