Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for EncryptableProperties #49

jpurnomosidi opened this issue Sep 9, 2013 · 8 comments

Support for EncryptableProperties #49

jpurnomosidi opened this issue Sep 9, 2013 · 8 comments


Copy link

@jpurnomosidi jpurnomosidi commented Sep 9, 2013

Owner is very cool framework in my opinion. However, how about the security of storing password in properties? Can owner support for encryptable properties like in Jasypt ?


Copy link

@lviggiano lviggiano commented Sep 9, 2013

👍 That is indeed very interesting. It happened many times I have deployed application in shared environment and some sensitive information was exposed in the configuration files. And that was not desirable, of course.

I would like to add this feature.

Copy link

@lviggiano lviggiano commented Sep 24, 2013

@jpurnomosidi I think that it should be possible for the user to implement some encryption using the @ConverterClass annotation.

For instance:

import org.aeonbits.owner.Config;
import org.aeonbits.owner.ConfigFactory;
import org.aeonbits.owner.Converter;
import sun.misc.BASE64Decoder;

import java.lang.reflect.Method;

 * To encrypt the password:
 *   System.out.println(new BASE64Encoder().encode(xor("tiger".getBytes(), "secret".getBytes())));

public class EncryptedPropertiesExample {

    interface EncryptedConfiguration extends Config {
        String scottPassword();

    public static class DecryptConverter implements Converter {
        public Object convert(Method method, String input) {
            try {
                String key = System.getProperty("example.encryption.key");
                return new String(xor(new BASE64Decoder().decodeBuffer(input), key.getBytes()));
            } catch (IOException e) {
                throw new UnsupportedOperationException(e);

    public static void main(String[] args) {
        System.setProperty("example.encryption.key", "secret");
        EncryptedConfiguration example = ConfigFactory.create(EncryptedConfiguration.class);

    private static byte[] xor(final byte[] input, final byte[] secret) {
        final byte[] output = new byte[input.length];
        if (secret.length == 0) {
            throw new IllegalArgumentException("empty encryption key");
        int spos = 0;
        for (int pos = 0; pos < input.length; ++pos) {
            output[pos] = (byte) (input[pos] ^ secret[spos]);
            if (spos >= secret.length) {
                spos = 0;
        return output;

When you run the above code, it will print tiger.

The above code runs fine with version 1.0.5-SNAPSHOT (on master branch), but possibly it doesn't work with version and prior (all released versions).

If you need this to work, I can backport a fix I made for 1.0.5-SNAPSHOT into 1.0.4 branch and release a bugfix release so you can exploit this solution.

Let me know.

By the way, I would like to implement some strong property encryption mechanism inside owner itself; in the meantime, this solution can possibly fix urgencies?

Copy link

@jpurnomosidi jpurnomosidi commented Sep 26, 2013 is ok. Btw, it's better not to use Sun base64 encoder/decoder ( Better use apache common codec or javax.xml.bind.DatatypeConverter.


Copy link

@lviggiano lviggiano commented Sep 26, 2013

I know, that was just for an example ;)

Copy link

@lviggiano lviggiano commented Oct 9, 2013

I removed the sun base64 and added commons-codec as test dependency. I will implement this feature more appropriately in the next versions. Today I released version 1.0.5 so you can use the solution I proposed as example if you update to version 1.0.5.

At the moment of writing, version 1.0.5 is not yet on maven central repository yet, but it should be there soon.

@ghost ghost assigned lviggiano Oct 18, 2013
Copy link

@rajatvig rajatvig commented Jan 13, 2014

public static class EncryptedValueConverter implements Converter<String> {
        public String convert(Method method, String input) {
            return (PropertyValueEncryptionUtils.isEncryptedValue(input) ? PropertyValueEncryptionUtils.decrypt(input, getEncryptor()) : input);

    private StringEncryptor getEncryptor() {
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();

        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        return standardPBEStringEncryptor;

The code is roughly illustrative of how we've used it with Jaspyt.
Maybe providing an annotation to provide the Encyptor will be nice.

Copy link

@lviggiano lviggiano commented Jan 13, 2014

Thanks @rajatvig for the example.

Copy link

@rrialq rrialq commented Sep 16, 2015

I have a owner version which implements this feature, I will do a pull request as soon as possible, after ending my tests, if you think this is the right way. The implementation doesn't need an external library. An example of a configuration with these feature:

    public interface SampleConfig extends Config {
        String password();

        String username;

@DecrypterManagerClass is a simple annotation to set the class that decrypts all of the passwords in this configuration.
@ENCRYPTEDKEY is the annotation to mark a property as encrypted. In previous example only password property will be decrypted by the SampleDecrypter.

The following code is implemented in the new code, and avoids the commons-codec library. Following is an example about using javax.crypto:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import javax.xml.bind.DatatypeConverter;

public class AESEncryption {
    public static final String ALGORITHM = "AES";

    private final String encoding;
    private final byte[] keyValue;

    public String getAlgorithm() {
        return AESEncryption.ALGORITHM;

    public AESEncryption( String keyValue )
    throws UnsupportedEncodingException {
        this ( keyValue, "UTF-8" );

    public AESEncryption( String keyValue, String encoding )
    throws UnsupportedEncodingException {
        this.encoding = encoding;
        this.keyValue = keyValue.getBytes( encoding );

    public String encrypt(String plainData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal( plainData.getBytes( this.encoding ) );
        String encryptedValue = DatatypeConverter.printBase64Binary( encVal );
        return encryptedValue;

    public String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance( ALGORITHM );
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = DatatypeConverter.parseBase64Binary( encryptedData );
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue, this.encoding );
        return decryptedValue;

    private Key generateKey() throws Exception {
        return new SecretKeySpec( this.keyValue, this.getAlgorithm() );

A simple test:

import org.junit.Assert;
import org.junit.Test;

public class AESEncryptionTestCase {
    public void encriptionDecriptionTest()
    throws UnsupportedEncodingException, Exception {
        String cipherKey = "ABCDEFGH12345678";
        String expectedValue = "This is a decoded value";
        AESEncryption aes = new AESEncryption( cipherKey );
        String encriptedValue = aes.encrypt( expectedValue );
        System.out.println( "Encripted=" + encriptedValue );
        String decriptedValue = aes.decrypt( encriptedValue );
        System.out.println( "Decripted=" + decriptedValue);
        Assert.assertEquals("The decripted value is not the same as original value", expectedValue, decriptedValue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants