Java Cryptography Extension (JCE) provides framework and implementation for generating key and encryption/decryption of data using various algorithms. In this tutorial, we will use Java DES implementation to encrypt and decrypt a file.

DES is a block cipher algorithm in which we will have to use same key for encryption and decryption. It is one of the basic cypher technique. It’s not safe because we need to give client application secure key to decrypt data.

Java DES Encryption Decryption Steps

  • First of all we need to get the KeyGenerator instance using DES algorithm.
  • Generate SecureKey (key) that will be used for encryption and decryption.
  • Get Cipher instance using DES algorithm, one for encrypt mode and another for decrypt mode. Initialize the cypher object using key and IvParameterSpec object.
  • For encryption, create object of CipherOutputStream using encrypt cipher. For decryption, create object of CipherInputStream using decrypt cipher.
  • Read the input stream and write to the output stream.

Below example first encrypt the file and save encrypted data to new file. Then it decrypts the same file to create the plain text file.

Once program terminates, you can check that both the plain text file have same data and the encrypted file don’t have plain text data. Below is the files content from my sample files, with cipher text highlighted.

java-des-algorithm-program

Further Reading: Generate CSR Java Program

References: Wikipedia and Java Cryptography Architecture

By admin

Leave a Reply