Java encryption AES algorithm and application in spring

Source: Internet
Author: User
Tags base64

Just put the code straight.

1.    Aesutil Encryption and Decryption tool class import Java.security.Key;    Import Java.security.SecureRandom;    Import java.util.Base64;    Import Javax.crypto.Cipher;    Import Javax.crypto.KeyGenerator; /** * @description: AES Encryption Tool class * @author: Maojialong * @date: November 7, 2017 morning 10:11:02*/     Public classAesutils {//Instantiate a key        Private Statickey key; //Original Key        Private StaticString Key_str ="my-springmvc-2017-11-07"; //Coding        Private StaticString CharsetName ="UTF-8"; //Cryptographic key Algorithm        Private StaticString Key_algorithm ="AES"; //Encryption-Decryption algorithm/working mode/Fill Method        Private StaticString Cipher_algorithm ="aes/ecb/pkcs5padding"; /** * Initialize key*/        Static {            Try{keygenerator KGen=keygenerator.getinstance (Key_algorithm); SecureRandom Random= Securerandom.getinstance ("sha1prng");                Random.setseed (Key_str.getbytes ()); Kgen.init ( -, Random); Key=Kgen.generatekey (); KGen=NULL; } Catch(Exception e) {Throw NewRuntimeException (e); }        }                /** * @description: AES symmetric encrypted string and converted to ASCII with Base64 from JDK * @author: Administrator * @date: November 2017 7th Morning 9:37:48 * @param str * @return*/         Public Staticstring getencryptstring (String str) {Try {                byte[] bytes =str.getbytes (CharsetName); Cipher Cipher=cipher.getinstance (Cipher_algorithm);                Cipher.init (Cipher.encrypt_mode, key); byte[] dofinal =cipher.dofinal (bytes); returnBase64.getencoder (). encodetostring (dofinal); } Catch(Exception e) {Throw NewRuntimeException (e); }        }             /** * @description: Decrypt AES Encrypted String * @author: Maojialong * @date: November 7, 2017 10:14:00 * @param str * @return*/         Public Staticstring getdecryptstring (String str) {Try {                byte[] bytes =Base64.getdecoder (). decode (str); Cipher Cipher=cipher.getinstance (Cipher_algorithm);                Cipher.init (Cipher.decrypt_mode, key); byte[] dofinal =cipher.dofinal (bytes); return NewString (dofinal, CharsetName); } Catch(Exception e) {Throw NewRuntimeException (e); }        }    }2. Custom profile parsing class import java.util.List;    Import Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /** * @description: Custom AES Decryption * @author: Maojialong * @date: November 7, 2017 morning 10:26:50*/     Public classEncodeaesplaceholderconfigurer extends Propertyplaceholderconfigurer {PrivateList<string>encodeproperties; /** * @description: Override Convertproperty method, encodeproperties to determine if AES is encrypted * @author: Maojialong *  @date: November 7, 2017 morning 10:27:24 * @param propertyname * @param propertyvalue * @return * @see Org.springframework.beans.factory.config.propertyresourceconfigurer#convertproperty (Java.lang.String, java.lang.String) * TODO*/@Overrideprotectedstring Convertproperty (String propertyname, String propertyvalue) {if(Encodeproperties! =NULL&&Encodeproperties.contains (PropertyName)) {PropertyValue=aesutils.getdecryptstring (PropertyValue); }            returnSuper.convertproperty (PropertyName, PropertyValue); }         PublicList<string>getencodeproperties () {returnencodeproperties; }         Public voidSetencodeproperties (list<string>encodeproperties) {             This. encodeproperties =encodeproperties; }    }3To load a configuration file in a configuration file<!--custom AES Encryption decryption--<bean id="Propertyconfigurer" class="util. Encodeaesplaceholderconfigurer"> <!--profile Address--<property name="Locations"> <list> <value>classpath:conf/jdbc.properties</value> <v Alue>classpath:redis.properties</value> </list> </property> <!--configuration requires a decryption of the Set items--<property name="encodeproperties"> <array> <value>jdbc.url</value> <value>jdbc.username    </value> <value>jdbc.password</value> </array> </property> </bean>

Java encryption AES algorithm and application in spring

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.