Jasypt Users Forum

Re: Performance of StandardPBEByteEncryptor using Spring

by marcos.sousa () :: Rate this Message:

Reply to Author | View in Thread

After checking the code of jasypt, looks like there is no issue related to jasypt.

The only problem was with encryption process itself.

Daniel, change the code of class: StandardPBEByteEncryptor to PBEParameterSpec be initialized  ONLY ONE TIME.

private PBEParameterSpec parameterSpec;

For encryption method:
        if (parameterSpec == null) {
        log.info("ONLY BECAUSE I AM NULL");
              // Create salt
                byte[] salt = this.saltGenerator.generateSalt(this.saltSizeBytes);

                /*
                 * Perform encryption using the Cipher
                 */
                parameterSpec =
                    new PBEParameterSpec(salt, this.keyObtentionIterations);
        }

For decryption method:
if (parameterSpec == null) {
        // If we are using a salt generator which specifies the salt
                // to be included into the encrypted message itself, get it from
                // there. If not, the salt is supposed to be fixed and thus the
                // salt generator can be safely asked for it again.
                byte[] salt = null;
                if (this.saltGenerator.includePlainSaltInEncryptionResults()) {
                    salt = ArrayUtils.subarray(
                            encryptedMessage, 0, this.saltSizeBytes);
                } else {
                    salt = this.saltGenerator.generateSalt(this.saltSizeBytes);
                }
               
               
                /*
                 * Perform decryption using the Cipher
                 */
                parameterSpec =
                    new PBEParameterSpec(salt, this.keyObtentionIterations);
        }

<code>    
private static final ExecutorService pool = Executors.newCachedThreadPool();

public static void main(String[] args) throws Exception {
                log.info("INI Sincronização");
                long start = Calendar.getInstance().getTimeInMillis();

                final StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
                standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
                standardPBEStringEncryptor.setPassword("#!xxxxxxxx!#");
                standardPBEStringEncryptor.initialize();
               
                for (int i = 1; i <= 4; i++) {
                        pool.execute(new Runnable() {
                                public void run() {
                                        int threadID = nextThreadNum();
                                        log.info("Thread-" + threadID + " started");
                                        for (int j = 1; j <= threadID*5000; j++) {
                                                for (int k = 1; k <= 23; k++) {
                                                        String strTeste = "marcos veludos augusto de sousa (" + j + "," + k + ")";
                                                        String strEncr = standardPBEStringEncryptor.encrypt(strTeste);
                                                }
                                        }
                                        log.info("Thread-" + threadID + " finished");
                                }
                        });
                }
                log.info("Sleeping 60 seconds");
                Thread.sleep(60000);
                log.info("Started pool shutdown...");
                shutdown();
                log.info("All tasks have completed following shut down");

long end = Calendar.getInstance().getTimeInMillis();
                log.info("END Sincronizaç?o " + (end - start)/1000 + " segundos");
                System.exit(0);
        }

        private static void shutdown() {
                pool.shutdown(); // Disable new tasks from being submitted
                try {
                        // Wait a while for existing tasks to terminate
                        if (!pool.awaitTermination(TIMEOUT_TIME, TimeUnit.SECONDS)) {
                                pool.shutdownNow(); // Cancel currently executing tasks
                                // Wait a while for tasks to respond to being cancelled
                                if (!pool.awaitTermination(TIMEOUT_TIME, TimeUnit.SECONDS))
                                        log.error("Pool did not terminate");
                        }
                } catch (InterruptedException ie) {
                        // (Re-)Cancel if current thread also interrupted
                        pool.shutdownNow();
                        // Preserve interrupt status
                        Thread.currentThread().interrupt();
                }
        }
       
        /* For autonumbering anonymous threads. */
    private static int threadInitNumber = 1;
    private static synchronized int nextThreadNum() {
        return threadInitNumber++;
    }
</code>