To encrypt a database configuration file using Jasypt

Source: Internet
Author: User
Tags decrypt

Placeholders (placeholder) are often used in spring configuration files to load resource files, and often some resource files are stored in clear text, such as JDBC configuration information, which is not always displayed in clear form from a system security standpoint. Today contacted Jasypt, looked up some information to study the next. Jasypt is an open source project on SourceForge.net and is a Java library. Read more about Google yourself.

The first step is to join the Jasypt dependency. Here we use Maven.

<dependency> <groupId>org.jasypt</groupId> <artifactId>jasypt</artifactId> <vers Ion>1.8</version></dependency>

Encryption:

@Testpublic void Encrypt () {//create dongle standardpbestringencryptor encryptor = new Standardpbestringencryptor ();    Configuration environmentstringpbeconfig config = new Environmentstringpbeconfig ();    Config.setalgorithm ("Pbewithmd5anddes");//Cryptographic Algorithm Config.setpassword ("Fuyung");//System attribute value Encryptor.setconfig (config); String plaintext = "root"; PlainText String ciphertext = encryptor.encrypt (plaintext); Encryption System.out.println (plaintext + ":" + ciphertext);//Run Result: root:8y9g4kizquchb78mmjnkhw==}

Decrypt:

@Testpublic void Decrypt () {Standardpbestringencryptor encryptor = new Standardpbestringencryptor ();        Environmentstringpbeconfig config = new Environmentstringpbeconfig ();    Config.setalgorithm ("Pbewithmd5anddes");    Config.setpassword ("Fuyung");    Encryptor.setconfig (config); String ciphertext = "8y9g4kizquchb78mmjnkhw==";//ciphertext//decrypt string plaintext = Encryptor.decrypt (ciphertext); Decrypt System.out.println (ciphertext + ":" + plaintext);//Run Result: 8y9g4kizquchb78mmjnkhw==: Root}

Integrated with spring. Add the following code to the spring configuration file:

<bean id= "Propertyconfigure"       class= " Org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer ">    < constructor-arg ref= "Configurationencryptor"/>    <property name= " Systempropertiesmodename " value=" System_properties_mode_override "/>    < Property name= "Ignoreresourcenotfound"  value= "true"/>    <property  Name= "Locations" >        <list>             <value>classpath:jdbc.properties</value>         </list>    </property></bean>< Bean id= "Configurationencryptor"       class= " Org.jasypt.encryption.pbe.StandardPBEStringEncryptor ">    <property name= "config"  ref= "environmentvariablesconfiguration"/></bean><bean id= " Environmentvariablesconfiguration "      class=" Org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig ">    <property name = "algorithm"  value= "pbewithmd5anddes"/>    <property name= "password"   Value= "CLM"/></bean>

Check out the Jdbc.properties file:

Jdbc.username = ENC (KPKWMXAX2LMUQQKKPCULPTIMXZNTDXXW)
Jdbc.password = ENC (wg/u1ymqoznh4wyp7hpttjl0v1kgflic)

Remember to add the enc prefix to your ciphertext and wrap it up with (). Why write this, look at the source code will know:

This will automatically decrypt when the spring container is started and the encrypted value is read.



To encrypt a database configuration file using Jasypt

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.