Due to the project needs, so recently read the next AES encryption decryption, words not much to say, directly see the implementation:
Encryption:
Encrypt private static byte[] Encrypt (string content, string password) {try {keygenerator KGen = keygenerator.getinstance ("AE S "); Kgen.init (+, New SecureRandom (Password.getbytes ())); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES"); Cipher Cipher = cipher.getinstance ("AES");//Create cipher byte[] bytecontent = content.getbytes ("Utf-8"); Cipher.init ( Cipher.encrypt_mode, key);//Initialize byte[] result = cipher.dofinal (bytecontent); return result; Encryption} catch (NoSuchAlgorithmException e) {e.printstacktrace ();} catch (Nosuchpaddingexception e) {e.printstacktrace ();} catch (InvalidKeyException e) {e.printstacktrace ();} catch (Unsupportedencodingexception e) {e.printstacktrace ();} catch (Illegalblocksizeexception e) {e.printstacktrace ();} catch (Badpaddingexception e) {e.printstacktrace ();} return null;} /** convert binary to 16 binary encryption * @param BUF * @return */public static String Parsebyte2hexstr (byte buf[]) {StrinGbuffer sb = new StringBuffer (); for (int i = 0; i < buf.length; i++) {String hex = integer.tohexstring (Buf[i] & 0xFF); if (hex.length () = = 1) {hex = ' 0 ' + hex; } sb.append (Hex.touppercase ()); } return sb.tostring (); }
Decrypt:
Decrypt private static byte[] Decrypt (byte[] account, String password) {try {keygenerator KGen = Keygenerator.getinstance ("AES"); Kgen.init (+, New SecureRandom (Password.getbytes ())); Secretkey Secretkey = Kgen.generatekey (); byte[] Encodeformat = secretkey.getencoded (); Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES"); Cipher Cipher = cipher.getinstance ("AES");//Create cipher Cipher.init (Cipher.decrypt_mode, key);//Initialize Byte[] result = cipher.dofinal (account); return result; Encryption} catch (NoSuchAlgorithmException e) {e.printstacktrace (); } catch (Nosuchpaddingexception e) {e.printstacktrace (); } catch (InvalidKeyException e) {e.printstacktrace (); } catch (Illegalblocksizeexception e) {E.printstaCktrace (); } catch (Badpaddingexception e) {e.printstacktrace (); } return null; }/** convert 16 binary to binary--Decrypt * @param hexstr * @return * * public static byte[] Parsehexstr2byte (String hexstr) { if (Hexstr.length () < 1) return null; Byte[] result = new Byte[hexstr.length ()/2]; for (int i = 0;i< hexstr.length ()/2; i++) {int high = Integer.parseint (Hexstr.substring (i*2, i*2+1), 16); int low = Integer.parseint (Hexstr.substring (i*2+1, i*2+2), 16); Result[i] = (byte) (high * + low); } return result; }
Client testing:
String content = "CDJZBDD"; String addpwd = "123456"; Convention public encryption Password//encryption System.out.println ("Before encryption:" + content); byte[] Enaccount = Encrypt (content, addpwd); String encryptresultstr = Parsebyte2hexstr (enaccount); SYSTEM.OUT.PRINTLN ("After encryption:" + encryptresultstr); Decrypt byte[] Decryptfrom = Parsehexstr2byte (ENCRYPTRESULTSTR); byte[] Decryptresult = Decrypt (decryptfrom,addpwd); System.out.println ("decrypted:" + new String (Decryptresult));
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
AES Encryption decryption