Skip to content

Commit 32f0aaa

Browse files
author
kevin.w.wall
committed
New example - show how to display encrypted properties.
1 parent 6d205be commit 32f0aaa

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import org.owasp.esapi.EncryptedProperties;
4+
import org.owasp.esapi.errors.EncryptionException;
5+
import org.owasp.esapi.reference.crypto.DefaultEncryptedProperties;
6+
7+
// Purpose: Short code snippet to show how to display encrypted property files
8+
// that were encrypted using ESAPI's EncryptedProperties class.
9+
//
10+
// Usage: java -classpath <cp> DisplayEncryptedProperties encryptedPropFileName
11+
// where <cp> is proper classpath, which minimally include esapi.jar & log4j.jar
12+
public class DisplayEncryptedProperties {
13+
14+
public DisplayEncryptedProperties() {
15+
}
16+
17+
public void loadProperties(String encryptedPropertiesFilename,
18+
Properties props )
19+
throws IOException
20+
{
21+
EncryptedProperties loader = new DefaultEncryptedProperties();
22+
loader.load( new FileInputStream(
23+
new File( encryptedPropertiesFilename) ) );
24+
25+
try {
26+
props.setProperty( "database.driver",
27+
loader.getProperty( "database.driver" ) );
28+
props.setProperty( "jdbc.url",
29+
loader.getProperty( "database.url" ) );
30+
props.setProperty( "jdbc.username",
31+
loader.getProperty( "database.username" ) );
32+
props.setProperty( "jdbc.password",
33+
loader.getProperty( "database.password" ) );
34+
} catch( EncryptionException ee ) {
35+
ee.printStackTrace();
36+
}
37+
}
38+
39+
public void showProperties(Properties props) throws Exception
40+
{
41+
String value = null;
42+
value = props.getProperty( "database.driver");
43+
System.out.println("database.driver=" + value);
44+
value = props.getProperty( "jdbc.url");
45+
System.out.println("jdbc.database=" + value);
46+
value = props.getProperty( "jdbc.username");
47+
System.out.println("jdbc.username=" + value);
48+
value = props.getProperty( "jdbc.password");
49+
System.out.println("jdbc.password=" + value);
50+
}
51+
52+
53+
public static void main(String[] args) {
54+
55+
try {
56+
DisplayEncryptedProperties dep = new DisplayEncryptedProperties();
57+
Properties props = new Properties();
58+
59+
String encryptedPropFname = "encrypted.properties";
60+
if ( args.length == 1 ) {
61+
encryptedPropFname = args[0];
62+
}
63+
64+
dep.loadProperties(encryptedPropFname, props);
65+
dep.showProperties(props);
66+
67+
} catch(Throwable t) {
68+
System.err.println("Caught: " + t.getClass().getName() +
69+
"; exception msg: " + t);
70+
t.printStackTrace(System.err);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)