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 10: The source for Util.java, a utility class used by other classes
in the packaged "pkg."
// $Id$
package pkg;
import java.io.*;
public class Util
{
// Read a file into a byte array
static public byte[] readFile( String filename ) throws IOException {
File file = new File( filename );
long len = file.length();
byte data[] = new byte[(int)len];
FileInputStream fin = new FileInputStream( file );
int r = fin.read( data );
if (r != len)
throw new IOException( "Only read "+r+" of "+len+" for "+file );
fin.close();
return data;
}
// Write byte array to a file
static public void writeFile( String filename, byte data[] )
throws IOException {
FileOutputStream fout = new FileOutputStream( filename );
fout.write( data );
fout.close();
}
}