Java-aes Encryption and decryption

Source: Internet
Author: User

 PackageCom.jy.demo.web;ImportJava.security.Provider;ImportJava.security.SecureRandom;Importjava.security.Security;Importjava.util.Arrays;ImportJavax.crypto.Cipher;ImportJavax.crypto.KeyGenerator;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;Importorg.apache.commons.codec.binary.Base64;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory; Public classaesutils{ Public Static FinalString IV = "2015030120123456"; Private Static FinalLogger Logger = Loggerfactory.getlogger (aesutils.class);  Public Static byte[] Aesencrypt (String content, String pkey) {Try{secretkeyspec key=NewSecretkeyspec (Pkey.getbytes (), "AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); Ivparameterspec IV=NewIvparameterspec ("2015030120123456". GetBytes ()); Cipher.init (1, key, IV); returnCipher.dofinal (Content.getbytes ("UTF-8")); }    Catch(Exception e) {e.printstacktrace (); }    return NULL; }  Private Staticsecretkey GenerateKey (String secretkey)throwsException {Provider P= Security.getprovider ("SUN"); SecureRandom SecureRandom= Securerandom.getinstance ("Sha1prng", p);    Securerandom.setseed (Secretkey.getbytes ()); Keygenerator kg= Keygenerator.getinstance ("AES");    Kg.init (SecureRandom); returnKg.generatekey (); }   Public Staticstring Aesencryptstr (string content, String pkey) {byte[] Aesencrypt =aesencrypt (content, Pkey); Logger.debug ("Aesencrypt String:" +arrays.tostring (Aesencrypt)); String Base64encodestr=base64.encodebase64string (Aesencrypt); Logger.debug ("Aesencrypt base64encodestr:" +base64encodestr); returnBase64encodestr; }   Public Staticstring Aesdecodestr (string content, String pkey)throwsException {byte[] Base64decodestr =base64.decodebase64 (content); Logger.debug ("Base64decodestr String:" +arrays.tostring (BASE64DECODESTR)); byte[] Aesdecode =Aesdecode (Base64decodestr, Pkey); if(Aesdecode = =NULL) {      return NULL; The String result=NewString (Aesdecode, "UTF-8"); Logger.debug ("Aesdecode Result:" +result); returnresult; }   Public Static byte[] Aesdecode (byte[] content, String pkey) {    Try{secretkeyspec key=NewSecretkeyspec (Pkey.getbytes (), "AES"); Ivparameterspec IV=NewIvparameterspec ("2015030120123456". GetBytes ("UTF-8"))); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); Cipher.init (2, key, IV); returncipher.dofinal (content); }    Catch(Exception e) {e.printstacktrace (); }    return NULL; }     Public Static voidMain (string[] args)throwsException {System.out.println (Aesutils.aesencryptstr ("123456", "abcdnnnnnn123456"));//encryption based on keySystem.out.println (Aesutils.aesdecodestr ("i/uruhvllepo+nwthbu7aa==", "abcdnnnnnn123456"));//decryption based on encrypted password and encryption key}  }

Java-aes Encryption and 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.