Spring Hibernate Entity Encryption

An entity encryption library that seamlessly encrypts when stored to database and decrypts when it is fetched.

License

License

Categories

Categories

Hibernate Data ORM
GroupId

GroupId

com.github.mekuanent
ArtifactId

ArtifactId

spring-hibernate-entity-encryption
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Hibernate Entity Encryption
An entity encryption library that seamlessly encrypts when stored to database and decrypts when it is fetched.
Project URL

Project URL

https://github.com/mekuanent/SpringHibernateEntityEncryption
Source Code Management

Source Code Management

http://github.com/mekuanent/SpringHibernateEntityEncryption/tree/master

Download spring-hibernate-entity-encryption

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mekuanent/spring-hibernate-entity-encryption/ -->
<dependency>
    <groupId>com.github.mekuanent</groupId>
    <artifactId>spring-hibernate-entity-encryption</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mekuanent/spring-hibernate-entity-encryption/
implementation 'com.github.mekuanent:spring-hibernate-entity-encryption:1.0.1'
// https://jarcasting.com/artifacts/com.github.mekuanent/spring-hibernate-entity-encryption/
implementation ("com.github.mekuanent:spring-hibernate-entity-encryption:1.0.1")
'com.github.mekuanent:spring-hibernate-entity-encryption:jar:1.0.1'
<dependency org="com.github.mekuanent" name="spring-hibernate-entity-encryption" rev="1.0.1">
  <artifact name="spring-hibernate-entity-encryption" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mekuanent', module='spring-hibernate-entity-encryption', version='1.0.1')
)
libraryDependencies += "com.github.mekuanent" % "spring-hibernate-entity-encryption" % "1.0.1"
[com.github.mekuanent/spring-hibernate-entity-encryption "1.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar 2.1.6.RELEASE
org.springframework.boot : spring-boot-starter-data-jpa jar 2.1.6.RELEASE

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Spring Entity Encryption

This library will allow you to to easily store data in encrypted format. It will store the attributes of your choice encrypted in your database and gives you the decrypted version when you need to read it.

Here's the Loopback version of the library https://github.com/mekuanent/loopback-encryption-mixin

Installation

For maven based projects

<dependency>
  <groupId>com.github.mekuanent</groupId>
  <artifactId>spring-hibernate-entity-encryption</artifactId>
  <version>1.0.0</version>
</dependency>

For gradle based projects

implementation 'com.github.mekuanent:spring-hibernate-entity-encryption:1.0.0'

You can check out this url for other builds https://search.maven.org/artifact/com.github.mekuanent/spring-hibernate-entity-encryption/1.0.0/jar

Setup

Add the following annotation to import the encryption configuration class.

...
@Import(EnableEncryptionConfig.class)
public class Application {
...

After that you need to specify your encryption key, salt,... globally. To do that you need to write the following in your application's main method.

EncryptionHandler.set(new PBEHandler("<your password>",
                "<your salt>", "<your IV>", <iteration>, 
                <derived key length (optional with default 256)>));

Next, put @Encrypted annotation on all fields of the entities you want to be encrypted when stored.

E.g.

@Encrypted
private String title;

That's it, you're all set.

Custom Encryption Algorithm

If you don't want to use the default encryption scheme. you can define your own by creating an @Component annotated class implementing IEncryptionHandler

E.g.

@Component
public class CustomEncryptionHandler implements IEncryptionHandler {

    @Override
    public String encrypt(String raw) {}
    
    @Override
    public String decrypt(String cipherText) {}

}

There are two ways of setting your custom encryption handler,

setting handler Globally

you can set it up in the main method of your application.

EncryptionHandler.set(new CustomEncryptionHandler("<your password>",
                "<your salt>", "<your IV>", <iteration>, 
                <derived key length (optional with default 256)>));

setting handlers for every field

In this case, your custom encryption handler is required to have an empty constructor.

You can set your handler to the field of your choice by just including it as a handler parameter

E.g.

@Encrypted(handler = CustomEncryptionHandler.class)
private String description;

Resources

you can checkout the complete sample application here: https://github.com/mekuanent/SpringEntityEncryptionLibExample

License

Spring Hibernate Entity Encryption is released under the terms of the Apache Software License Version 2.0 (see license.txt).

Versions

Version
1.0.1
1.0.0