title: How to set the hibernate.connection.password as an encrypted value in hibernate.cfg.xml and then decrypt it to be used in the config?
body: <p>I am trying to create a encrypted value in the properties for hibernate.connection.password.  At run time the encrypted value is picked up and decrypted.  I then try to overwrite the properties in the HibernateUtil.  But even after decryption the value doesn't seemed to be used because it fails to authenticate. How would I get this to work?</p>
  
  <pre><code>public class HibernateUtil {
      private static final SessionFactory sessionFactory;
  
      private static ServiceRegistry serviceRegistry;
  
      private static final String keyFile="/keys/.aoo1key";
      static {
          try {
                  Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
                  String passwordToDecrypt=cfg.getProperty("hibernate.connection.password");
                  if(passwordToDecrypt.startsWith("ENC(")&amp;&amp;passwordToDecrypt.endsWith(")")) {
                      passwordToDecrypt = passwordToDecrypt.substring(4,passwordToDecrypt.length()-1);
                      PropertiesEncryption propertiesEncryption = new PropertiesEncryption(keyFile);
                      String decrypted = propertiesEncryption.decryptValue(passwordToDecrypt);
                      cfg.setProperty("hibernate.connection.password", decrypted);
                  }
  
                  StandardServiceRegistry standardRegistry = cfg.getStandardServiceRegistryBuilder().build();
  
                  Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();
                  sessionFactory = metaData.getSessionFactoryBuilder().build();
          } catch (Throwable th) {
  
              System.err.println("Enitial SessionFactory creation failed" + th);
  
              throw new ExceptionInInitializerError(th);
  
          }
      }
  
      public static SessionFactory getSessionFactory() {
  
          return sessionFactory;
  
      }
  
  
  }
  </code></pre>
  
  <p>hibernate.cfg.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration"&gt;
  
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
  
  
  &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/hibernate5&lt;/property&gt;
  &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
   &lt;!--  &lt;property name="hibernate.connection.password"&gt;dog&lt;/property&gt;   --&gt;
  &lt;property name="hibernate.connection.password"&gt;ENC(MYFBqlDom8ZawVi+toB3PU0MdI10Desvsq4D2Z5lejk=)&lt;/property&gt;
  &lt;property name="hibernate.connection.pool_size"&gt;10&lt;/property&gt;
  &lt;property name="show_sql"&gt;true&lt;/property&gt;
  &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  
  &lt;mapping class="net.model.Employee" /&gt;
  
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>The Exception</p>
  
  <pre><code>Enitial SessionFactory creation failedorg.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  Exception in thread "main" java.lang.ExceptionInInitializerError
      at net.roseindia.HibernateUtil.&lt;clinit&gt;(HibernateUtil.java:66)
      at net.roseindia.GetAllData.main(GetAllData.java:18)
  Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
      at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
      at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
      at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
      at net.roseindia.HibernateUtil.&lt;clinit&gt;(HibernateUtil.java:60)
      ... 1 more
  Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
      at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:105)
      at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
      at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
      at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
      at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
      at org.hibernate.engine.jdbc.connections.internal.PooledConnections.&lt;init&gt;(PooledConnections.java:42)
      at org.hibernate.engine.jdbc.connections.internal.PooledConnections.&lt;init&gt;(PooledConnections.java:20)
      at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
      at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
      at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
      ... 13 more
  Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
      at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
      at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:718)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:46)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
      at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
      ... 28 more
  </code></pre>