I am working on updating a password encryption utility from something entirely homegrown to one built around Jasypt and Bouncy Castle. The utility encrypts the password; the encrypted string is then patched into a properties file and read back in and decrypted by a Grails application.
I wrote a Java command-line utility for encrypting the password. The relevant Java code is:
public class PasswordUtility {
private final String SALT = "randomstring";
private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
public PasswordUtility( String seed ) {
String password = SALT + seed;
encryptor.setProvider( new BouncyCastleProvider() );
encryptor.setAlgorithm( "PBEWITHSHA256AND192BITAES-CBC-BC" );
encryptor.setPassword( password );
}
public String decrypt( String encryptedText ) {
String processText = encryptedText;
return encryptor.decrypt( processText );
}
public String encrypt( String plainText ) {
return encryptor.encrypt( plainText );
}
}
The Groovy code is:
class StringEncryptor {
String salt = "randomstring"
private StandardPBEStringEncryptor initCrypto( String keySplit ) {
StandardPBEStringEncryptor pbe = new StandardPBEStringEncryptor()
pbe.setProvider( new BouncyCastleProvider() )
pbe.setAlgorithm( "PBEWITHSHA256AND192BITAES-CBC-BC" )
String cryptKey = salt + keySplit
pbe.setPassword( cryptKey )
return pbe
}
String encrypt( String keySplit, String encryptText ) {
StandardPBEStringEncryptor pbe = initCrypto( keySplit )
pbe.encrypt( encryptText )
}
String decrypt( String keySplit, String encryptText ) {
log.info encryptText
log.info keySplit
StandardPBEStringEncryptor pbe = initCrypto( keySplit )
pbe.decrypt( encryptText )
}
}
When I run PasswordUtility locally (on a Mac), paste the results into the properties file and run Grails locally, the password is decrypted correctly. When I run PasswordUtility on a RHEL virtual server and paste the results into the properties file, I get an EncryptionOperationNotPossibleException, and looking at the code, it implies that the decryption might have failed -- StandardPBEByteEncryptor line 1055). When I take a string from the Mac and try to decrypt it on RHEL, the decryption returns a null. I am able to take a string from one RHEL box and decrypt it on another RHEL box.