Encrypting columns in a database using jasypt

來源:互聯網
上載者:User

Created by  Ken Stevens. Last edited by  Steve Shaw, 1 year ago.

For our project we needed to encrypt a number of columns in the database for security purposes and tried out a tool called Jasypt and were able to meet our encryption requirements in under a day. Here are the changes we made to get it to work. Increase size of columns to be be encrypted in database. (3x was not large enough, 10x was large enough). Change type of non-varchar columns to varchar. Add the following dependency to your pom.xml

<dependency>  <groupId>org.jasypt</groupId>  <artifactId>jasypt</artifactId>  <version>1.3.1</version></dependency>
In model classes that have encrypted properties, define a new hibernate @Type as follows. In this example, I am encrypting a String property and a Date property. Note that for embedded model classes (i.e. those with the Hibernate @Embedded annotation) the new @Type only needs to be defined in the enclosing class.
@TypeDef(            name="encrypted_string",            typeClass = org.jasypt.hibernate.type.EncryptedStringType.class,            parameters = {                @Parameter(name="encryptorRegisteredName", value="hibernateStringEncryptor"),            }        ),        @TypeDef(                name="encrypted_date_as_string",                typeClass = org.jasypt.hibernate.type.EncryptedDateAsStringType.class,                parameters = {                    @Parameter(name="encryptorRegisteredName", value="hibernateStringEncryptor"),                }            )        }
Above each getter of each property in the model class that needs to be encrypted, add annotations as follows.
@Type(type="encrypted_string")    public String getFoo() {        return foo;    }    @Type(type="encrypted_date_as_string")       public Date getFooDate() {        return fooDate;    }
Tell Jasypt how to instantiate a hibernateStringEncryptor bean via Spring. Add the following to your Spring configuration file (applicationContext.xml).
<bean id="hibernateStringEncryptor"    class="org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor">    <property name="registeredName">        <value>hibernateStringEncryptor</value>    </property>    <property name="password">        <value>yourPasswordGoesHere</value>    </property>  </bean>
The way we tested this was we created a record and then read the record back through straight JDBC to confirm that unencrypted columns matched and encrypted columns did not match. Note that on our project I ran into a problem in that at test time, Jasypt was not able to find hibernateStringEncryptor because the hibernateStringEncryptor bean had never been instantiated (in the application server, all the beans are automatically instantiated when the app server starts up). To get around this problem, we added the following line to our test startup method to instantiate one of these beans before running the tests so that the hibernateStringEncryptor got registered with Jasypt.
getFactory().getBean("hibernateStringEncryptor");

Note that the encryption used in this example is merely "Strong" encryption. If you want to use an even stronger encryption, then you would change

<property name="password">        <value>yourPasswordGoesHere</value>    </property>
to the following
<property name="algorithm">        <value>PBEWithMD5AndTripleDES</value>    </property>    <property name="password">        <value>yourPasswordGoesHere</value>    </property>    <property name="keyObtentionIterations">        <value>1000</value>    </property>
.

However if you do this, then you will need to change your local_policy.jar and US_export_policy.jar files in your C:\Program Files\Java\jdk1.5.X_XX\jre\lib\security folder with the "Unlimited Strength Java(TM) Cryptography Extension Policy Files" versions of these files. You can obtain these from Sun by clicking on the "Download" button beside "Java Cryptography Extension (JCE)" in the "Other Downloads" section of This Page.


source:  http://i-proving.ca/space/Ken+Stevens/blog/2007-09-07_2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.