Data at rest encryption with Jasypt and Hibernate

January 14, 2010

Data at rest encryption is a commonly important pattern in any enterprise application within which certain information must be protected when placed in a persisted state. Among the difficulties of building applications that support data at rest encryption are distinguishing encrypted data from unencrypted data at the application layer, and the algorithms needed to handle translating from one to the other. An application which is aware that at some points its data may be encrypted and at other points it may not violates the practice of separation of concern. The possibility that data must be encrypted at rest is irrelevant to application layer logic, which need not be concerned with such matters. This idea is encapsulated by the phrase transparent data at rest encryption.

Jasypt solves part of this problem by providing an easy to use encryption library which abstracts the often unnecessary cryptography background from implementation and integration concerns. Coupled with a cryptography API such as Bouncy Castle, Jasypt integrates smoothly into a Hibernate and Spring environment to provide transparent data at rest encryption.

In the following example, a simple domain will be used to demonstrate Jasypt and Hibernate integration in a Spring application. Data will be transparently encrypted when written to the database, and transparently decrypted when read from the database.


The domain consists of a simple class called Employee. The Employee class contains a field, ssn, which stores an employee's social security number. Due to the sensitive nature of this sort of data, it is a candidate for data at rest encryption.

@Table(name = "employee")
public class Employee {

  @Column(name = "identifier")
  private String identifier;

  @Column(name = "name")
  private String name;

   * The employee's social security number, which is to be encrypted within
   * the database.
  @Column(name = "ssn")
  @Type(type = "encryptedString")
  private String ssn;

  public String getIdentifier() {
    return identifier;

  public void setIdentifier(String identifier) {
    this.identifier = identifier;

  public String getName() {
    return name;

  public void setName(String name) { = name;

  public String getSsn() {
    return ssn;

  public void setSsn(String ssn) {
    this.ssn = ssn;

The Employee class is a basic value object class storing an employee's name and social security number, as well as an identifier. The ssn property is annotated with Hibernate's @Type annotation, which allows its designation as an encrypted string field. The declaration of this type will be in below.

@TypeDefs( {
  @TypeDef(name = "encryptedString",
      typeClass = EncryptedStringType.class,
      parameters = {
        @Parameter(name = "encryptorRegisteredName",
            value = "strongHibernateStringEncryptor")
}) is a convenient place for declaring encrypted field type definitions, as it provides a single place for potentially many types of encrypted field type definitions and can be scanned easily by Spring.

public interface Repository {

  public Object retrieve(Class<?> entityClass, String identifier);

  public void store(Object entity);

The Repository interface specifies a basic data store, providing data retrieval and storage, and will be implemented using Hibernate.

public class HibernateRepository implements Repository {

  private SessionFactory sessionFactory;

  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;

  public Object retrieve(Class<?> entityClass, String identifier) {
    DetachedCriteria criteria = DetachedCriteria.forClass(entityClass).add(
    return criteria.getExecutableCriteria(

  public void store(Object entity) {

The HibernateRepository class implements the Repository interface using Hibernate's API.

That's it for our application code. All that remains is to test it.


public class TransactionalHibernateRepository implements Repository {

  private HibernateRepository hibernateRepository;

  public void setHibernateRepository(HibernateRepository hibernateRepository) {
    this.hibernateRepository = hibernateRepository;

  public Object retrieve(Class<?> entityClass, String identifier) {
    return hibernateRepository.retrieve(entityClass, identifier);

  public void store(Object entity) {;

The TransactionalHibernateRepository class is a dumb implementation of the Repository interface which is annotated as @Transactional and uses an instance of HibernateRepository. This will make it easy to let Spring handle transaction management and Hibernate Session management.

public class HibernateRepositoryTest {

  private JdbcTemplate jdbcTemplate;

  private HibernatePBEStringEncryptor hibernateStringEncryptor;

  private Repository repository;

  public void cleanDatabase() {
    jdbcTemplate.execute("delete from employee");

  public void testSsnEncryption() {

    Employee employee = new Employee();
    employee.setName("Johnny McDoe");

    String encSsn = jdbcTemplate.queryForObject(
        "select ssn from employee where identifier = '"
            + employee.getIdentifier() + "'", String.class);

    assertEquals(employee.getSsn(), hibernateStringEncryptor

  public void testSsnDecryption() {

        .execute("insert into employee (identifier, name, ssn) values ('emp1', 'Johnny McDoe', '"
            + hibernateStringEncryptor.encrypt("123-456-7890")
            + "')");

    Employee employee = (Employee) repository.retrieve(Employee.class, "emp1");

    assertEquals("emp1", employee.getIdentifier());
    assertEquals("Johnny McDoe", employee.getName());
    assertEquals("123-456-7890", employee.getSsn());

The HibernateRepositoryTest class runs two basic tests on the code: testSsnEncryption(), which ensures that data stored in the database is properly encrypted, and testSsnDecryption(), which ensures that data retrieved from the database is properly decrypted. An instance of HibernatePBEStringEncryptor is used to decrypt and verify transparently encrypted data, and is passed from the Spring configuration for this test case, shown below.


<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns=""
  xmlns:xsi="" xmlns:tx=""

  <tx:annotation-driven />

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />

  <bean id="transactionManager"
    <property name="sessionFactory" ref="sessionFactory" />

  <bean id="hibernateRepository" class="com.earldouglas.dbencryption.HibernateRepository">
    <property name="sessionFactory" ref="sessionFactory" />

  <bean id="transactionalHibernateRepository"
    <property name="hibernateRepository" ref="hibernateRepository" />

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="org.hsqldb.jdbcDriver" />
    <property name="jdbcUrl" value="jdbc:hsqldb:mem:db" />
    <property name="user" value="sa" />
    <property name="password" value="" />

  <bean id="sessionFactory"
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.earldouglas.dbencryption" />
    <property name="annotatedPackages" value="com.earldouglas.dbencryption" />
    <property name="hibernateProperties">
        <entry key="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
        <entry key="hibernate.show_sql" value="false" />

  <bean id="hibernateStringEncryptor"
    <property name="registeredName">
    <property name="algorithm">
    <property name="password">
    <property name="provider">
      <bean class="org.bouncycastle.jce.provider.BouncyCastleProvider" />


The Spring configuration contained in HibernateRepositoryTest-context.xml is relatively straightfoward; it sets up a JdbcTemplate for manual manipulation of the database, a transaction manager, instances of HibernateRepository, TransactionalHibernateRepository, a c3p0 DataSource, and a Hibernate SessionFactory, provided by Spring's AnnotatedSessionFactoryBean. Note in this bean the packagesToScan and annotatedPackages properties, which are used to pick up the @Entity-annotated Employee class as well as the @TypeDef declarations in Finally, a HibernatePBEStringEncryptor is instantiated using a standard algorithm, a registered name specified in the corresponding @TypeDef from, and Bouncy Castle as the JCE provider.

That's all there is to it. This is a very simple example, and there are many options regarding encryption algorithms, encryptable data types other than strings, and more.