{
getSecureRandom().nextBytes(data);
// get the checksum
byte[] checksum = getMD5Checksum(data);
CipherProvider tmpCipherProvider = createNewCipher(ENCRYPT,mainSecretKey,mainIV);
tmpCipherProvider.encrypt(data, 0, data.length, data, 0);
// openFileForWrite
verifyKeyFile = privAccessFile(sf,Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE,"rw");
// write the checksum length as int, and then the checksum and then the encrypted data
verifyKeyFile.writeInt(checksum.length);
verifyKeyFile.write(checksum);
verifyKeyFile.write(data);
verifyKeyFile.sync(true);
}
else
{
// Read from verifyKey.dat as an InputStream. This allows for
// reading the information from verifyKey.dat successfully even when using the jar
// subprotocol to boot derby. (DERBY-1373)
verifyKeyInputStream = privAccessGetInputStream(sf,Attribute.CRYPTO_EXTERNAL_KEY_VERIFY_FILE);
DataInputStream dis = new DataInputStream(verifyKeyInputStream);
// then read the checksum length
int checksumLen = dis.readInt();
byte[] originalChecksum = new byte[checksumLen];
dis.readFully(originalChecksum);
dis.readFully(data);
// decrypt data with key
CipherProvider tmpCipherProvider = createNewCipher(DECRYPT,mainSecretKey,mainIV);
tmpCipherProvider.decrypt(data, 0, data.length, data, 0);
byte[] verifyChecksum = getMD5Checksum(data);
if(!MessageDigest.isEqual(originalChecksum,verifyChecksum))
{