Java Q&A
by Cliff Berg

Listing One
private final byte[] salt = { 
    (byte)0xaa, (byte)0xbb, (byte)0xcc, (byte)0xdd,
    (byte)0x22, (byte)0x44, (byte)0xab, (byte)0x12 };
private final int iterations = 10;

Listing Two
protected static Cipher computePBECipher(String password, 
        byte[] salt, int iterations, String cipherName, int mode)
throws Exception
{
    // Compute the key
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, iterations);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(cipherName);
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
                    
    // Construct the cipher
    Cipher cipher = Cipher.getInstance(cipherName);
    cipher.init(mode, key, pbeParamSpec);
    return cipher;
}

Listing Three
Cipher cipher = computePBECipher
(
    password,            // the user's chosen password
    salt,                // the "salt" - gets added to the password
    iterations,          // number of times to apply the encryption
    cipherName,          // "PBEWithMD5AndDES"
    Cipher.ENCRYPT_MODE  // use "DECRYPT" to reverse the process
);

Listing Four
#
# List of providers and their preference orders 
#
security.provider.1=sun.security.provider.Sun
security.provider.2=com.sun.crypto.provider.SunJCE   

Listing Five
FileOutputStream fos = null;
fos = new FileOutputStream(path);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);







2


