Spring's encryption and decryption of property files

Source: Internet
Author: User
Tags base64

Typically used to configure sensitive information such as passwords

Decryption/Encryption Tool class

 PackageCom.baobaotao.placeholder;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;ImportJavax.crypto.Cipher;ImportJavax.crypto.KeyGenerator;ImportJava.security.Key;ImportJava.security.SecureRandom;/*** Encryption/Decryption Tool class * Created by Sherry on 15-6-28.*/ Public classDesutils {/*Specify the encryption/decryption key used*/    Private Statickey key; /*Note: The key must be a multiple of 8*/    Private StaticString key_str = "MYKEYKKK"; Static {        Try {            /*prevent random generation of keys under Linux*/SecureRandom SecureRandom= Securerandom.getinstance ("Sha1prng");            Securerandom.setseed (Key_str.getbytes ()); Keygenerator Keygenerator= Keygenerator.getinstance ("DES"); //Keygenerator.init (New SecureRandom (Key_str.getbytes ()));Keygenerator.init (SecureRandom); Key=Keygenerator.generatekey (); Keygenerator=NULL; }Catch(Exception e) {Throw NewRuntimeException (e); }    }    /*des encryption of strings, return BASE64 encoded encrypted strings*/     Public Staticstring getencryptstring (String string) {Base64encoder Base64encoder=NewBase64encoder (); Try {            byte[] strbytes = String.getbytes ("UTF-8"); Cipher Cipher= Cipher.getinstance ("DES");            Cipher.init (Cipher.encrypt_mode,key); byte[] Encryptstrbytes =cipher.dofinal (strbytes); returnBase64encoder.encode (encryptstrbytes); } Catch(Exception e) {Throw NewRuntimeException (e); }    }    /*decrypts a BASE64 encoded encrypted string, returning the decrypted string*/     Public Staticstring getdecryptstring (String str) {Base64decoder Base64decoder=NewBase64decoder (); Try {            byte[] Strbytes =Base64decoder.decodebuffer (str); Cipher Cipher= Cipher.getinstance ("DES");            Cipher.init (Cipher.decrypt_mode,key); byte[] Decryptstrbytes =cipher.dofinal (strbytes); return NewString (Decryptstrbytes, "UTF-8"); } Catch(Exception e) {Throw NewRuntimeException (e); }    }     Public Static voidMain (string[] args) {args=Newstring[]{"Root", "123456"}; String[] Result=NewString[2]; if(args = =NULL|| Args.length<1) {System.out.println ("Please enter a string to encrypt, split with a space"); }Else {            inti = 0;  for(String Arg:args) {System.out.println (arg+ ":" +getencryptstring (ARG)); Result[i++] =getencryptstring (ARG); }        }         for(String temp:result) {System.out.println (temp+":"+getdecryptstring (temp)); }    }}

Property Editor

 PackageCom.baobaotao.placeholder;ImportOrg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;/*** Created by Sherry on 15-6-28.*/ Public classEncryptpropertyplaceholderconfigurerextendsPropertyplaceholderconfigurer {PrivateString[] Encryptpropnames = {"Username_mysql", "Password"}; @Overrideprotectedstring Convertproperty (String propertyname, String propertyvalue) {if(Isencryptprop (PropertyName)) {String Decryptvalue=desutils.getdecryptstring (PropertyValue); System.out.println ("Decryption Result:" +decryptvalue); returnDecryptvalue; }Else{System.out.println ("No decryption required:" +PropertyValue); returnPropertyValue; }    }    /*determine if the property needs to be encrypted*/     Public BooleanIsencryptprop (String PropertyName) { for(String encryptpropertyname:encryptpropnames) {if(Encryptpropertyname.equals (PropertyName)) {return true; }        }        return false; }}

Configuration uses

    <Beanclass= "Com.baobaotao.placeholder.EncryptPropertyPlaceholderConfigurer"p:location= "Classpath:jdbc.properties"p:fileencoding= "UTF-8"/>    <!--Configure the data source -    <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"Destroy-method= "Close"P:driverclassname= "${driverclassname}"P:url= "${url}"P:username= "${username_mysql}"P:password= "${password}"/>

Spring's encryption and decryption of property files

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.