Cryptography has its roots in very complex (and often theoretical) mathematics. As a result, computers and cryptography complement each other well. Today's advanced cryptographic operations involve mind-boggling amounts of mathematical calculations, and computers perform these calculations exponentially faster than a human can perform them by hand. The Java language includes a well-defined architecture that allows you to include cryptographic services in your designs without fully comprehending the mathematical proofs or calculations behind the algorithms. However, this does not mean that it is not important to understand the algorithms (i.e., the cryptographic tools) at your disposal. As an analogy, a screwdriver is a wonderful tool for driving a wood screw into a piece of wood; however, that same screwdriver would not be effective if the object being driven was a finishing nail.
Performing cryptographic operations with Java does not involve hundreds of lines of code or require a Ph.D. in mathematics from MIT. Perhaps the most visible aspect of cryptography is encryption, which can be accomplished in Java using as little as seven lines of code, not counting proper exception handling. Here is a brief example demonstrating a simple encryption operation. Don't worry about comprehending every aspect of the program just yet—we have the whole book to explore Java's cryptographic capabilities!
NOTE: Please review the preface for code style information and download instructions.
Example 1.1 Sample Code Location: com.mkp.jce.chapl.SmallExample
try
{
//Lookup a key generator for the DES cipher KeyGenerator kg = KeyGenerator.getInstance("DES");
//Generate a secret key that can be used by the DES cipher SecretKey key = kg.generateKeyO;
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "DES");
//Lookup an instance of a DES cipher Cipher cipher = Cipher.getlnstance("DES");
//Initialize the cipher using the secret key cipher.init(Cipher.ENCRYPT_MODE, keySpec);
//Encrypt our message
String plainText = "This is a secret message";
byte[] cipherText = cipher.doFinal(plainText.getBytesO);
System.out.println("Resulting Cipher Text:n");
for(int i=0;i<cipherText.length;i++)
{
System.out.print(cipherText[i] + " ");
}
System.out.println("");
} catch (Exception e) {
e.printStackTraceO;
}

|