AES Full name Advanced encryptionstandard, high encryption algorithm, more secure, can replace DES.
Aes:
PackageCom.blog.d201706.encrypt;ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.SecretKeySpec;ImportJava.security.Key; Public classAes {/*** Add decryption key*/ Private FinalKey KeySpec; /*** Constructor Function *@paramKey*/ PublicAes (String key) {KeySpec=NewSecretkeyspec (Key.getbytes (), "AES"); } /*** Encryption *@paramSTR *@return */ Publicstring Encryt (String str) {//initializes the Cipher object according to the key, Encrypt_mode represents the encryption mode Try{Cipher C= Cipher.getinstance ("AES"); C.init (Cipher.encrypt_mode, KeySpec); byte[] src =str.getbytes (); //encryption, results saved into Cipherbyte byte[] Cipherbyte =c.dofinal (SRC); returnparsebyte2hexstr (Cipherbyte); } Catch(Exception e) {e.printstacktrace (); } return NULL; } /*** Decryption *@paramDecodestr *@return */ Publicstring Decrypt (string decodestr) {//initializes the Cipher object according to the key, Decrypt_mode represents the encryption mode Try { if(NULL= = Decodestr)return NULL; byte[] Buff =Parsehexstr2byte (DECODESTR); Cipher C; C= Cipher.getinstance ("AES"); C.init (Cipher.decrypt_mode, KeySpec); byte[] Cipherbyte =c.dofinal (Buff); return NewString (Cipherbyte); } Catch(Exception e) {e.printstacktrace (); } return NULL; } /*** Convert binary to 16 binary * *@paramBUF *@return */ Public 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 (); } /*** Convert 16 binary to binary * *@paramHexstr *@return */ Public 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; }}View Code
Mainaes:
PackageCom.blog.d201706.encrypt; Public classMainaes { Public Static voidMain (string[] args)throwsException {AES AES=NewAes ("1234567890123456"); String msg= "Test text: Today Week 5" +System.currenttimemillis (); String encontent=Aes.encryt (msg); String decontent=Aes.decrypt (encontent); System.out.println ("Clear Text is:" +msg); System.out.println ("After encryption:" +encontent); System.out.println ("After decryption:" +decontent); }}View Code
AES encryption and decryption Java application