CipherStreams with Other Algorithms
There's no requirement that we use Rijndael when encrypting files in the example above. It could easily be
done with any other block cipher without changing anything besides the algorithm name and the bit size of
the key. To use DESede for instance, just change "Rijndael" to "DESede" and the bit size from 256 to 168.
Sometimes we'll want to use a stream cipher, like RC4. RC4 is very fast, and would probably be the best
choice for encrypting extremely large files, like audio or video files. RC4 is not quite as strong as AES, but
as we've mentioned, 128 bits is enough strength for almost any application. Also, if we feel it's necessary,
we can change the keysize we're using for RC4, as it can accept keys of up to 1024 bits.
RC4 doesn't use an initialization vector, as it's built into the algorithm. So we no longer need to create the
IV, store it to the output file, or read it from the input file. There are only two lines that need alterations in
both encrypt() and decrypt(). For encrypt() they are highlighted as shown below:
// Create a cipher using that key to initialize it
Cipher cipher = Cipher.getInstance("RC4");
System.out.println("Initializing SecureRandom...");
// Now we need an Initialization Vector for the cipher in CBC mode.
// We use 16 bytes, because the block size of Rijndael is 256 bits.
SecureRandom random = new SecureRandom();
byte[] iv = new byte[16];
random.nextBytes(iv);
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);
and for decrypt(), they are:
// Create a cipher using that key to initialize it
Cipher cipher = Cipher.getInstance("RC4");
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);
CipherInputStream cis = new CipherInputStream(fis, cipher);
System.out.println("Decrypting the file...");
Now we should be able to encrypt and decrypt files using RC4 instead of Rijndael/AES.
We've finished our discussion of cipher streams, and are going to move on to a new class that uses
symmetric encryption: SealedObject.
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.