first, define the encryption type with a
@TypeDef annotation, which could be either inside the persisted entity
class or inside a @TypeDefs declaration in a separate
package-info.java file:
@TypeDef(
name="encryptedString",
typeClass=EncryptedStringType.class,
parameters= {
@Parameter(name="encryptorRegisteredName", value="myHibernateStringEncryptor")
}
)
...and then simply map property with the already declared type:
@Type(type="encryptedString")
public String getAddress() {
return address;
}
Providing the encryptor to Hibernate
myHibernateStringEncryptor ,This can be binded in two different manners, depending on whether we are using an IoC container like Spring or not.
With Spring
<bean id="hibernateStringEncryptor"
class="org.jasypt.hibernate4.encryptor.HibernatePBEStringEncryptor">
<property name="registeredName">
<value>strongHibernateStringEncryptor</value>
</property>
<property name="algorithm">
<value>PBEWithMD5AndTripleDES</value>
</property>
<property name="password">
<value>jasypt</value>
</property>
</bean>
Without Spring,
StandardPBEStringEncryptor strongEncryptor = new StandardPBEStringEncryptor();
HibernatePBEEncryptorRegistry registry =
HibernatePBEEncryptorRegistry.getInstance();
registry.registerPBEStringEncryptor("strongHibernateStringEncryptor", strongEncryptor);
Documentation here