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 andIvParameterSpec
object. - For encryption, create object of
CipherOutputStream
using encrypt cipher. For decryption, create object ofCipherInputStream
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.journaldev.des; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; public class DESEncryptionExample { private static Cipher encryptCipher; private static Cipher decryptCipher; private static final byte[] iv = { 11, 22, 33, 44, 99, 88, 77, 66 }; public static void main(String[] args) { String clearTextFile = "/Users/pankaj/source.txt"; String cipherTextFile = "/Users/pankaj/cipher.txt"; String clearTextNewFile = "/Users/pankaj/source-new.txt"; try { // create SecretKey using KeyGenerator SecretKey key = KeyGenerator.getInstance("DES").generateKey(); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); // get Cipher instance and initiate in encrypt mode encryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); // get Cipher instance and initiate in decrypt mode decryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec); // method to encrypt clear text file to encrypted file encrypt(new FileInputStream(clearTextFile), new FileOutputStream(cipherTextFile)); // method to decrypt encrypted file to clear text file decrypt(new FileInputStream(cipherTextFile), new FileOutputStream(clearTextNewFile)); System.out.println("DONE"); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IOException e) { e.printStackTrace(); } } private static void encrypt(InputStream is, OutputStream os) throws IOException { // create CipherOutputStream to encrypt the data using encryptCipher os = new CipherOutputStream(os, encryptCipher); writeData(is, os); } private static void decrypt(InputStream is, OutputStream os) throws IOException { // create CipherOutputStream to decrypt the data using decryptCipher is = new CipherInputStream(is, decryptCipher); writeData(is, os); } // utility method to read data from input stream and write to output stream private static void writeData(InputStream is, OutputStream os) throws IOException { byte[] buf = new byte[1024]; int numRead = 0; // read and write operation while ((numRead = is.read(buf)) >= 0) { os.write(buf, 0, numRead); } os.close(); is.close(); } } |
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.
Further Reading: Generate CSR Java Program
References: Wikipedia and Java Cryptography Architecture