How to integrate support for securing a property in my Java application?

ImportantThe following article uses options that are available starting with the Professional edition and project type.

Step 1 - Loading and initializing the SecureProp library

 
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.WString;
private interface SecurePropDLL extends Library {
    // Match the calling convention of the "GetRevealedTextLength" exported function
    int GetRevealedTextLength(WString aSecuredText);
    
    // Match the calling convention of the "RevealText" exported function
    void RevealText(WString aSecuredText, char[] aPlainTextBuffer, int aBufferLength);
}
…
// Load the dll file
SecurePropDLL dll = Native.loadLibrary(“SecureProp.dll”, SecurePropDLL.class);
…
      

SecureProp library exported functions

  • GetRevealedTextLength - the DLL exported function that reveals the secured text and returns its length
  • RevealText - the DLL exported function that reveals the secured text and stores the result in the provided buffer

Step 2 - Using the SecureProp library

 
// Load the dll file
SecurePropDLL dll = Native.loadLibrary(kDllName, SecurePropDLL.class);
// Load the properties from the ini file
ArrayList<SecuredProperty> properties = new ArrayList<SecuredProperty>();
LoadPropertiesFromIni(properties);
if (properties.size() == 0)
    return;
//------------------------------------------------------------------------------------------
// Use the SecureProp library
//------------------------------------------------------------------------------------------
for (SecuredProperty property : properties) {
    WString propValue = new WString(property.mValue);
    
    //------------------------------------------------------------------------------------------
    // Exported function call
    // Reveal the secured text and return its length
    //------------------------------------------------------------------------------------------
    int textLength = dll.GetRevealedTextLength(propValue);
    
    // Allocate a buffer of wide characters with the length: textLength + 1
    // We need to add 1 in order to provide space for the null terminator
    char[] textBuffer = new char[++textLength];
    //------------------------------------------------------------------------------------------
    // Exported function call
    // Reveal the secured text and store the result in the provided buffer
    //------------------------------------------------------------------------------------------
    dll.RevealText(propValue, textBuffer, textLength);
  
    // Output the revealed text
    System.out.println(property.mName + " = " + new String(textBuffer));
}
      

Sample Project

SecurePropExample.jar contains dependencies to JNA (Java Native Access), which can be downloaded from here. Afterwards, you need to add it in the installation directory, next to SecurePropExample.jar.

Download the Java sample showcasing how the secure property functionality works.

NoteThe project requires JRE 1.8.0_151 or higher to run.