How to use ENCRYPTBYPASSPHRASE and DECRYPTBYPASSPHRASE in hibernate?

1

I comment, I'm working with hibernate 4.3 in netbean with SQL connection and in my database I have the following table:

USUARIO(
 ID_USUARIO VARCHAR(08) NOT NULL PRIMARY KEY
 USU VARCHAR(12)
 CLAVE VARBINARY(8000)
)

Before working with hibernate I was encrypting and decrypting my key with stored procedure using ENCRYPTBYPASSPHRASE and SQL DECRYPTBYPASSPHRASE, now that I work with hibernate I was using the same stored procedures and it works fine, but I was seeing that there is a way to do it directly with:

@Column(name = "CLAVE", nullable = false)
    @ColumnTransformer(write = "ENCRYPTBYPASSPHRASE('frase',?)", read = "DECRYPTBYPASSPHRASE('frase',CLAVE)")
    public byte[] getClave() {
        return this.clave;
    }

I also tried with (and still does not work)

CAST(DECRYPTBYPASSPHRASE('frase',CLAVE) as varchar(12))

But when doing my query and showing the key it shows me characters and not the key itself.

System.out.println("Pass 2: " + new String(usuario.getClave()));

According to what I read, what I do is fine, but does not work any idea of why?

    
asked by Oscar 10.06.2018 в 19:22
source

1 answer

0

What I had done was fine, my problem was that when mapping the tables in the database I mapped them as xml files (in hibernate.cfg.xml )

<mapping resource="dao/dto/hibernate/Usuario.hbm.xml"/>

and not as annotations.

<mapping class="dao.dto.hibernate.Usuario"/>

Keep in mind:

USUARIO(
 ID_USUARIO VARCHAR(08) NOT NULL PRIMARY KEY,
 USU VARCHAR(12),
 CLAVE VARBINARY(8000),
)

Set and get of the Pojo:

@Column(name = "CLAVE", nullable = false)
@ColumnTransformer(write = "ENCRYPTBYPASSPHRASE('palabrita',?)", read = "DECRYPTBYPASSPHRASE('palabrita',CLAVE)")
public byte[] getClave() {
    return this.clave;
}

public void setClave(byte[] clave) {
    this.clave = clave;
}

when reading, you must transform from byte [] to String and write vice versa.

    
answered by 11.06.2018 / 00:57
source