I'm having trouble encrypting the database password in hibernate.cfg.xml
This is my properties file.
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=TEST;</property> <property name="connection.username">sa</property> <property name="connection.password">ENC(vMO/j5jfpaU2cUhPVoOk5Q==)</property> <property name="connection.provider_class">org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider</property> <property name="connection.encryptor_registered_name">hibernateEncryptor</property>
Then in HiberanteUtil.java
I have this
// Builds session factory. private static SessionFactory configureSessionFactory() throws HibernateException { Configuration configuration = new Configuration().configure(); StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword("pass"); HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance(); registry.registerPBEStringEncryptor("hibernateEncryptor", encryptor); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()).buildServiceRegistry(); return configuration.buildSessionFactory(serviceRegistry); }
I created an encrypted password using encrypt.bat
.
Then the mistake i have
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user "Ca". ClientConnectionId: 8033573f-5f52-4fe9-A728-fbe4f57d89c4
If I remove this part
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword("someKey"); HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance(); registry.registerPBEStringEncryptor( "hibernateEncryptor", encryptor);
I have the same error, so I think it is not logging, but I have no idea how to do this.
This is how I encrypt

UPDATE
The only thing I can do to make it work is something like this, but it is not the way I think.
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword("somePass"); encryptor.setAlgorithm("PBEWITHMD5ANDDES"); String pass=encryptor.decrypt("HhpmA/XmJoLro8TYYu4YyA=="); HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance(); registry.registerPBEStringEncryptor( "hibernateEncryptor", encryptor); Configuration configuration = new Configuration().configure() .setProperty("hibernate.connection.encryptor_registered_name","hibernateEncryptor") .setProperty("hibernate.connection.password",pass);
So, I think the problem is related to "hibernateEncryptor"
, I think I need to register
<typedef name="encryptedString" class="org.jasypt.hibernate4.type.EncryptedStringType"> <param name="encryptorRegisteredName">hibernateEncryptor</param> <typedef>
But when I put it in hibernate.cfg.xml
, it speaks of incorrect matching, so I add it to the class with annotation, but nothing happens because I think it is reading after connecting to the database that I want to encrypt .: (
@TypeDef(name="encryptedString",typeClass=org.jasypt.hibernate4.type.EncryptedStringType.class, parameters= {@Parameter(name="encryptorRegisteredName",value="hibernateEncryptor")})