package org.sagebionetworks.bridge.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.salt.StringFixedSaltGenerator;

public class EncryptorUtil {

    /**
     * arg0: password,
     * arg1: salt
     * arg2: target value
     * arg3: encrypt or decrypt, encrypt is the default
     */
    public static void main(String[] args) {
        if (args.length < 3) {
            throw new RuntimeException("Must supply at least 3 parameters: "
                    + "password, salt, target, decrypt (optional, when missing, encrypt)");
        }
        final String password = args[0];
        final String salt = args[1];
        final StringEncryptor encryptor = getEncryptor(password, salt);
        if (args.length == 3 || "encrypt".equalsIgnoreCase(args[3])) {
            System.out.println("Encrypted: " + encryptor.encrypt(args[2]));
        } else {
            System.out.println("Decrypted: " + encryptor.decrypt(args[2]));
        }
    }

    /**
     * Gets a password-based string encryptor.
     */
    public static PBEStringEncryptor getEncryptor(final String password, final String salt) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword(password);
        encryptor.setSaltGenerator(new StringFixedSaltGenerator(salt));
        // TODO: Better encryption
        // encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
        // encryptor.setKeyObtentionIterations(2000);
        // encryptor.setSaltGenerator(new RandomSaltGenerator());
        return encryptor;
    }
}

@@ -37,16 +37,75 @@
 onfig;%0A%0A
+import org.bouncycastle.jce.provider.BouncyCastleProvider;%0A
 import o
@@ -1401,144 +1401,52 @@
 setP
-assword(password);%0A        encryptor.setSaltGenerator(new StringFixedSaltGenerator(salt));%0A        // TODO: Better encryption%0A        //
+rovider(new BouncyCastleProvider());%0A       
  enc
@@ -1474,26 +1474,33 @@
 PBEW
-ithMD5AndTripleDES
+ITHSHAAND256BITAES-CBC-BC
 %22);%0A
@@ -1498,35 +1498,32 @@
 BC-BC%22);%0A       
- //
  encryptor.setKe
@@ -1524,35 +1524,25 @@
 .set
-KeyObtentionIterations(2000
+Password(password
 );%0A 
@@ -1551,11 +1551,8 @@
     
- //
  enc
@@ -1583,14 +1583,19 @@
 new 
-Random
+StringFixed
 Salt
@@ -1604,16 +1604,20 @@
 nerator(
+salt
 ));%0A    

Replace the default PBEWithMD5AndDES with PBEWITHSHAAND256BITAES/CBC.