Advanced Customization > Business Logic Customization > Encrypted Passwords > Encrypted Passwords > Sample Code
  
Sample Code
Creating a Java keystore for Windchill to encrypt values:
try
{
WTKeyStore keyStore = new
WTKeyStore(WTProperties.getLocalProperties(). getProperty("wt.home"));

}catch(IOException e){
logger.error("Could not load keystore", e);
}
Encrypting a value in the keystore:
String value = “encrypted.wt.pom.dbPassword”;
String property = “wt.pom.dbPassword”;
try
{
WTKeyStore keyStore = new
WTKeyStore(WTProperties.getLocalProperties(). getProperty("wt.home"));

// encrypt property value
keyStore.put(property, value);

} catch (IOException ioe) {
logger.error("Error creating keystore: ", ioe);
} catch (NullPointerException npe) {
logger.error("Could not add the encrypted value to the keystore: ", npe);
}
Decrypting a property:
String product_root = WTProperties.getLocalProperties(). getProperty(“wt.home”);
String value = “encrypted.wt.pom.dbPassword”;
String property = “wt.pom.dbPassword”;
// decrypt encrypted values
value = WTKeyStoreUtil.decryptProperty(property, value, product_root);

/* value can now be used as normal as it will contain the decrypted value */
Additionally, a secondary means can be used to decrypt a value:
String value = “encrypted.wt.pom.dbPassword”;
try
{
WTKeyStore keyStore = new
WTKeyStore(WTProperties.getLocalProperties(). getProperty("wt.home"));

// decrypt encrypted values
String ks_value = keyStore.get(value);
if(ks_value != null)
{
value = ks_value;
}
}catch(IOException e){
logger.error("Could not load keystore", e);
}
Adding a new regular expression for encrypting dynamic properties:
/* This code currently exists in WTKeystoreUtil.java. except where
* where denoted “NEW”
*/
private static final String NEW_REGULAR_EXPRESSION = “<your regular
expression goes here>”; // NEW


private static List<String> getEncryptionPropertiesRegExList(final
String product_root) {
List<String> propertiesList = new ArrayList<String>();

if(isCompletePropertiesListFile(product_root)) {
/*
* add a hardcoded list of dynamic properties that will
* utilize regular expressions to determine what to encrypt
* for dynamic property names.
*/
propertiesList.add(WT_FEDERATION_DEFAULTAUTHORIZATION);
propertiesList.add(NEW_REGULAR_EXPRESSION); // NEW

logger.debug("Full property list found, returning regex
list of size=" + propertiesList.size());
} else {
logger.debug("Full property list not found, return empty
regex list");
}
return propertiesList;
}
This will add a new regular expression for matching property name patterns. Again, care must be taken when creating a regular expression. See Procedure – Encrypting Dynamic .xconf Managed Single-Valued Properties.