Title: Professional Java Security
ISBN: 1861004257
US Price: $ 49.99
Canadian Price:
C$ 74.95
UK Price: £ 39.99
© Wrox Press Limited, US and UK.

Reviews : Java Books :
Professional Java Security : Symmetric Encryption

Now we'll open the files for reading and writing. We'll write the IV bytes to the output file unencrypted, as we'll need to use it later to decrypt the file. Then we'll create an IVParameterSpec object that we will use to create a cipher.

	
	FileInputStream fis = new FileInputStream(fileInput);
	FileOutputStream fos = new FileOutputStream(fileOutput);
	
	// Write the IV as the first 16 bytes in the file
	
	fos.write(iv);
	IvParameterSpec spec = new IvParameterSpec(iv);
	
	System.out.println("Initializing the cipher.");
	
	cipher.init(Cipher.ENCRYPT_MODE, key, spec);

Now we want to wrap a CipherOutputStream around the FileOutputStream using the cipher we just created:

	CipherOutputStream cos = new CipherOutputStream(fos, cipher);

Now we simply read the bytes from the input stream and write them to the cipher stream. This will encrypt the entire file. When we're done, we close the input and output.

	System.out.println("Encrypting the file...");

	int theByte = 0;
	while ((theByte = fis.read()) != -1)
	{
		cos.write(theByte);
	}
	fis.close();
	cos.close();
}

Decrypting the file is just the opposite. We read in the IV, initialize a cipher, and create a CipherInputStream and use it to decrypt the file.

	/**
	 *   Decrypt a file using Rijndael. Load the key
	 *   from the filesystem, given a password.
	 */
	
	private static void decrypt(char[] password, String fileInput, String fileOutput)
			throws Exception
	{
		System.out.println("Loading the key.");
		Key key = loadKey(password);
		System.out.println("Loaded the key.");
	
		// Create a cipher using that key to initialize it
	
		Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
	
		FileInputStream fis = new FileInputStream(fileInput);
		FileOutputStream fos = new FileOutputStream(fileOutput);
	
		// Read the IV from the file. It's the first 16 bytes.
	
		byte[] iv = new byte[16];
		fis.read(iv);
	
		IvParameterSpec spec = new IvParameterSpec(iv);
	
		System.out.println("Initializing the cipher.");
		cipher.init(Cipher.DECRYPT_MODE, key, spec);
	
		CipherInputStream cis = new CipherInputStream(fis, cipher);
	
		System.out.println("Decrypting the file...");
	
		int theByte = 0;
		while ((theByte = cis.read()) != -1)
		{
			fos.write(theByte);
		}
		cis.close();
		fos.close();
	}
}

Running the Application

Now let's run the application. We'll begin by creating a key, protected with the password "sasquatch".

	C:\> java FileEncryptor  –c sasquatch

This will print out a few messages like this:

	
	Generating a Rijndael key…
	Done generating the key.

Next we can encrypt a file. Create a file called test.txt in your current directory, and place some text in it. We can then encrypt it to testEncrypted.txt with the following command:

	
	C:\> java FileEncryptor  –e sasquatch test.txt testEncrypted.txt
This will give the following output:
	Loading the key.
	Loaded the key.
	Initializing SecureRandom...
	Initializing the cipher.
	Encrypting the file...

You can now view the testEncrypted.txt file to check that it has been encrypted. If we wish, we can decrypt this file with the following command:

	C:\> java FileEncryptor  –d sasquatch testEncrypted.txt testDecrypted.txt

The output we get from this is:

	Loading the key.
	Loaded the key.
	Initializing the cipher.
	Decrypting the file...

We can now view the file testDecrypted.txt to verify that it has been properly decrypted.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.