Encrypted Preferences in Java
By Greg Travis, October 01, 2002
If you're using the Preferences API in Java, this encryption strategy lets you hide your preferences data in plain sight.
October 2002/Encrypted Preferences in Java
Listing 3: A simple test program, this time using encryption. It does more
or less the same thing as the program in Listing 1, except that this variant
uses an Encrypted Preferences object, which transparently encrypts the data
before storing it, and decrypts it before retrieving it.
// $Id$
package pkg.encrypted;
import java.security.*;
import java.util.prefs.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import ep.*;
import pkg.Util;
public class EncryptedTest
{
static private final String algorithm = "DES";
static public void main( String args[] ) throws Exception {
byte rawKey[] = Util.readFile( "key" );
DESKeySpec dks = new DESKeySpec( rawKey );
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( algorithm );
SecretKey secretKey = keyFactory.generateSecret( dks );
Preferences root =
EncryptedPreferences.userNodeForPackage(
EncryptedTest.class, secretKey );
root.put( "transparent", "encryption" );
Preferences subnode = root.node( "subnode" );
subnode.put( "also", "encrypted" );
root.exportSubtree( System.out );
}
}