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

Simple Encryption Example

Now that we've gone over some of the classes and methods we'll need for symmetric encryption, we're going to create a simple program that utilizes them. Our program will create a DESede key, and then encrypt a string with it. We'll then decrypt the encrypted string and display it along with the plaintext and ciphertext bytes as and when we use them.

Here we're using the name DESede rather than TripleDES since that's the term used in the Bouncy Castle provider. You may have to change this if you using a different provider.
import java.security.*;
import javax.crypto.*;

/**
 * 	SimpleExample.java
 */

public class SimpleExample
{
	public static void main (String[] args)
			throws Exception
	{
		if (args.length != 1) {
			System.err.println("Usage: java SimpleExample text");
			System.exit(1);
		}
		String text = args[0];

		System.out.println("Generating a DESede (TripleDES) key...");

		// Create a TripleDES key

		KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
		keyGenerator.init(168);   // need to initialize with the keysize
		Key key = keyGenerator.generateKey();

		System.out.println("Done generating the key.");

		// Create a cipher using that key to initialize it

		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key);

		byte[] plaintext = text.getBytes("UTF8");

		// Print out the bytes of the plaintext

		System.out.println("\nPlaintext: ");
		for (int i=0;i<plaintext.length;i++) {
			System.out.print(plaintext[i]+" ");
		}

		// Perform the actual encryption

		byte[] ciphertext = cipher.doFinal(plaintext);

		// Print out the ciphertext

		System.out.println("\n\nCiphertext: ");
		for (int i=0;i<ciphertext.length;i++) {
			System.out.print(ciphertext[i]+" ");
		}

		// Re-initialize the cipher to decrypt mode

		cipher.init(Cipher.DECRYPT_MODE, key);

		// Perform the decryption

		byte[] decryptedText = cipher.doFinal(ciphertext);

		String output = new String(decryptedText,"UTF8");

		System.out.println("\n\nDecrypted text: "+output);
	}
}


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.