Contents Index Obfuscating an UltraLite database Changing the encryption key for a database

UltraLite Static Java User's Guide
  Adding Non Data Access Features to UltraLite Applications
    Configuring and managing database storage
      Encrypting UltraLite databases

Encrypting an UltraLite database

UltraLite databases are created on the first connection attempt. To encrypt an UltraLite database, you supply an encryption key before that connection attempt. On the first attempt, the supplied key is used to encrypt the database. On subsequent attempts, the supplied key is checked against the encryption key, and connection fails unless the key matches.

To strongly encrypt an UltraLite database (Java)

  1. Set a property named key before creating a database object for the first time.

    Here is a code fragment that reads the encryption key from the command line.

    InputStreamReader isr = new InputStreamReader( System.in );
    BufferedReader br = new BufferedReader( isr );
    String key = null ;
    System.out.print( "Enter encryption key:" );
    key = br.readLine() ;
    System.out.println( "The key is: " + key );
    
    // (3) Connect to the database
    java.util.Properties p = new java.util.Properties();
    p.setProperty( "persist", "file" );
    p.setProperty( "key", key );
    SampleDB db = new SampleDB( p );

    Here, SampleDB is the database filename as supplied in the UltraLite generator -f command-line option.

    For more information, see The UltraLite generator, and Using a Properties object to store connection information.

  2. Create the database object using the properties.

    For example:

    Connection conn = db.connect();

    After the first connection attempt, subsequent attempts to access the database produce an Incorrect or missing encryption key SQLException if the wrong key is supplied.

You can find a sample Java application demonstrating encryption in the directory \Samples\UltraLite\JavaSecurity. The encryption code is held in \Samples\UltraLite\JavaSecurity\Sample.java.

Here is a code snippet from the sample:

// Obtain the encryption key
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( isr );
String key = null ;
System.out.print( "Enter encryption key:" );
key = br.readLine() ;
System.out.println( "The key is: " + key );

java.util.Properties p = new java.util.Properties();
p.setProperty( "persist", "file" );
p.setProperty( "key", key );
SampleDB db = new SampleDB( p );
Connection conn = db.connect();

Contents Index Obfuscating an UltraLite database Changing the encryption key for a database