title: Cannot encrypt password in configuration file
body: <p>I'm having trouble encrypting the database password in <code>hibernate.cfg.xml</code></p>
  
  <p>This is my property file.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;!-- Database connection settings --&gt;
  &lt;property name="connection.driver_class"&gt;com.microsoft.sqlserver.jdbc.SQLServerDriver&lt;/property&gt;
  &lt;property name="connection.url"&gt;jdbc:sqlserver://localhost:1433;databaseName=TEST;&lt;/property&gt;
  &lt;property name="connection.username"&gt;sa&lt;/property&gt;
  &lt;!-- Encryption --&gt;
  &lt;property name="connection.password"&gt;ENC(vMO/j5jfpaU2cUhPVoOk5Q==)&lt;/property&gt;
  &lt;property name="connection.provider_class"&gt;org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider&lt;/property&gt;
  &lt;property name="connection.encryptor_registered_name"&gt;hibernateEncryptor&lt;/property&gt;
  </code></pre>
  
  <p>Then in the <code>HiberanteUtil.java</code> I have this</p>
  
  <pre><code>// 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);
  }
  </code></pre>
  
  <p>I've created the encrypted password with <code>encrypt.bat</code>.</p>
  
  <p>Then the error i have is </p>
  
  <blockquote>
    <p>com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
    'sa'. ClientConnectionId:8033573f-5f52-4fe9-a728-fbe4f57d89c4</p>
  </blockquote>
  
  <p>If I remove this part</p>
  
  <pre><code>StandardPBEStringEncryptor encryptor =
          new StandardPBEStringEncryptor();
  encryptor.setPassword("someKey");
  HibernatePBEEncryptorRegistry registry =
          HibernatePBEEncryptorRegistry.getInstance();
  
  registry.registerPBEStringEncryptor(
          "hibernateEncryptor", encryptor);
  </code></pre>
  
  <p>I have the same error, so I think it doesn't register but I have no idea how to do it.</p>
  
  <p>This is how i encrypt</p>
  
  <p><img src="https://i.stack.imgur.com/F9Fhw.png" alt="jasypt problem image"></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The only thing i can made to get it work is something like this, but is not the way i think. </p>
  
  <pre><code>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);
  </code></pre>
  
  <p>So i think the problem is with the <code>"hibernateEncryptor"</code>, i think i need to register </p>
  
  <pre><code>  &lt;typedef name="encryptedString" class="org.jasypt.hibernate4.type.EncryptedStringType"&gt;
     &lt;param name="encryptorRegisteredName"&gt;hibernateEncryptor&lt;/param&gt;
    &lt;typedef&gt;
  </code></pre>
  
  <p>But when i put it in <code>hibernate.cfg.xml</code>  says invalid mapping, so i add it to a class with annotation but nothing happen cause i think this is read after database connection that is what i want to encrypt. :(</p>
  
  <pre><code>@TypeDef(name="encryptedString",typeClass=org.jasypt.hibernate4.type.EncryptedStringType.class,
          parameters= {@Parameter(name="encryptorRegisteredName",value="hibernateEncryptor")})
  </code></pre>