AES Encryption decryption

Source: Internet
Author: User

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

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.