January 28, 2015
Hot Topics:

Unlocking the Secrets of Java Cryptography Extensions: The Basics

  • October 15, 2008
  • By David Thurmond
  • Send Email »
  • More Articles »

A Simple Example

Now, you can begin looking at the basics with a very simple example program. This program will take plaintext, convert it to unintelligible ciphertext, and then convert it back again. The complete listing is shown in Listing 1.1:

package com.dlt.developer.crypto;

import javax.crypto.*;

 * @author David Thurmond
 * A simple example of encrypting and decrypting
 * a text string using Data Encryption Standard encryption.

public class SimpleEncryptExample {
   public static void main(String[] args) throws Exception {
      // First, create the encryption key...
      System.out.println("Generating key");
      KeyGenerator keygen =
      SecretKey desKey = keygen.generateKey();

      // Now, create the cipher object with appropriate parameters...
      System.out.println("Encrypting using DES/ECB/PKCS5Padding");
      Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
      desCipher.init(Cipher.ENCRYPT_MODE, desKey);

      // Now, set up the plaintext to encrypt...
      byte[] plaintext = "The falcon flies at midnight".getBytes();
      System.out.println("The plaintext is \'" +
         new String(plaintext).toString() + "\'");

      // Now, do the encryption...
      byte[] ciphertext = desCipher.doFinal(plaintext);

      // Display the results...
      System.out.println("The ciphertext is \'" +
         new String(ciphertext).toString() + "\'");

      // Now, let's prove that the encryption worked by decrypting
      // using the same key...
      System.out.println("Decrypting the ciphertext");
      desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

      // Note that we now initialize the cipher in decrypt mode...
      desCipher.init(Cipher.DECRYPT_MODE, desKey);

      // Now, decrypt the ciphertext and show the results...
      plaintext = desCipher.doFinal(ciphertext);
      System.out.println("The plaintext is \'" +
         new String(plaintext).toString() + "\'");
      }    // main

}    // SimpleEncryptExample

Listing 1.1: SimpleEncryptExample.java

The output generated from running this program is shown in Listing 1.2:

Generating key
Encrypting using DES/ECB/PKCS5Padding
The plaintext is 'The falcon flies at midnight'
The ciphertext is 'Im©¾ØLMŸm«L´ÞŒdÄf¾¥«ˆqJ"?'
Decrypting the ciphertext
The plaintext is 'The falcon flies at midnight'

Listing 1.2: Output of SimpleEncryptExample.java

Although the example is a simple one, it demonstrates a great deal of functionality. Now, examine each part of the program and understand what is going on.

First, a key is generated, which is necessary to encrypt the plaintext:

KeyGenerator keygen =
SecretKey desKey = keygen.generateKey();

In the first line, a KeyGenerator object is specified by supplying the encryption algorithm that you will use later on when encrypting the data. This is necessary because different encryption algorithms require keys with differing characteristics, so, for example, a DES-compatible key is not suitable for encrypting data using the RSA encryption scheme.

Next, the cipher is created and initialized:

Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding);
desCipher.init(Cipher.ENCRYPT_MODE, desKey);

In line 1 above, the cipher is initialized by specifying a string called a transform. The transform has the format <encryption algorithm>/<mode>/<padding scheme>. In this case, the encryption algorithm is DES, the mode is ECB (electronic codebook), and the padding scheme is PKCS5Padding. The cipher could also be created without supplying the mode and padding parameters, in which case appropriate default values will be assigned by the encryption provider, as shown below:

Cipher desCipher = Cipher.getInstance("DES");

A complete listing of the encryption algorithms, modes, and padding schemes available within the Sun JCE can be found in the Java Cryptography Extension Reference Guide, Appendix A, at http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html#AppA.

Next, the cipher is initialized by executing the following:

desCipher.init(Cipher.ENCRYPT_MODE, desKey);

Page 2 of 6

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel