Monday, June 13, 2011

Database encryption using Jasypt (Java Simplified Encryption)


Here are required jars for using Jasypt
commons-lang
commons-codec
jasypt

Here is example of string encryptor with Spring and Hibernate.


Define encryptor in spring configuration file:

    
    
        PBEWithSHA1AndDESede
    
  
    
        jasypt
    
    
        4
    
 
  
  
    
        strongHibernateStringEncryptor
    
    
        
    
 

In hibernate mapping file define type as follows:

@TypeDef(
        name="encryptedString", 
        typeClass=EncryptedStringType.class, 
        parameters={@Parameter(name="encryptorRegisteredName",
                               value="strongHibernateStringEncryptor")}
    )


And then specify type as encryptedString for the columns you want to encrypt.
e.g.

@Column(name = "QO_OPTION", length = 4000, nullable = false)
 @Type(type="encryptedString")
 private String optionText;

Here hibernate will take care of encrypting while saving string to database and decrypting while loading object from database.


No comments: