org.jasypt.exceptions.EncryptionOperationNotPossibleException Jasypt and Hibernate running as windows service

246 Views Asked by At

A Hibernate application (no spring-boot) uses Jasypt-1.9.3 to decrypt database password from properties file. And it works just fine as a jar. However when the jar file is run as windows service using prunsrv.exe (a Commons Daemon Service Runner) it gives org.jasypt.exceptions.EncryptionOperationNotPossibleException (again, when the DB password is not encrypted, running the jar as a windows service works without problem). I have attached the code snippet where the decryption occurs. The salt used is declared as env't variable JASYPT_ENCRYPTOR_PASSWORD.

        Properties properties = new Properties();
        ResourceBundle options = Helper.getResourceFile(System.getProperty("user.dir") + "/system.properties");
        StandardPBEStringEncryptor  encryptor = new StandardPBEStringEncryptor();
        String salt = System.getenv("JASYPT_ENCRYPTOR_PASSWORD");
        String dbPassword = options.getString("db_password");
        if (salt != null && dbPassword.startsWith("ENC(")) {
            dbPassword = dbPassword.replace("ENC(", "");
            dbPassword = dbPassword.substring(0, dbPassword.lastIndexOf(")"));
            encryptor.setPassword(salt);
            encryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
            encryptor.setIvGenerator(new RandomIvGenerator());
            dbPassword = encryptor.decrypt(dbPassword);
        }

        properties.setProperty("hibernate.connection.username", options.getString("db_user"));
        properties.setProperty("hibernate.connection.password", dbPassword);
        properties.setProperty("hibernate.connection.url", "jdbc:jtds:sqlserver://" + options.getString("db_url") + ":" + options.getString("db_port") + "/" + options.getString("db_name") + ";characterEncoding=UTF-8;TDS=7.0");
        return new AnnotationConfiguration().configure().mergeProperties(properties).buildSessionFactory();

I'm thinking this has to do with some kind of windows service configuration / permission. What are the possible things to check for?

Thanks in advance

1

There are 1 best solutions below

0
On

Solved the issue. I had overlooked a very simple step. When a windows service is run with prunsrv.exe, there's a .bat file that installs the jar as a service. Since I had added new features to the java application that included encryption/decryption of properties file, I needed to re-install the jar as a service, as suppose to just stop-and-start it from windows services. Therefore, re-running the .bat file solved my problem.