Kyan
Kyan

Reputation: 505

MySQL column encryption with Hibernate in Spring MVC

I need to encrypt data saved onto my DB. I am currently using spring and hibernate to save data.

I have looked at some materials and tried to implement the code, however, it has resulted in various generic errors, some of the material was not targeted to MySQL etc.

Here's the code that has got me furthest

@Column(name="disability_description")
@Length(max=500)
@ColumnTransformer(
        read =  "AES_DECRYPT(disability_description, 'mykey')",
        write = "AES_ENCRYPT(?, 'mykey')"
                )
private String disabilityDescription;

This, however, doesn't work as I get the following errors

org.hibernate.exception.GenericJDBCException: could not execute statement

java.sql.SQLException: Incorrect string value: '\xF9\x82u\x01\x99\x1A...' for column 'disability_description' at row 1

Please point in the right direction. I am lost. Also mykey doesn't point to anything, I just entered a random word.

Upvotes: 1

Views: 1328

Answers (1)

hasnae
hasnae

Reputation: 2183

I doubt that your column is not of type BINARY:

Mysql Doc:

AES_ENCRYPT() encrypts the string str using the key string key_str and returns a binary string containing the encrypted output.

Upvotes: 1

Related Questions