# Unlocking the Secrets of Java Cryptography Extensions: The Basics

### 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:

packagecom.dlt.developer.crypto;importjavax.crypto.*; /** *@authorDavid Thurmond* A simple example of encrypting anddecrypting* a text string using Data Encryption Standard encryption. */public classSimpleEncryptExample {public static voidmain(String[] args)throwsException { // First, create the encryption key... System.out.println("Generating key"); KeyGenerator keygen = KeyGenerator.getInstance(""DES/ECB/PKCS5Padding"); 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 theplaintextto encrypt...byte[] plaintext = "The falcon flies at midnight".getBytes(); System.out.println("The plaintext is \'" +newString(plaintext).toString() + "\'"); // Now, do the encryption...byte[] ciphertext = desCipher.doFinal(plaintext); // Display the results... System.out.println("The ciphertext is \'" +newString(ciphertext).toString() + "\'"); // Now, let's prove that the encryption worked bydecrypting// using the same key... System.out.println("Decrypting the ciphertext"); desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // Note that we now initialize the cipher indecryptmode... desCipher.init(Cipher.DECRYPT_MODE, desKey); // Now,decrypttheciphertextand show the results... plaintext = desCipher.doFinal(ciphertext); System.out.println("The plaintext is \'" +newString(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 = KeyGenerator.getInstance("DES/ECB/PKCS5Padding"); 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

*This article was originally published on October 15, 2008*